gpt4 book ai didi

android - 无法使用 Android BluetoothProfile 连接到蓝牙 Health Device Fora

转载 作者:太空宇宙 更新时间:2023-11-03 10:46:24 25 4
gpt4 key购买 nike

我想通过 Android BluetoothPRofile 连接到 Fora 温度计并获取读数。以下是我的方法:-

  1. 在 OnCreate() 中我写了这段代码:-

    if (!mBluetoothAdapter.getProfileProxy(this, mBluetoothServiceListener,
    BluetoothProfile.HEALTH)) {
    Toast.makeText(this, "No Health Profile Supported",
    Toast.LENGTH_LONG);
    return;
    }
  2. 这会触发下面提到的 mBluetoothServiceListener 回调:-

    @SuppressLint("NewApi")
    private final BluetoothProfile.ServiceListener mBluetoothServiceListener =
    new BluetoothProfile.ServiceListener() {
    public void onServiceConnected(int profile, BluetoothProfile proxy) {

    Log.d(TAG, "onServiceConnected to profile: " + profile + " while health is " + BluetoothProfile.HEALTH);
    if (profile == BluetoothProfile.HEALTH) {
    mBluetoothHealth = (BluetoothHealth) proxy;
    if (Log.isLoggable(TAG, Log.DEBUG))
    Log.d(TAG, "onServiceConnected to profile: " + profile);
    }
    else if(profile == BluetoothProfile.HEADSET)
    {
    Log.d(TAG, "onServiceConnected to profile: " + profile);
    }
    }

    public void onServiceDisconnected(int profile) {
    if (profile == BluetoothProfile.HEALTH) {
    mBluetoothHealth = null;
    }
    }
    };
  3. 在此之后,代码会搜索附近的蓝牙设备并将其显示在列表中。该列表项的 onclicklistener 调用以下代码:-

    boolean bool = mBluetoothHealth.registerSinkAppConfiguration(TAG, HEALTH_PROFILE_SOURCE_DATA_TYPE, mHealthCallback);

    // where HEALTH_PROFILE_SOURCE_DATA_TYPE = 0x1008 (it's a body thermometer)
  4. 注册过程完成后,我会尝试像这样连接设备:-

        boolean bool = mBluetoothHealth.connectChannelToSource(mDevice, mHealthAppConfig);
  5. 一旦完成上述所有步骤,BluetoothHealthCallback 就会在任何时候被调用设备连接状态改变

            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;
    } else if (status == BluetoothHealth.APP_CONFIG_REGISTRATION_SUCCESS) {
    mHealthAppConfig = config;
    } else if (status == BluetoothHealth.APP_CONFIG_UNREGISTRATION_FAILURE ||
    status == BluetoothHealth.APP_CONFIG_UNREGISTRATION_SUCCESS) {
    }
    }

    // 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_DISCONNECTED &&
    newState == BluetoothHealth.STATE_CHANNEL_CONNECTED) {
    if (config.equals(mHealthAppConfig)) {
    mChannelId = channelId;

    (new ReadThread(fd)).start();
    } else {

    }
    } else if (prevState == BluetoothHealth.STATE_CHANNEL_CONNECTING &&
    newState == BluetoothHealth.STATE_CHANNEL_DISCONNECTED) {

    } else if (newState == BluetoothHealth.STATE_CHANNEL_DISCONNECTED) {
    if (config.equals(mHealthAppConfig)) {

    } else {

    }
    }
    }
    };

在上述情况下,我得到 BluetoothHealthCallback 恰好 2 次:-

  1. 我第一次得到 prevState = BluetoothHealth.STATE_CHANNEL_DISCONNECTED 和newState = BluetoothHealth.STATE_CHANNEL_CONNECTING

  2. 在第二次我得到 prevState = BluetoothHealth.STATE_CHANNEL_CONNECTING 和newState = BluetoothHealth.STATE_CHANNEL_DISCONNECTED

即尝试连接到设备,但未成功。同样在这两个回调中,ParcelFileDescriptor 都是空的

*注意:设备已经配对

最佳答案

你认为你下面的“if”语句正确吗?

if (prevState == BluetoothHealth.STATE_CHANNEL_DISCONNECTED && newState == BluetoothHealth.STATE_CHANNEL_CONNECTED) {

它应该是(因为它发生在经典蓝牙设备上)

if (prevState == BluetoothHealth.STATE_CHANNEL_CONNECTING && newState == BluetoothHealth.STATE_CHANNEL_CONNECTED) {

先连接再连接

或者我可以只是

if (newState == BluetoothHealth.STATE_CHANNEL_CONNECTED) {

对其他“if”语句进行同样的更正。

此外,设备是否支持配对和连接选项?如果是,那么您可以尝试手动连接它。

关于android - 无法使用 Android BluetoothProfile 连接到蓝牙 Health Device Fora,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22809749/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com