【问题标题】:Android BLE read Gatt CharacteristicAndroid BLE 读取 Gatt 特性
【发布时间】:2016-09-28 08:17:26
【问题描述】:

我正在尝试在我的 APP 中读取一些蓝牙特性。 现在我有一个问题,从我的 Gatt 服务器更改特性后该怎么做。起初,我尝试使用线程一次又一次地重新触发对特征的读取,如下所示:

new Thread(new Runnable() {
    @Override
    public void run() {
        int[] newData = new int[30];
        while(true){
            try{
                for(int i=0;i<newData.length;i++){
                    newData[i] = 0;
                }
                BluetoothGatt tmpGatt = refExtDataClass.getRefBluetoothGatt();
                tmpGatt.readCharacteristic(characteristic);

                byte[] value = characteristic.getValue();

                for(int i=0;i<newData.length;i++){
                    newData[i] = value[i];
                }
                refExtDataClass.setNmData(newData);
            }catch(Exception e){
                break;
            }
        }
    }
}).start();

但问题是数据似乎在某一时刻损坏了(就像我总是从我的 MCU 端将相同的数据写入特征)。

是否允许这样读取 BLE 数据?是否有任何建议的方式来一直读取 BLE 数据?或者在我的 App 端更新它?

如果您需要任何其他代码,请告诉我。

【问题讨论】:

    标签: android bluetooth-lowenergy gatt characteristics


    【解决方案1】:

    读取 GATT 特征是一个异步操作。在您收到onCharacteristicRead 回调之前,结果不可用。

    无论如何,您应该将 GATT 服务器配置为在有新数据要发送时发送notifications,而不是一直轮询它。

    【讨论】:

    • 很高兴听到我根本不喜欢我的“解决方案”的另一种方式。我刚刚读到我需要“激活”来自我的 gatt 客户端的通知。如果我使用characteristic.addDescriptor(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE)就足够了吗?
    猜你喜欢
    • 1970-01-01
    • 2014-02-13
    • 2016-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多