zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

【错误记录】Android 应用连接 BLE 设备无法读取数据 ( 可以写出数据 | 无法读取数据 )

2023-06-13 09:17:44 时间

文章目录

一、问题描述


Android 应用连接 BLE 硬件设备后 , 出现如下情况 :

发送数据成功 : Android 应用 向 BLE 硬件设备发送数据 , 成功 ;

接收数据失败 : Android 应用 无法接收到 BLE 硬件设备发送给手机的数据 ;

二、问题分析


举个栗子 :

这是在 Google 官方的 BLE 蓝牙示例程序 BluetoothLeGatt 中的 BLE 连接配置代码 :

    /**
     * Enables or disables notification on a give characteristic.
     *
     * @param characteristic Characteristic to act on.
     * @param enabled If true, enable notification.  False otherwise.
     */
    public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
                                              boolean enabled) {
        if (mBluetoothAdapter == null || mBluetoothGatt == null) {
            Log.w(TAG, "BluetoothAdapter not initialized");
            return;
        }
        mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
    }

代码文件地址 : BluetoothLeService.java

上述代码是在遍历完 BluetoothGattService 与 BluetoothGattCharacteristic 之后 , 选择读取指定特性 ( BluetoothGattCharacteristic ) 中的数据 , 就将特性传入上述 setCharacteristicNotification 方法 参数 ;

但是上述设置 , 仅设置了一半内容 , 还需要为 BluetoothGattCharacteristic 中的 BluetoothGattDescriptor 作进一步设置 ;

在上面的基础上 , 还需要为 BluetoothGattCharacteristic 中的 BluetoothGattDescriptor 集合中的所有元素设置 BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE 值 , 然后写出该 BluetoothGattDescriptor , 此时设置读取该 BluetoothGattCharacteristic 特性值才能生效 , 否则无法读取其中的数据 ;

BluetoothGattCharacteristic 中维护了下面的变量 , BluetoothGattDescriptor 队列 , 通过调用下面的 getDescriptors 方法 , 获取该队列 ;

public class BluetoothGattCharacteristic implements Parcelable {
    /**
     * List of descriptors included in this characteristic.
     */
    protected List<BluetoothGattDescriptor> mDescriptors;

    /**
     * Returns a list of descriptors for this characteristic.
     *
     * @return Descriptors for this characteristic
     */
    public List<BluetoothGattDescriptor> getDescriptors() {
        return mDescriptors;
    }
}

调用 BluetoothGattDescriptor 的 setValue 方法 , 为其设置 BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE 值 , 并写出该值 , 即可将读取该特性的设置发送给 BLE 蓝牙模块 ;

public class BluetoothGattDescriptor implements Parcelable {
    /**
     * Updates the locally stored value of this descriptor.
     *
     * <p>This function modifies the locally stored cached value of this
     * descriptor. To send the value to the remote device, call
     * {@link BluetoothGatt#writeDescriptor} to send the value to the
     * remote device.
     *
     * @param value New value for this descriptor
     * @return true if the locally stored value has been set, false if the requested value could not
     * be stored locally.
     */
    public boolean setValue(byte[] value) {
        mValue = value;
        return true;
    }
}

三、完整设置代码


    public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
                                              boolean enabled) {
        if (mBluetoothAdapter == null || mBluetoothGatt == null) {
            Log.w(TAG, "BluetoothAdapter not initialized");
            return;
        }
        mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);

		// 获取 指定 BluetoothGattCharacteristic 中的 List<BluetoothGattDescriptor> mDescriptors 队列
        List<BluetoothGattDescriptor> descriptors = characteristic.getDescriptors();
        // 遍历设置 BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE 值 , 并写出
        for(BluetoothGattDescriptor descriptor : descriptors) {
                    descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
                    mBluetoothGatt.writeDescriptor(descriptor);
        }

    }

进行上述修改后 , 便可接收 BLE 蓝牙设备的数据 ;