我試圖在Android中使用具有字段的設備的BLE:電池(RO),狀態(RO),強度(R/W)。 我跟着一些關於使用設備設置Gatt的教程。 我使用下面的代碼:BLE readCharacteristics總是返回false或getvalue爲空
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
Log.w(LOGGER + mOwner.get().getName(), "onServicesDiscovered received: " + status);
if (status == BluetoothGatt.GATT_SUCCESS) {
// Save all services
for (BluetoothGattService gattService : mBluetoothGatt.getServices()) {
for (BluetoothGattCharacteristic charac : gattService
.getCharacteristics()) {
if (isContained(charac) {
mCharacteristics.set(mCharacteristicsUuid.indexOf(charac.getUuid()
.toString()), charac);
mBluetoothGatt.setCharacteristicNotification(charac, true);
// UUID for notification
BluetoothGattDescriptor descriptor = charac
.getDescriptor(UUID.fromString("00002902-0000-1000-" +
"8000-00805f9b34fb"));
if (descriptor != null) {
descriptor.setValue(BluetoothGattDescriptor
.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);
}
}
}
}
所以我儘量發現服務,如果做迭代他們,和他們的特性。如果他們的UUID是正確的,我將它們添加到mCharacteristics中。 如果這是一個可讀的值(這裏是其中的每個人),我啓用通知。
當它這樣做我試着做了第一次讀:
Log.e(LOGGER + mOwner.get().getName(), "Reading first charac(index=0) : " + mBluetoothGatt
.readCharacteristic(mCharacteristics.get(0)));
}
我必須精確,所有的這是一個專門的線程內,在Android服務跟上。 我確定設備已連接,並且具有特徵。 對於每一個我驗證了(性能&可重寫)的值,總是> 0 ... 但是對於只讀特性,我總是在讀取時出現錯誤... 而對於強度(讀/寫)返回true,但onCharacteristicRead()永遠不會被調用,並且getValue()方法返回null。
我很新,在Android中使用BLE。
有人對此問題有所瞭解嗎? 在此先感謝!編輯:我終於找到了解決方案,但沒有如我所料... 事實上,如果我設置通知啓用,ii無法讀取後的特徵... 有人可以告訴我如何我可以執行這樣:
> 1) on discover
> a) get all characts + set in list characs I want
> b) enable notification for ALL of these charac (if possible, I supposed, because of the descriptor that can be null ?)
> c) first read to know starting values