une*_*red 3 android bluetooth-lowenergy
我的应用程序在连接到某些设备上的 BluetoothGatt 时出现问题。
所有与 BLE 相关的代码都位于 android 中Service,除华为 CAM-L03外,一切正常。据报道,一些旧的三星 Galaxy 设备也存在问题。
我最近发现,在某些设备上,所有 BLE 通信都必须在 UI 线程上执行。我写了一个快速测试应用程序,当一切都在 UI 线程上运行时,华为的问题就消失了。
我试着创建Handler在Service与主尺蠖:
final Context context = getApplicationContext();
Handler handler = new Handler(context.getMainLooper());
handler.post(new Runnable(){
@Override
public void run() {
BluetoothGatt gatt = device.connectGatt(context, false, callBackHandler);
…
}
});
Run Code Online (Sandbox Code Playgroud)
但这仍然在连接回调中给了我 GATT_ERROR 133,我有点怀疑,因为上下文是服务,而不是 UI 应用程序。
我在这里只看到两个选项:
我不满足于其中任何一个。还有其他我不知道的选择吗?
我想出了问题究竟是什么以及如何解决它。我将我的答案添加为对连接尝试时抛出的通用 gatt 错误 (133) 的另一项检查,因为我没有找到暗示同一问题的答案。
I have been led to a wrong conclusion by answers like this. I think that bug was long fixed on Android. The fact that I was getting connection failed callback is an indication that the thread issue was irrelevant since the callbacks were registered.
The problem I was facing was related to the fact that I kept the bluetooth adapter scanning while attempting to connect to the BLE device. It appears that some phones have a problem with that.
The requirements of my application dictate continuous scanning for the time the service is running and all the test phones I had around didn't have problems with it. This model of HUAWEI though, would refuse to connect.
TL;DR Stop scanning before attempting to connect, restart scanning if required after disconnect.
| 归档时间: |
|
| 查看次数: |
3394 次 |
| 最近记录: |