我正在使用google ble sdk开发BLE应用, 我测试了三星s4(4.3),sony z1 compact(4.3),htc new one(4.4) 但只有三星s4只是异常
问题是调用setCharacteristicNotification(布尔启用) 如果我得到onDescriptorWrite回调,那么启动获取通知数据约5~6秒 我将获得gatt服务断开回调,我的日志正常输出9次 log将输出42次,
抱歉我的英语不好, 我只是使用我的代码和日志来帮助任何人理解我的问题setCharacteristicNotification
public boolean setCharacteristicNotification(boolean enabled){
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return false;
}
BluetoothGattService Service =
mBluetoothGatt.getService
(UUID.fromString(GattAttributes.SONOSTAR_SENSOR_UUID));
if (Service == null) {
Log.e(TAG, "service not found!");
return false;
}
BluetoothGattCharacteristic characteristic =
Service.getCharacteristic
(UUID.fromString(GattAttributes.SONOSTAR_NOTIFY_R));
final int charaProp = characteristic.getProperties();
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
UUID.fromString(GattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);
return true;
}
return false;
}
onCharacteristicChanged
@Override
public void onCharacteristicChanged(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic)
{
Log.i(TAG, "on notify" +"");
NOTIFY_DATA_ARRAY.add(characteristic.getValue());
Log.i(TAG, NOTIFY_DATA_ARRAY.size() + " "+characteristic.getValue());
}
我的日志
03-30 18:33:50.985: D/BluetoothGatt(8240): setCharacteristicNotification() - uuid: 0000ffb5-0000-1000-8000-00805f9b34fb enable: true
03-30 18:33:50.985: D/BluetoothGatt(8240): writeDescriptor() - uuid: 00002902-0000-1000-8000-00805f9b34fb
03-30 18:33:51.055: D/BluetoothGatt(8240): onDescriptorWrite() - Device=20:CD:39:90:A2:9B UUID=0000ffb5-0000-1000-8000-00805f9b34fb
03-30 18:33:51.055: I/BluetoothLeService(8240): android.bluetooth.BluetoothGatt@436e0190 gatt
03-30 18:33:51.055: I/BluetoothLeService(8240): android.bluetooth.BluetoothGattDescriptor@436f8bf0 descriptors
03-30 18:33:51.055: I/BluetoothLeService(8240): 0 status
03-30 18:33:51.055: D/BluetoothLeService(8240): Callback: Wrote GATT Descriptor successfully.
03-30 18:33:51.545: D/BluetoothGatt(8240): onNotify() - Device=20:CD:39:90:A2:9B UUID=0000ffb5-0000-1000-8000-00805f9b34fb
03-30 18:33:51.545: I/BluetoothLeService(8240): on notify
03-30 18:33:51.545: I/BluetoothLeService(8240): 1 [B@436fd2f0
03-30 18:33:52.030: D/BluetoothGatt(8240): onNotify() - Device=20:CD:39:90:A2:9B UUID=0000ffb5-0000-1000-8000-00805f9b34fb
03-30 18:33:52.035: I/BluetoothLeService(8240): on notify
03-30 18:33:52.035: I/BluetoothLeService(8240): 2 [B@43700d80
03-30 18:33:52.520: D/BluetoothGatt(8240): onNotify() - Device=20:CD:39:90:A2:9B UUID=0000ffb5-0000-1000-8000-00805f9b34fb
03-30 18:33:52.520: I/BluetoothLeService(8240): on notify
03-30 18:33:52.520: I/BluetoothLeService(8240): 3 [B@437044f8
03-30 18:33:53.055: D/BluetoothGatt(8240): onNotify() - Device=20:CD:39:90:A2:9B UUID=0000ffb5-0000-1000-8000-00805f9b34fb
03-30 18:33:53.055: I/BluetoothLeService(8240): on notify
03-30 18:33:53.055: I/BluetoothLeService(8240): 4 [B@43707d00
03-30 18:33:53.545: D/BluetoothGatt(8240): onNotify() - Device=20:CD:39:90:A2:9B UUID=0000ffb5-0000-1000-8000-00805f9b34fb
03-30 18:33:53.545: I/BluetoothLeService(8240): on notify
03-30 18:33:53.545: I/BluetoothLeService(8240): 5 [B@4370b488
03-30 18:33:54.030: D/BluetoothGatt(8240): onNotify() - Device=20:CD:39:90:A2:9B UUID=0000ffb5-0000-1000-8000-00805f9b34fb
03-30 18:33:54.030: I/BluetoothLeService(8240): on notify
03-30 18:33:54.030: I/BluetoothLeService(8240): 6 [B@4370eba0
03-30 18:33:54.520: D/BluetoothGatt(8240): onNotify() - Device=20:CD:39:90:A2:9B UUID=0000ffb5-0000-1000-8000-00805f9b34fb
03-30 18:33:54.520: I/BluetoothLeService(8240): on notify
03-30 18:33:54.520: I/BluetoothLeService(8240): 7 [B@43712178
03-30 18:33:55.055: D/BluetoothGatt(8240): onNotify() - Device=20:CD:39:90:A2:9B UUID=0000ffb5-0000-1000-8000-00805f9b34fb
03-30 18:33:55.055: I/BluetoothLeService(8240): on notify
03-30 18:33:55.055: I/BluetoothLeService(8240): 8 [B@437157b0
03-30 18:33:55.540: D/BluetoothGatt(8240): onNotify() - Device=20:CD:39:90:A2:9B UUID=0000ffb5-0000-1000-8000-00805f9b34fb
03-30 18:33:55.540: I/BluetoothLeService(8240): on notify
03-30 18:33:55.545: I/BluetoothLeService(8240): 9 [B@43718d88
03-30 18:34:20.310: D/BluetoothGatt(8240): onClientConnectionState() - status=0 clientIf=6 device=20:CD:39:90:A2:9B
03-30 18:34:20.310: I/BluetoothLeService(8240): Disconnected from GATT server.
03-30 18:34:20.320: I/HomeActivity(8240): ACTION_GATT_DISCONNECTED onReceive
03-30 18:34:20.395: E/ViewRootImpl(8240): sendUserActionEvent() mView == null
答案 0 :(得分:0)
从您的日志中定义该服务器断开连接调用。所以可能是某段时间服务器断开连接(某些时间在ble服务器中定义)。因此,如果三星设备花时间阅读通知数据设备,请将其自行断开。
答案 1 :(得分:0)
启用通知后,只需明确读取一次,如下面的三星设备代码所示。
解决
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mBluetoothGatt.readCharacteristic(characteristic);
通知将开始。当通信正在双向进行时,Tt工作但不起作用(客户端到服务器,服务器到客户端连续相同的特征)。