为什么 BluetoothGattCallback 会在几秒钟后不断断开连接?

Why BluetoothGattCallback keeps disconecting after a few seconds?

我尝试连接到 MiBand 2 并保持连接,但几秒钟后连接失败并重新连接。

我扫描可用的设备并显示它们。当我点击我想连接的设备时,它会连接,但几秒钟后就会断开连接。

在连接设备时我这样做:

private void connectDevice(BluetoothDevice itemAtPosition) {
            itemAtPosition.createBond();
            Log.i("BOND","Created with device");
        bluetoothGatt = itemAtPosition.connectGatt(getApplicationContext(), true, miBandGattCallBack);

    }

并在 GattCallBack 上执行以下操作。

miBandGattCallBack = new BluetoothGattCallback() {
            @Override
            public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
                switch (newState) {
                    case BluetoothGatt.STATE_DISCONNECTED:
                        Log.d("Info", "Device disconnected");

                        break;
                    case BluetoothGatt.STATE_CONNECTED: {
                        Log.i("Infooo", "Connected with device");
                        Log.i("Infooo", "Discovering services");
                        gatt.discoverServices();
                    }
                    break;
                }
            }

            @Override
            public void onServicesDiscovered(BluetoothGatt gatt, int status) {

                if (!sharedPreferences.getBoolean("isAuthenticated", false)) {
                    authoriseMiBand();
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putBoolean("isAuthenticated", true);
                    editor.apply();
                } else
                    Log.i("Device", "Already authenticated");
            }

            @Override
            public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {

            }

            @Override
            public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
                super.onCharacteristicWrite(gatt, characteristic, status);
            }

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

            }

            @Override
            public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
                Log.d("Descriptor", descriptor.getUuid().toString() + " Read");
            }

            @Override
            public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
                Log.d("Descriptor", descriptor.getUuid().toString() + " Written");
            }
        };

    }

我想尽可能长时间地保持连接,几个小时,我的意思是像通过蓝牙将智能手环连接到 phone,只要你有电池它就会保持连接。

你所做的似乎没有任何问题,事实上,如果你想尽可能长时间地保持连接而忘记处理重新连接,最好的策略是使用 autoConnect 参数到 connectGatt 方法中的 true。设置此参数将告诉 Android 在可用时立即自动连接到您的 MiBand 2。如果断开连接,Android 会在后台为您连接到它。那里可能发生的事情不取决于您,它很可能与蓝牙设备的固件或移动设备的蓝牙堆栈有关。例如,如果连接的主设备不执行任何 activity.

,固件本身可能会在几秒钟后触发断开连接以节省一些电池

换句话说,如果你手机的蓝牙芯片组足够好,还有一个外围设备,并且后者的固件不确定地保持连接,你的应用程序就不会断开连接.

[提示]:如果您想实现该目的,我建议使用单独的 Android 服务来处理那里的所有蓝牙内容。在官方文档中,您有一个 basic example