如何防止BluetoothGattCallback一次执行多次 [英] How to prevent BluetoothGattCallback from being executed multiple times at a time

查看:507
本文介绍了如何防止BluetoothGattCallback一次执行多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有 BluetoothGattCallback

public class MyService extends Service {

    private BluetoothGattCallback callback;

    @Override
    public void onCreate() {
            super.onCreate();

            callback = new BluetoothGattCallback() {
                      @Override
                      public synchronized void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
                              Log.i("onConnectionStateChanged", "Status " + status);                
                              Log.i("onConnectionStateChanged", "New State " + newState);                
                      }
            };
    }

    // registration of bluetooth adapter and blah blah blah


}

当我启动应用程序时,它可以正常工作,并且回调仅被调用一次,但是经过几次尝试,它却被调用了两次。

When I start the app, it works fine and the callback only gets called once, but after a couple of tries, it gets called twice.

示例日志

10-22 13:29:18.731 26944-26961/redacted.lollipop I/onConnectionStateChange: Status 0
10-22 13:29:18.731 26944-26961/redacted.lollipop I/onConnectionStateChange: New State 2
10-22 13:29:18.731 26944-26961/redacted.lollipop I/onConnectionStateChange: Status 0
10-22 13:29:18.731 26944-26961/redacted.lollipop I/onConnectionStateChange: New State 2

更多示例日志

10-22 13:29:48.836 26944-26961/redacted.lollipop I/onConnectionStateChange: Status 8
10-22 13:29:48.836 26944-26961/redacted.lollipop I/onConnectionStateChange: New State 0
10-22 13:29:48.850 26944-30763/redacted.lollipop I/onConnectionStateChange: Status 8
10-22 13:29:48.850 26944-30763/redacted.lollipop I/onConnectionStateChange: New State 0

应用保持活动状态的时间越长,它的调用次数就越多。如何防止这种情况发生?

And it gets called a lot more times the longer the app stays active. How do I prevent this?

推荐答案

要记住的一件事是每次致电

One thing to keep in mind is that each time you call

bluetoothDevice.connectGatt(context, true, callback);

它将创建bluetoothGatt对象的新实例。 查看该源代码,您将看到:

It creates a new instance of the bluetoothGatt object. check out the source for this one you will see:

         BluetoothGatt gatt = new BluetoothGatt(context, iGatt, this, transport);
         gatt.connect(autoConnect, callback);

所以一件棘手的事情是,如果您的设备断开连接并重新连接到它。 connectGatt(上下文,true,回调);而不是在先前的bluetoothGatt实例上调用connect(),您将获得2个都可以处理gatt回调的bluetoothGatt实例。

So one tricky thing is that if your device disconnects and you re-connect to it with. connectGatt(context, true, callback); instead of calling connect() on the previous bluetoothGatt instance you will get 2 bluetoothGatt instances that both have a handle to your gatt callback.

最初,我试图解决此问题通过重新连接之前尝试关闭并断开bluetoothGatt。

Initially I was trying to fix the problem by trying to close and disconnect the bluetoothGatt before reconnecting.

   if (service.bluetoothGatt!=null){
        Log.i("Rides","Closeing bluetooth gatt on disconnect");
        service.bluetoothGatt.close();
        service.bluetoothGatt.disconnect();
        service.bluetoothGatt=null;
    } 

但这不能很好地工作,因此我将获得多个onConnectionStateChanged回调。

But this did not work well, somehow I would get multiple onConnectionStateChanged callbacks.

我能够通过检查是否有有效的bluetoothGatt对象并确保在重新连接时对其调用connect()来解决此问题。

I was able to resolve this problem by checking if I has a valid bluetoothGatt object and making sure to call connect() on it if its a reconnection.

----更新后的答案----

我发现更好在 onConnectionStateChanged 回调。当您发出断开连接时,它会向蓝牙设备发送一条消息,要求断开连接。然后,一旦响应,您将获得回调并关闭蓝牙gatt连接。通过等待回调并在完全关闭之前不打开另一个gatt连接,这似乎可以防止多个gatt对象连接到应用程序。

I have found that its better to call bluetoothGatt.close() inside the onConnectionStateChanged callback. When you issue a disconnect it sends a message to the bluetooth device to request disconnect. Then once it responds you get the callback and close the bluetooth gatt connection. By waiting for the callback and not opening another gatt connection until its fully closed it seems to prevent multiple gatt objects from getting connected to the app.

这篇关于如何防止BluetoothGattCallback一次执行多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆