0

我正在尝试使用健康设备配置文件和示例通过蓝牙从 Onyx2(指尖血氧计)获取数据,这些数据可以在Android 开发者网站上找到。但我收到以下错误

E/BluetoothEventLoop.cpp(432): onHealthDeviceConnectionResult: D-Bus error: org.bluez.Error.HealthError (Error getting remote SDP records). 这个问题的原因是什么?顺便说一句,大约 50 次中的 1 次,我得到了数据。

// Callbacks to handle connection set up and disconnection clean up.
private final BluetoothProfile.ServiceListener mBluetoothServiceListener =
        new BluetoothProfile.ServiceListener() {
    public void onServiceConnected(int profile, BluetoothProfile proxy) {
        if (profile == BluetoothProfile.HEALTH) {
            mBluetoothHealth = (BluetoothHealth) proxy;
            if (Log.isLoggable(TAG, Log.DEBUG))
                Log.d(TAG, "onServiceConnected to profile: " + profile);
        }
    }

    public void onServiceDisconnected(int profile) {
        if (profile == BluetoothProfile.HEALTH) {
            mBluetoothHealth = null;
        }
    }
};

private final BluetoothHealthCallback mHealthCallback = new BluetoothHealthCallback() {
    // Callback to handle application registration and unregistration events.  The service
    // passes the status back to the UI client.
    public void onHealthAppConfigurationStatusChange(BluetoothHealthAppConfiguration config,
            int status) {
        if (status == BluetoothHealth.APP_CONFIG_REGISTRATION_FAILURE) {
            mHealthAppConfig = null;
            sendMessage(STATUS_HEALTH_APP_REG, RESULT_FAIL);
        } else if (status == BluetoothHealth.APP_CONFIG_REGISTRATION_SUCCESS) {
            mHealthAppConfig = config;
            sendMessage(STATUS_HEALTH_APP_REG, RESULT_OK);
        } else if (status == BluetoothHealth.APP_CONFIG_UNREGISTRATION_FAILURE ||
                status == BluetoothHealth.APP_CONFIG_UNREGISTRATION_SUCCESS) {
            sendMessage(STATUS_HEALTH_APP_UNREG,
                    status == BluetoothHealth.APP_CONFIG_UNREGISTRATION_SUCCESS ?
                    RESULT_OK : RESULT_FAIL);
        }
    }

    // Callback to handle channel connection state changes.
    // Note that the logic of the state machine may need to be modified based on the HDP device.
    // When the HDP device is connected, the received file descriptor is passed to the
    // ReadThread to read the content.
    public void onHealthChannelStateChange(BluetoothHealthAppConfiguration config,
            BluetoothDevice device, int prevState, int newState, ParcelFileDescriptor fd,
            int channelId) {
        if (Log.isLoggable(TAG, Log.DEBUG))
            Log.d(TAG, String.format("prevState\t%d ----------> newState\t%d",
                    prevState, newState));
        if (prevState == BluetoothHealth.STATE_CHANNEL_CONNECTING &&
                newState == BluetoothHealth.STATE_CHANNEL_CONNECTED) {
            if (config.equals(mHealthAppConfig)) {
                mChannelId = channelId;
                sendMessage(STATUS_CREATE_CHANNEL, RESULT_OK);
                (new ReadThread(fd)).start();
            } else {
                sendMessage(STATUS_CREATE_CHANNEL, RESULT_FAIL);
            }
        } else if (prevState == BluetoothHealth.STATE_CHANNEL_CONNECTING &&
                   newState == BluetoothHealth.STATE_CHANNEL_DISCONNECTED) {
            sendMessage(STATUS_CREATE_CHANNEL, RESULT_FAIL);
        } else if (newState == BluetoothHealth.STATE_CHANNEL_DISCONNECTED) {
            if (config.equals(mHealthAppConfig)) {
                sendMessage(STATUS_DESTROY_CHANNEL, RESULT_OK);
            } else {
                sendMessage(STATUS_DESTROY_CHANNEL, RESULT_FAIL);
            }
        }
    }
};
// Initiates application registration through {@link
    // BluetoothHDPService}.
    Button registerAppButton = (Button) findViewById(R.id.button_register_app);
    registerAppButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            sendMessage(BluetoothHDPService.MSG_REG_HEALTH_APP,
                    HEALTH_PROFILE_SOURCE_DATA_TYPE);
        }
    });

    // Initiates application unregistration through {@link
    // BluetoothHDPService}.
    Button unregisterAppButton = (Button) findViewById(R.id.button_unregister_app);
    unregisterAppButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            sendMessage(BluetoothHDPService.MSG_UNREG_HEALTH_APP, 0);
        }
    });

    // Initiates channel creation through {@link BluetoothHDPService}. Some
    // devices will
    // initiate the channel connection, in which case, it is not necessary
    // to do this in the
    // application. When pressed, the user is asked to select from one of
    // the bonded devices
    // to connect to.
    Button connectButton = (Button) findViewById(R.id.button_connect_channel);
    connectButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            mAllBondedDevices = (BluetoothDevice[]) mBluetoothAdapter
                    .getBondedDevices().toArray(new BluetoothDevice[0]);

            if (mAllBondedDevices.length > 0) {
                int deviceCount = mAllBondedDevices.length;
                if (mDeviceIndex < deviceCount)
                    mDevice = mAllBondedDevices[mDeviceIndex];
                else {
                    mDeviceIndex = 0;
                    mDevice = mAllBondedDevices[0];
                }
                String[] deviceNames = new String[deviceCount];
                int i = 0;
                for (BluetoothDevice device : mAllBondedDevices) {
                    deviceNames[i++] = device.getName();
                }
                SelectDeviceDialogFragment deviceDialog = SelectDeviceDialogFragment
                        .newInstance(deviceNames, mDeviceIndex);
                deviceDialog.show(getFragmentManager(), "deviceDialog");
            }
        }
    });

    // Initiates channel disconnect through {@link BluetoothHDPService}.
    Button disconnectButton = (Button) findViewById(R.id.button_disconnect_channel);
    disconnectButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            disconnectChannel();
        }
    });
    registerReceiver(mReceiver, initIntentFilter());
}

// Sets up communication with {@link BluetoothHDPService}.
private ServiceConnection mConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName name, IBinder service) {
        mHealthServiceBound = true;
        Message msg = Message.obtain(null,
                BluetoothHDPService.MSG_REG_CLIENT);
        msg.replyTo = mMessenger;
        mHealthService = new Messenger(service);
        try {
            mHealthService.send(msg);
        } catch (RemoteException e) {
            Log.w(TAG, "Unable to register client to service.");
            e.printStackTrace();
4

1 回答 1

1

我记得 Android HDP 演示应用程序在终止时不关闭蓝牙连接有一些问题。您可以进行的一项测试来验证这一点:

  • 关闭你的安卓的蓝牙。
  • 再次打开它。
  • 与健康设备配对(在安卓设置中)
  • 启动你的安卓应用程序。
  • 尝试使用健康设备连接和传输数据。

如果这些步骤在重复时始终如一,我几乎可以肯定这与应用程序没有像我以前那样释放资源有关。

于 2013-03-19T18:45:32.690 回答