简体   繁体   English

Android BLE连接到GATT服务所需的时间比预期的长

[英]Android BLE connecting to GATT service takes longer than expected

I am new to android BLE development and to BLE development in general and I noticed that most of the time, Android might take 3-7 seconds before the call to onConnectionStateChange gets triggered after my connectGatt call. 我是android BLE开发和BLE开发的新手,我注意到大多数时候,在connectGatt调用之后,触发onConnectionStateChange的调用之前,Android可能需要3到7秒的时间。 Is this normal? 这正常吗? I am curious because I worked with Bluetooth 2.0 previously and everything was much faster there. 我很好奇,因为我以前使用过蓝牙2.0,并且那里的一切都快得多。 Also, I did some test coding on iPhone and the establishment of initial connection was much faster there as well. 另外,我在iPhone上进行了一些测试编码,并且初始连接的建立也快得多。 I will post a sample code below along with the few android monitor messages indicating where the slowdown occurs. 我将在下面发布示例代码以及一些指示减速发生位置的Android监视器消息。

class BluetoothConnection{


    void connectToDevice(BluetoothDevice device) {
        if (mBluetoothGatt == null) {
            Log.d(TAG, "-----Trying to connect to GATT server.");
            mBluetoothGatt = device.connectGatt(mContext, false, mGattCallback);
        }
    }


    private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {

        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            if (newState == BluetoothProfile.STATE_CONNECTED) {
                Log.d(TAG, "-----Connected to GATT server.");
                Log.d(TAG, "-----Attempting to start service discovery:" +
                        mBluetoothGatt.discoverServices());

            } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
                Log.d(TAG, "-----Disconnected from GATT server.");
            }
        }


        @Override
        // New services discovered
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            if (status == BluetoothGatt.GATT_SUCCESS) {
                Log.w(TAG, "-----Successfully discovered the services");

                BluetoothGattService gattService = mBluetoothGatt.getService(UUID.fromString("0000ffe0-0000-1000-8000-00805f9b34fb"));

                Log.d(TAG, "-- Service = " + gattService.getUuid());
                characteristicsTxRx  = gattService.getCharacteristic(UUID.fromString("0000ffe1-0000-1000-8000-00805f9b34fb"));

                Log.d(TAG, "-- Characteristic = " + characteristicsTxRx.getUuid());
                mBluetoothGatt.setCharacteristicNotification(characteristicsTxRx, true);

                Intent intent = new Intent(CONNECTION_ESTABLISHED);
                mContext.sendBroadcast(intent);
            }
        }


        @Override
        public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
            super.onCharacteristicChanged(gatt, characteristic);

            final byte[] data = characteristic.getValue();
            if (data != null && data.length > 0) {
                Intent intent = new Intent(DATA_AVAILABLE);
                String incomingMessage = new String(data);
                intent.putExtra(DATA, incomingMessage);
                mContext.sendBroadcast(intent);
            }
        }
    };
}

And here are couple lines from my log, as you can see it took almost 5 seconds here to get confirmation that we are connected to GATT. 这是我日志中的几行,您可以看到这里花了将近5秒钟来确认我们已连接到GATT。

09-07 10:07:05.211 29907-29907/com.bubblewall.saik.bubblewall D/bubbleWallMessage: -----Trying to connect to GATT server.
09-07 10:07:09.898 29907-29920/com.bubblewall.saik.bubblewall D/bubbleWallMessage: -----Connected to GATT server.
09-07 10:07:09.901 29907-29920/com.bubblewall.saik.bubblewall D/bubbleWallMessage: -----Attempting to start service discovery:true
09-07 10:07:10.347 29907-29920/com.bubblewall.saik.bubblewall W/bubbleWallMessage: -----Successfully discovered the services

For anyone who might be interested in the solution that worked for me. 对于任何可能对适用于我的解决方案感兴趣的人。 I could not find any Android documentation that suggested any particular advertising intervals, but the IOs documentation suggested to use an interval of 152.5 ms. 我找不到任何建议任何特定广告间隔的Android文档,但IOs文档建议使用152.5 ms的间隔。 After setting my interval to this, my app started discovering and connecting much faster to the Bluetooth. 将时间间隔设置为此时间后,我的应用程序开始发现并更快地连接到蓝牙。 Thank you, Emil, for pointing me in the right direction. 谢谢您,埃米尔(Emil)为我指明了正确的方向。

Here is the IOs documentation page that I mentioned above. 这是我上面提到的IOs文档页面。 https://developer.apple.com/library/content/qa/qa1931/_index.html https://developer.apple.com/library/content/qa/qa1931/_index.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM