Android:蓝牙GATT连接延迟

时间:2019-11-28 18:11:36

标签: bluetooth-lowenergy bluetooth-gatt

我正在使用一个Android应用程序,该应用程序已将蓝牙LE连接到从(自制)设备。 我使用以下代码作为基础: https://github.com/android/connectivity-samples/tree/master/BluetoothLeGatt/Application/src/main/java/com/example/android/bluetoothlegatt 作为服务运行的Bluetooth LE,在活动“ onCreate”中绑定到该服务。如果连接状态已更改,则活动将通过BroadcastReceiver接收它,如果状态为“已连接”,则启用按钮以将消息发送到从属设备,这是BroadcastReceiver:

if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) {
            Log.d(TAG, "mGattUpdateReceiver onReceive: Gatt connected");
            startButton.setEnabled(true);
            mConnected = true;
            updateConnectionState(R.string.connected);
            invalidateOptionsMenu();
            //Toast.makeText(context, "Device is successfully connected!", Toast.LENGTH_SHORT).show();
            Snackbar snackbar = Snackbar.make(rootLayout, "Device is successfully connected!", Snackbar.LENGTH_SHORT);
            snackbar.show();

如果我立即单击该按钮,我会收到一个错误消息,因为找不到BLE服务,请参阅日志,时间戳记,07:31:05.927:

2015-01-21 07:30:59.959 D/ViewRootImpl@d25d1dc[DeviceControlActivity]: ViewPostImeInputStage processPointer 0
2015-01-21 07:31:00.038 D/ViewRootImpl@d25d1dc[DeviceControlActivity]: ViewPostImeInputStage processPointer 1
2015-01-21 07:31:00.045 D/BluetoothLeService: Trying to use an existing mBluetoothGatt for connection.
2015-01-21 07:31:00.045 D/BluetoothAdapter: isSecureModeEnabled
2015-01-21 07:31:00.056 D/DeviceControlActivity: onOptionsItemSelected: Connect to 00:15:87:20:B7:76
2015-01-21 07:31:04.023 D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=7 device=00:15:87:20:B7:XX
2015-01-21 07:31:04.058 I/BluetoothLeService: Connected to GATT server.
2015-01-21 07:31:04.058 D/BluetoothGatt: discoverServices() - device: 00:15:87:20:B7:XX
2015-01-21 07:31:04.061 I/BluetoothLeService: Attempting to start service discovery:true
2015-01-21 07:31:04.064 D/DeviceControlActivity: mGattUpdateReceiver onReceive: Gatt connected
2015-01-21 07:31:04.086 D/TextView: setTypeface with style : 0
2015-01-21 07:31:05.760 D/ViewRootImpl@d25d1dc[DeviceControlActivity]: ViewPostImeInputStage processPointer 0
2015-01-21 07:31:05.763 D/DeviceControlActivity: Button tapped: 2131296315
2015-01-21 07:31:05.893 D/ViewRootImpl@d25d1dc[DeviceControlActivity]: ViewPostImeInputStage processPointer 1
2015-01-21 07:31:05.895 D/DeviceControlActivity: Button released: 2131296315
2015-01-21 07:31:05.927 W/BluetoothLeService: writeCustomCharacteristic: Custom BLE Service not found
2015-01-21 07:31:06.801 D/BluetoothGatt: onClientConnParamsChanged() - Device=00:15:87:20:B7:XX interval=16 status=0
2015-01-21 07:31:07.346 D/BluetoothGatt: onClientConnParamsChanged() - Device=00:15:87:20:B7:XX interval=6 status=0
2015-01-21 07:31:07.904 D/BluetoothGatt: onSearchComplete() = Device=00:15:87:20:B7:XX Status=0
2015-01-21 07:31:08.083 D/BluetoothGatt: onClientConnParamsChanged() - Device=00:15:87:20:B7:XX interval=16 status=0

如您先前所见,有一个logcat条目,连接GATT的时间戳为07:31:04.064。 我还注意到,如果我等一下,直到最后四个BluetoothGatt日志“ onClientConnParamsChanged()”和“ onSearchComplete()”出现,只要我单击按钮,“ writeCustumCharacteristic”都会成功完成。

您知道为什么需要4秒钟来建立连接吗?或者,如果确实建立了连接,那么在这些之后如何获得通知?

谢谢您的帮助!

0 个答案:

没有答案
相关问题