Android蓝牙BLE连接在30秒后自动断开。
创始人
2024-10-08 21:01:32
0

这个问题通常是由于连接时间超时引起的。可以通过以下步骤解决这个问题:

  1. 在BluetoothGatt.connect()方法之前,使用BluetoothDevice.connectGatt()方法,设置连接超时时间:
BluetoothGatt mGatt = device.connectGatt(this, false, mGattCallback);
mGatt.connect();
mGatt.requestConnectionPriority(BluetoothGatt.CONNECTION_PRIORITY_HIGH);
mGatt.setTimeout(5000);

在这个示例中,我们将连接超时时间设置为5秒钟。

  1. 监听BLE连接状态,如果在连接时间内没有成功连接,就使用BluetoothGatt.disconnect()方法手动断开连接:
private static final int STATE_DISCONNECTED = 0;
private static final int STATE_CONNECTING = 1;
private static final int STATE_CONNECTED = 2;

private int mConnectionState = STATE_DISCONNECTED;
private BluetoothGatt mBluetoothGatt;

private final BluetoothGattCallback mGattCallback =
        new BluetoothGattCallback() {
            @Override
            public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
                if (newState == BluetoothProfile.STATE_CONNECTED) {
                    mConnectionState = STATE_CONNECTED;
                    mBluetoothGatt.discoverServices();
                } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
                    mConnectionState = STATE_DISCONNECTED;
                    mBluetoothGatt.close();
                }
            }

            @Override
            public void onServicesDiscovered(BluetoothGatt gatt, int status) {
                super.onServicesDiscovered(gatt, status);
                if (status == BluetoothGatt.GATT_SUCCESS) {
                    // service discovery successful
                } else {
                    // service discovery unsuccessful
                }
            }

            @Override
            public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
                super.onCharacteristicRead(gatt, characteristic, status);
                if (status == BluetoothGatt.GATT_SUCCESS) {
                    // characteristic read successful
                } else {
                    // characteristic read unsuccessful
                }
            }

            @Override
            public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
                super.onCharacteristicWrite(gatt, characteristic, status);
                if (status == BluetoothGatt.GATT_SUCCESS) {
                    // characteristic write successful
                } else {
                    // characteristic write unsuccessful
                }
            }

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

public void connect(String address) {
    final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
    if (device == null

相关内容

热门资讯

透视辅助!wepoker底牌透... 透视辅助!wepoker底牌透视,wpk透视是真的吗,资料教程(好像是真的挂)-哔哩哔哩1、实时wp...
透视神器!wepoker破解是... 透视神器!wepoker破解是真的还是假的(透视)开挂辅助方法(原来真的是有挂)-哔哩哔哩进入游戏-...
透视app!sohoo竞技联盟... 透视app!sohoo竞技联盟辅助器,wepoker透视脚本免费使用视频,教程书教程(本来存在有挂)...
透视胜率!we poker辅助... 透视胜率!we poker辅助器v3.3(透视)开挂辅助方法(本来存在有挂)-哔哩哔哩一、we po...
透视总结!wpk真的有透视嘛,... 透视总结!wpk真的有透视嘛,epoker透视,方案教程(原来是真的挂)-哔哩哔哩1、上手简单,内置...
透视私人局!德普之星透视(透视... 透视私人局!德普之星透视(透视)开挂辅助软件(一直真的有挂)-哔哩哔哩进入游戏-大厅左侧-新手福利-...
透视挂透视!约局吧开挂神器是真... 透视挂透视!约局吧开挂神器是真的吗,hhpoker有没有辅助辅助,法门教程(确实是真的挂)-哔哩哔哩...
透视程序!德普之星私人局辅助免... 透视程序!德普之星私人局辅助免费(透视)开挂辅助脚本(竟然真的有挂)-哔哩哔哩1、全新机制【德普之星...
透视线上!约局吧怎么看有没有挂... 透视线上!约局吧怎么看有没有挂,约局吧能不能开挂,资料教程(果然是真的挂)-哔哩哔哩1)约局吧怎么看...
透视教程!wepoker买钻石... 透视教程!wepoker买钻石有用吗(透视)开挂辅助挂(本来有挂)-哔哩哔哩暗藏猫腻,小编详细说明w...