我在新的nexus 7(4.3 JSS15R)上使用蓝牙示例(android-sdks/samples/android-18/legacy/BluetoothLeGatt/)来测试蓝牙。而且我发现 readCharacteristic 花费了太多时间(每次读取大约 1 ~ 3 秒)。
首先,我在单击服务项时触发 readCharacteristic。
DeviceControlActivity.java
....
private final ExpandableListView.OnChildClickListener servicesListClickListner =
....
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
....
mBluetoothLeService.readCharacteristic(characteristic);
然后我在读取特征时重复 readCharacteristic。
BluetoothLeService.java
....
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
....
@Override
public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic,
int status) {
if(TEST_COUNT < 20) {
Log.d(TAG,"onCharacteristicRead");
readCharacteristic(characteristic);
TEST_COUNT ++;
} else {
TEST_COUNT = 0;
}
/*
if (status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
}
*/
}
日志显示:
10-29 11:16:37.360: DEBUG/MyTest(9554): onCharacteristicRead
10-29 11:16:39.362: DEBUG/MyTest(9554): onCharacteristicRead
10-29 11:16:41.364: DEBUG/MyTest(9554): onCharacteristicRead
10-29 11:16:43.356: DEBUG/MyTest(9554): onCharacteristicRead
10-29 11:16:45.358: DEBUG/MyTest(9554): onCharacteristicRead
10-29 11:16:47.340: DEBUG/MyTest(9554): onCharacteristicRead
10-29 11:16:49.342: DEBUG/MyTest(9554): onCharacteristicRead
10-29 11:16:51.334: DEBUG/MyTest(9554): onCharacteristicRead
10-29 11:16:53.336: DEBUG/MyTest(9554): onCharacteristicRead
10-29 11:16:55.328: DEBUG/MyTest(9554): onCharacteristicRead
10-29 11:16:57.319: DEBUG/MyTest(9554): onCharacteristicRead
10-29 11:16:59.311: DEBUG/MyTest(9554): onCharacteristicRead
10-29 11:17:01.313: DEBUG/MyTest(9554): onCharacteristicRead
10-29 11:17:03.305: DEBUG/MyTest(9554): onCharacteristicRead
10-29 11:17:05.297: DEBUG/MyTest(9554): onCharacteristicRead
10-29 11:17:07.289: DEBUG/MyTest(9554): onCharacteristicRead
10-29 11:17:09.291: DEBUG/MyTest(9554): onCharacteristicRead
10-29 11:17:11.283: DEBUG/MyTest(9554): onCharacteristicRead
10-29 11:17:13.285: DEBUG/MyTest(9554): onCharacteristicRead
10-29 11:17:15.277: DEBUG/MyTest(9554): onCharacteristicRead
接收每个特征读取需要 2 秒,真的很奇怪。
我使用三星 ble sdk 和相同的 ble 设备在 Galaxy s4 上测试了相同的行为。但是日志显示它花费更少的时间阅读。
10-29 11:51:19.393 I/MyTest﹕ onCharacteristicRead
10-29 11:51:19.493 I/MyTest﹕ onCharacteristicRead
10-29 11:51:19.588 I/MyTest﹕ onCharacteristicRead
10-29 11:51:19.688 I/MyTest﹕ onCharacteristicRead
10-29 11:51:19.783 I/MyTest﹕ onCharacteristicRead
10-29 11:51:19.878 I/MyTest﹕ onCharacteristicRead
10-29 11:51:19.978 I/MyTest﹕ onCharacteristicRead
10-29 11:51:20.078 I/MyTest﹕ onCharacteristicRead
10-29 11:51:20.183 I/MyTest﹕ onCharacteristicRead
10-29 11:51:20.323 I/MyTest﹕ onCharacteristicRead
10-29 11:51:20.418 I/MyTest﹕ onCharacteristicRead
10-29 11:51:20.518 I/MyTest﹕ onCharacteristicRead
10-29 11:51:20.613 I/MyTest﹕ onCharacteristicRead
10-29 11:51:20.713 I/MyTest﹕ onCharacteristicRead
10-29 11:51:20.808 I/MyTest﹕ onCharacteristicRead
10-29 11:51:20.908 I/MyTest﹕ onCharacteristicRead
10-29 11:51:21.003 I/MyTest﹕ onCharacteristicRead
10-29 11:51:21.103 I/MyTest﹕ onCharacteristicRead
10-29 11:51:21.248 I/MyTest﹕ onCharacteristicRead
10-29 11:51:21.343 I/MyTest﹕ onCharacteristicRead
10-29 11:51:21.443 I/MyTest﹕ onCharacteristicRead
有人遇到同样的问题吗?它是由新的nexus 7硬件引起的吗?还是安卓 4.3 sdk?
我应该怎么做才能减少花在 readCharacteristic 上的时间?