用于测试的设备:Nexus 4,Android 4.3
连接工作正常,但onCharacteristicChanged我的回调方法永远不会被调用.但是我正在使用setCharacteristicNotification(char, true)inside 注册通知onServicesDiscovered,该函数甚至返回true.
设备日志(当通知应该出现/通过蓝牙设备发送时,实际上根本没有消息):
07-28 18:15:06.936 16777-16809/de.ffuf.leica.sketch D/BluetoothGatt: setCharacteristicNotification() - uuid: 3ab10101-f831-4395-b29d-570977d5bf94 enable: true
07-28 18:15:06.936 4372-7645/com.android.bluetooth D/BtGatt.GattService: registerForNotification() - address=C9:79:25:34:19:6C enable: true
07-28 18:15:06.936 4372-7645/com.android.bluetooth D/BtGatt.btif: btif_gattc_reg_for_notification
07-28 18:15:06.946 4372-7645/com.android.bluetooth D/BtGatt.btif: btgattc_handle_event: Event 1018
07-28 18:15:06.946 4372-7645/com.android.bluetooth D/BtGatt.GattService: onRegisterForNotifications() - address=null, status=0, registered=1, charUuid=3ab10101-f831-4395-b29d-570977d5bf94
07-28 18:15:06.946 4372-7645/com.android.bluetooth D/BtGatt.btif: btgattc_handle_event: Event 1016
07-28 18:15:06.946 4372-7645/com.android.bluetooth D/BtGatt.btif: btgattc_handle_event: Event 1018
07-28 18:15:06.946 4372-7645/com.android.bluetooth D/BtGatt.GattService: onRegisterForNotifications() - address=null, status=0, …Run Code Online (Sandbox Code Playgroud) android bluetooth bluetooth-lowenergy gatt android-4.3-jelly-bean
我正在尝试编写一个Android应用程序,模仿我编写的iOS应用程序中已存在的功能.我正在与2个不同的BLE设备连接:
在iOS上,我有两个设备都运行良好并报告数据.在Android上,我无法让它工作.经过数小时的研究和测试,我认为我要解决的基本问题是:
在iOS上,我调用以下代码以使BLE设备在有数据要报告时通知我的iOS设备:
#pragma mark - CBPeripheralDelegate Protocol methods
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error {
for (CBCharacteristic *characteristic in [service characteristics]) {
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
}
}
Run Code Online (Sandbox Code Playgroud)
而已.iOS中此方法的注释说明如下:
如果指定的特性配置为允许通知和指示,则调用此方法仅启用通知.
基于此(以及它在iOS中工作的事实),我正在计算我想要通知的特性的配置描述符应该像这样配置:
descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
gatt.writeDescriptor(descriptor);
Run Code Online (Sandbox Code Playgroud)
考虑到这一点,我的BLEDevice班级看起来像这样:
public abstract class BLEDevice {
protected BluetoothAdapter.LeScanCallback mLeScanCallback;
protected BluetoothGattCallback mBluetoothGattCallback;
protected byte[] mBytes;
protected Context mContext;
protected GotReadingCallback mGotReadingCallback;
protected String mDeviceName;
public final static UUID UUID_WEIGHT_SCALE_SERVICE
= UUID.fromString(GattAttributes.WEIGHT_SCALE_SERVICE);
public final static UUID UUID_WEIGHT_SCALE_READING_CHARACTERISTIC
= UUID.fromString(GattAttributes.WEIGHT_SCALE_READING_CHARACTERISTIC); …Run Code Online (Sandbox Code Playgroud)