【发布时间】:2016-04-15 22:44:11
【问题描述】:
我正在尝试连续读取 BLE 设备的特征。
我在我的服务类中创建了一个 Runnable:
private class BackgroundRunnableForRead implements Runnable
{
private volatile boolean isRunning = true ;
@Override
public void run() {
try {
BluetoothLeService.this.backgroundRunID = Thread.currentThread().getId();
while( isRunning) {
List<BluetoothGattService> gattServices = BluetoothLeService.this.getSupportedGattServices();
if (gattServices != null && gattServices.size() > 0) {
BluetoothGattCharacteristic characteristic = getCharacteristic(gattServices);
if (characteristic != null && (characteristic.getProperties() & 2) > 0) {
BluetoothLeService.this.readCharacteristic(characteristic);
}
}
}
}
catch(Exception e)
{
isRunning= false;
e.printStackTrace();
}
}
public void kill()
{
this.isRunning = false;
}
}
成功发现我正在调用的服务:
public void startReadingCharacteristics()
{
System.out.println("BluetoothLeService.startReadingCharacteristics");
this.mBackgroundRunnable = new BackgroundRunnableForRead();
mReadThread = new Thread(mBackgroundRunnable);
mReadThread.start();
}
这是我的特征读取回调 -
public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic,
int status) {
System.out.println("BluetoothLeService.onCharacteristicRead" + status);
if (status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
}
}
该应用程序在 Nexus 5、Nexus 4 和 Motorola G 上运行良好。
当我在三星 S6 上运行此代码时,它不起作用,onCharacteristicRead() 未被调用。
我了解到,对 readCharacteristics() 进行顺序调用可能会导致问题,因为它会等待 onCharacteristicRead 执行。
【问题讨论】:
-
什么是操作系统版本?
-
操作系统版本 - Android 5.1.1。
-
请格式化您的代码
标签: android bluetooth-lowenergy