关于android:无法从BLE设备读取特征值

Not able to read a characteristic value from BLE device

我需要向 Android BLE 设备写入和读取特征值。我能写但不能读。这是我的写作方式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public void writeCustomCharacteristic(int value) {
    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        Log.w(TAG,"BluetoothAdapter not initialized");
        return;
    }
    /*check if the service is available on the device*/
    BluetoothGattService mCustomService = mBluetoothGatt.getService(UUID.fromString("<MY SERVICE UUID>"));
    if(mCustomService == null){
        Log.w(TAG,"Custom BLE Service not found");
        return;
    }
    /*get the read characteristic from the service*/
    BluetoothGattCharacteristic mWriteCharacteristic = mCustomService.getCharacteristic(UUID.fromString("<MY CHARACTERSTIC UUID>"));
   mWriteCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
       mWriteCharacteristic.setValue(value,BluetoothGattCharacteristic.FORMAT_UINT8,0);
    if(!mBluetoothGatt.writeCharacteristic(mWriteCharacteristic)){
        Log.w(TAG,"Failed to write characteristic");
    }else{
        Log.w(TAG,"Success to write characteristic");
    }
}

如果我使用

这个操作是成功的

1
 mWriteCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);

如果我使用任何其他写入类型而不是 WRITE_TYPE_NO_RESPONSE 那么它不会触发 onCharacteristicWrite() 回调方法。

这是我阅读特征的方式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
 private boolean readCharacteristics(int groupPosition,int childPosition){
    try{
        if (mGattCharacteristics != null) {
            final BluetoothGattCharacteristic characteristic = mGattCharacteristics.get(2).get(2);
            final int charaProp = characteristic.getProperties();
            if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
                // If there is an active notification on a characteristic, clear
                // it first so it doesn’t update the data field on the user interface.
                if (mNotifyCharacteristic != null) {
                    mBluetoothLeService.setCharacteristicNotification(mNotifyCharacteristic, false);
                    mNotifyCharacteristic = null;
                }
                mBluetoothLeService.readCharacteristic(characteristic);
            }
            if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
                mNotifyCharacteristic = characteristic;
                mBluetoothLeService.setCharacteristicNotification(characteristic, true);
            }
            waitSometime(100);
            if ((charaProp | BluetoothGattCharacteristic.PROPERTY_INDICATE) > 0) {
                mNotifyCharacteristic = characteristic;
                mBluetoothLeService.setCharacteristicIndication(characteristic, true);
            }
            waitSometime(100);
            if ((charaProp | BluetoothGattCharacteristic.PROPERTY_WRITE) > 0) {

                byte[] value = characteristic.getValue(); //this is being NULL always

                if (mNotifyCharacteristic != null) {
                    mBluetoothLeService.setCharacteristicNotification(mNotifyCharacteristic, true);
                    mNotifyCharacteristic = null;
                }
                mBluetoothLeService.writeCharacteristic(characteristic, value);
                if(value!=null&&value.length>0) {
                    Toast.makeText(DeviceControlActivity.this,"success reading data",Toast.LENGTH_LONG).show();
                }else{
                  Toast.makeText(DeviceControlActivity.this,"error reading data",Toast.LENGTH_LONG).show();
                }
            }
       /* if ((charaProp | BluetoothGattCharacteristic.PROPERTY_INDICATE) > 0) {
            mNotifyCharacteristic = characteristic;
            //characteristic.setWriteType();
            mBluetoothLeService.setCharacteristicIndication(
                    characteristic, true);
            byte[] value=characteristic.getValue();
        }*/
            return true;
        }
    }catch (Exception e){
        e.printStackTrace();
    }
    return false;
}

characteristic.getValue() 始终返回 null。我哪里错了?


这是我的看法:

你需要弄清楚你将获得什么样的特征数据?其中一些需要先设置指示或通知!喜欢:

一个。获取特征的描述符

1
2
   BluetoothGattDescriptor descriptor = gattCharacteristic.getDescriptors().get(0);
     //Usually is Client_Characteristic_Configuration

乙。根据特性,设置描述符的property()为true。

1
2
3
4
5
6
7
8
9
10
if ((gattCharacteristic.PROPERTY_NOTIFY)> 0 ){
    if (descriptor != null) {
       descriptor.setValue((BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE));
       }
   }
else if((gattCharacteristic.PROPERTY_INDICATE) >0 ){
    if(descriptor != null){
       descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
       }
   }

c。将值发送到远程 BLE 设备。

1
gatt.writeDescriptor(descriptor);

之后,你可以在回调函数中获取数据:

1
onCharacteristicChanged

至少以上对我有用!
希望能帮到人~


原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/268981.html

(0)
上一篇 2022年6月20日 14:25
下一篇 2022年6月20日 14:25

相关推荐

发表回复

登录后才能评论