在Android中读取GATT特征的正确方法是什么?

est*_*bro 10 bluetooth-lowenergy android-bluetooth android-4.3-jelly-bean

在试图读取Android API 18中蓝牙低功耗GATT特性的值时,我遇到了以下困境:检索存储在特征中的值的正确方法是什么?并且应该在哪个级别的堆栈中执行此操作?

在进行自己的研究时,我偶然发现了两种可能的方法:

  • BluetoothGatt .readCharacteristic(BluetoothGattCharacteristic特性)
  • BluetoothGattCharacteristic .getValue()

    public void onClick(View v){        
        byteValue = mBTValueCharacteristic.getValue();
        if ((byteValue[0] & 0x01) == 1)
            byteValue[0] = 0x00;
        else
            byteValue[0] = 0x01;
    
        mBTValueCharacteristic.setValue(byteValue);
        mBTGatt.writeCharacteristic(mBTValueCharacteristic);
    }
    
    Run Code Online (Sandbox Code Playgroud)

以上是引导我解决这个问题的原始代码.在其中,我尝试读取特征的值,并使用按钮简单地切换其状态.

reT*_*eTs 7

BluetoothGatt.readCharacteristic(BluetoothGattCharacteristic characteristic)
Run Code Online (Sandbox Code Playgroud)

此功能使用蓝牙的特征值更新您的BluetoothGattCharacteristic对象(在您的Android设备上).

BluetoothGattCharacteristic.getValue()
Run Code Online (Sandbox Code Playgroud)

此功能只是BluetoothGattCharacteristic对象的getter功能.android和蓝牙设备之间没有任何交易.

  • 等到 onCharacteristicRead 回调触发,否则您将获得旧值。 (2认同)