android BLE 4.0 setCharacteristicNotification接收不到数据
Posted 持续学习刻意练习
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android BLE 4.0 setCharacteristicNotification接收不到数据相关的知识,希望对你有一定的参考价值。
蓝牙开发踩的一个坑~特此记录~
问题描述:最近在开发 android BLE 读写数据
但是向设备写数据很顺利,但是在接收设备传来的数据时,死活接收不到。
/**
* Enables or disables notification on a give characteristic.
*
* @param characteristic Characteristic to act on.
* @param enabled If true, enable notification. False otherwise.
*/
public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
boolean enabled)
if (mBluetoothAdapter == null || mBluetoothGatt == null)
Log.w(TAG, "BluetoothAdapter not initialized");
return;
mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
上面这样写是收不到数据的,主要是少加了 mBluetoothGatt.writeDescriptor(descriptor);
下面是修改后的代码:
/**
* Enables or disables notification on a give characteristic.
*
* @param characteristic Characteristic to act on.
* @param enabled If true, enable notification. False otherwise.
*/
public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
boolean enabled)
if (mBluetoothAdapter == null || mBluetoothGatt == null)
Log.w(TAG, "BluetoothAdapter not initialized");
return;
// mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
boolean isEnableNotification = mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
if(isEnableNotification)
List<BluetoothGattDescriptor> descriptorList = characteristic.getDescriptors();
if(descriptorList != null && descriptorList.size() > 0)
for(BluetoothGattDescriptor descriptor : descriptorList)
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);
如果不是这种情况接收不到数据就是另外一种可能了characteristic 的 uuid 设置不对,
你可以看下 boolean isEnableNotification = mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
返回的是false 还是true。
本位转载自:http://347563186.iteye.com/blog/2331399
以上是关于android BLE 4.0 setCharacteristicNotification接收不到数据的主要内容,如果未能解决你的问题,请参考以下文章
android BLE 4.0 setCharacteristicNotification接收不到数据