【问题标题】:What is the proper method for reading a GATT characteristic in Android?在 Android 中读取 GATT 特征的正确方法是什么?
【发布时间】:2013-11-29 14:43:26
【问题描述】:

在尝试读取 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);
    }
    

以上是导致我遇到此问题的原始代码。在其中,我尝试读取特征的值,然后使用按钮简单地切换其状态。

【问题讨论】:

    标签: bluetooth-lowenergy android-bluetooth android-4.3-jelly-bean


    【解决方案1】:
    BluetoothGatt.readCharacteristic(BluetoothGattCharacteristic characteristic)
    

    此函数正在使用来自蓝牙的特征值更新您的 BluetoothGattCharacteristic 对象(在您的 Android 设备上)。

    BluetoothGattCharacteristic.getValue()
    

    这个函数只是BluetoothGattCharacteristic 对象的一个​​getter 函数。 android和蓝牙设备之间没有任何交易。

    【讨论】:

    • 感谢您的澄清,但我还有一个问题。您认为我可以在同一个点击操作中读取特征并获得其值吗?由于无线连接的性质,是否会出现时间问题?
    • 等到 onCharacteristicRead 回调触发,否则你只是得到旧值。
    猜你喜欢
    • 1970-01-01
    • 2017-02-12
    • 2016-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多