Android BLE接收的设置的小坑
2016-06-28 本文已影响4875人
蓝点工坊
一个蓝牙接收框架,在TI CC2541下工作正常,接收采用Notify字符进行接收.
在查找到这个Characteristic后,如下方法设置通知可以.
boolean ret = mBluetoothGatt.setCharacteristicNotification(mRxChar,true);
然后在蓝牙回调函数中的接收数据
public void onCharacteristicChanged(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic)
但是这套代码切换到NORDIC 51822环境下可以扫描,联接,发送,但就是不能接收.
在Nordic的官方的nRF ToolBox的源码中,经过查阅分析后发现,是setCharacteristicNotification()不能真正打开Notify 接收通知.于把其初始化方法摘出来放在蓝牙库中,一试果然能接收,特的记录下,其中的enableNotifications就是nRF的初始化方法,在
private static final UUID CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");
/**
* Enables notifications on given characteristic
*
* @return true is the request has been sent, false if one of the arguments was <code>null</code> or the characteristic does not have the CCCD.
*/
protected final boolean enableNotifications(final BluetoothGattCharacteristic characteristic) {
final BluetoothGatt gatt = mBluetoothGatt;
if (gatt == null || characteristic == null)
return false;
// Check characteristic property
final int properties = characteristic.getProperties();
if ((properties & BluetoothGattCharacteristic.PROPERTY_NOTIFY) == 0)
return false;
Log.d("BLE", "gatt.setCharacteristicNotification(" + characteristic.getUuid() + ", true)");
gatt.setCharacteristicNotification(characteristic, true);
final BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID);
if (descriptor != null) {
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
Log.v("BLE", "Enabling notifications for " + characteristic.getUuid());
Log.d("BLE", "gatt.writeDescriptor(" + CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID + ", value=0x01-00)");
return gatt.writeDescriptor(descriptor);
}
return false;
}
/**
* Enables indications on given characteristic
*
* @return true is the request has been sent, false if one of the arguments was <code>null</code> or the characteristic does not have the CCCD.
*/
protected final boolean enableIndications(final BluetoothGattCharacteristic characteristic) {
final BluetoothGatt gatt = mBluetoothGatt;
if (gatt == null || characteristic == null)
return false;
// Check characteristic property
final int properties = characteristic.getProperties();
if ((properties & BluetoothGattCharacteristic.PROPERTY_INDICATE) == 0)
return false;
Log.d("BLE", "gatt.setCharacteristicNotification(" + characteristic.getUuid() + ", true)");
gatt.setCharacteristicNotification(characteristic, true);
final BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID);
if (descriptor != null) {
descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
Log.v("BLE", "Enabling indications for " + characteristic.getUuid());
Log.d("BLE", "gatt.writeDescriptor(" + CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID + ", value=0x02-00)");
return gatt.writeDescriptor(descriptor);
}
return false;
}
为了保险,两个初始化方法都写上,这样蓝牙库在TI和Nodic下均能正常接收了!
mBluetoothGatt.setCharacteristicNotification(mRxChar,true);
//nRF 芯片必须带这个
enableNotifications(mRxChar);