Anu*_*ool 7 android bluetooth-lowenergy
我试图不断地从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;
}
}
Run Code Online (Sandbox Code Playgroud)
在成功发现我呼吁的服务时:
public void startReadingCharacteristics()
{
System.out.println("BluetoothLeService.startReadingCharacteristics");
this.mBackgroundRunnable = new BackgroundRunnableForRead();
mReadThread = new Thread(mBackgroundRunnable);
mReadThread.start();
}
Run Code Online (Sandbox Code Playgroud)
这是我的特色阅读回调 -
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);
}
}
Run Code Online (Sandbox Code Playgroud)
该应用程序在Nexus 5,Nexus 4和Motorola G上运行良好.
当我在三星S6上运行此代码时,它不起作用,onCharacteristicRead()不会被调用.
我读到,在readCharacteristics()等待onCharacteristicRead执行时,进行顺序调用会导致问题.
| 归档时间: |
|
| 查看次数: |
893 次 |
| 最近记录: |