簡體   English   中英

Android BLE:成功將ENABLE_NOTIFICATION_VALUE值寫入BluetoothGattDescriptor,但是onCharacteristicChanged不會觸發

[英]Android BLE: Successfully write ENABLE_NOTIFICATION_VALUE value to BluetoothGattDescriptor but onCharacteristicChanged never fires

我有一個BLE外設,可以在iOS和Android 7.0上與給定的服務/特性UUID很好地配合工作。 在Android 6.0棉花糖上,即使在描述符上設置了ENABLE_NOTIFICATION_VALUE,onCharacteristicChanged方法也不會觸發。 請幫我弄清楚如何為運行Android OS 6.0 Marshmallow的Android設備觸發onCharacteristicChanged。 這是我用來嘗試使通知起作用的代碼:

    boolean success = mBluetoothGatt.setCharacteristicNotification(characteristic, true);
    Log.e(TAG, "set char notification = " + (success ? "It worked :)" : "It did not work :("));

    if (UUID_DATA_PACKET.equals(characteristic.getUuid())) {
        BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
                UUID.fromString(EkoCoreGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));

        descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);

        success = mBluetoothGatt.writeDescriptor(descriptor);
        Log.e(TAG, "set descriptor = " + descriptor.getCharacteristic().getWriteType() + ", success = " + (success ? "It worked :)" : "It did not work :("));
    }

在上面的代碼中,setCharacteristicNotification和writeDescriptor調用均返回成功(1 / true / etc)。 另外,onDescriptorWrite回調返回GATT_SUCCESS。 但是,當我們稍后在代碼中讀取描述符時,發現通知值仍設置為禁用。 我們已經嘗試了許多解決方案,例如在setCharacteristicNotification和writeDescriptor調用之間設置延遲,但尚未找到解決此問題的解決方案。 與相關外圍設備配對沒有問題,但是以某種方式獲得通知似乎是不可能的。 任何提示將不勝感激。

在編寫描述符之前添加以下內容解決了我的問題:

characteristic.setWriteType(BluetoothGattCharacteristic.WRIT‌​E_TYPE_DEFAULT);

顯然,Android 6中存在一些錯誤。希望此答案對其他人有所幫助。 很難找到。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM