在Android上使用健康设备配置文件时出现异常

时间:2013-03-08 11:16:54

标签: android sockets bluetooth

我正在尝试通过蓝牙使用健康设备配置文件和样本从Onyx2(Fingertip Oximeter)获取数据,该数据可在Android Developers site找到。但我收到以下错误

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();

1 个答案:

答案 0 :(得分:1)

我记得Android HDP演示应用程序在终止时没有关闭蓝牙连接时遇到了一些问题。 您可以进行的一项测试是:

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

如果这些步骤在重复时始终如一地工作,我几乎可以肯定这与应用程序没有释放资源有关,就像我之前感到悲伤一样。