Wun*_*Wun 13 android characteristics bluetooth-lowenergy
我在Android中开发BLE,我可以扫描,连接和写入BLE设备的特性.
我调用下面的函数传递BluetoothGatt,并characteristic于AsyncTask当点击Button.
write_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new WriteCharacteristic(mBluetoothGatt , HueCharacteristic).execute();
}
});
Run Code Online (Sandbox Code Playgroud)
写特性的代码如下:
private class WriteCharacteristic extends AsyncTask<String, Void, String> {
public BluetoothGatt mGatt;
public BluetoothGattCharacteristic mCharacteristic;
public WriteCharacteristic(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic){
mGatt = gatt;
mCharacteristic = characteristic;
}
@Override
protected String doInBackground(String... urls) {
mGatt.writeCharacteristic(mCharacteristic);
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
但我尝试点击该按钮连续,看来Android的没有每次写入characteristic到BLE装置.
如果我连续点击按钮5次,则会丢失1~3次.它只能写characteristic到BLE装置两次.
题:
Is there any better way to write characteristic consecutive and stable to BLE device for Android?
Dev*_*red 20
Android蓝牙堆栈中的读/写特性系统不擅长排队多个操作.在发送另一个操作之前,您需要等待操作完成.此外,由于您的代码正在使用AsyncTask,您将在某些设备上并行执行任务,因此,当您反复按下按钮时,即使请求也不会被序列化.
要从框架中获得稳定的结果,您需要自己排队这些请求并等待BluetoothGattCallback onCharacteristicWrite()在发送下一个命令之前触发.您的代码需要同步对GATT对象的所有访问,以便下一个writeCharacteristic()永远不会到达完成回调为前一个请求触发.
| 归档时间: |
|
| 查看次数: |
4815 次 |
| 最近记录: |