我试图在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