1

I try to connect to MiBand 2 and keeps the connection, but after a few seconds it fails and reconnect.

I scan for the available devices and display them. When I click on the device I want to connect to it connects, but after a few seconds it disconnects.

On connect Device I do this:

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

    }

And on the GattCallBack the following.

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");
            }
        };

    }

I want to keep the connection as long as possible, for hours, I mean like connecting the smart band to the phone via Bluetooth, as long as you have battery it stays connected.

Mary
  • 103
  • 2
  • 11

1 Answers1

3

There doesn't seem to be anything wrong of what you're doing, in fact, if you want to keep the connection as long as possible forgetting about handling reconnections the best strategy is to use the autoConnect parameter to true in the connectGatt method. Setting this parameter will tell Android to automatically connect to your MiBand 2 as soon as the it becomes available. If it gets disconnected, Android will connect to it under the hood for you. What's probably happening there is not up to you and it's most likely related to either the firmware of the Bluetooth device or the mobile device's Bluetooth stack. For example, the firmware itself could trigger a disconnection just to save some batteries after a few seconds if the connected Master device doesn't perform any activity.

In other words, if the Bluetooth chipset of your mobile is good enough, as well as the one of the Peripheral, and the firmware of the latter keeps a connection indeterminately, you wouldn't get any disconnection in your app.

[TIP]: If you want to achieve that purpose, I suggest to use a separate Android Service to handle all the Bluetooth stuff there. In the official documentation you have a basic example for that.

GoRoS
  • 5,183
  • 2
  • 43
  • 66
  • Thank you, but I do not understand, should my device behave like a Gatt Server? And every time I want some information from the Mi Band should I do another call? Sorry but I didn't understand from the basic example the Gatt Server part – Mary Apr 12 '19 at 18:38
  • No, in Bluetooth your app will act as a Master (client) and the Mi Band as a Peripheral (server). Once you invoke that `connectGatt` method, you just need to check the information you want. Of course, check the connection is established beforehand in case the autoConnect haven't connected to it yet. My suggestion of the Android Service will just give you more advantages not to deal with all Bluetooth classes and the Android lifecycle itself within the Activity. – GoRoS Apr 12 '19 at 18:47
  • 1
    Thank you very much, I will try this – Mary Apr 12 '19 at 20:15
  • Goros, I need more help, I didn;t succeed doing the Service ? I wrote to you on Linkedin – Mary Apr 16 '19 at 20:12
  • could you help me with further details, please? I really need more help – Mary Apr 17 '19 at 17:47
  • @Mary did you find what was the problem? – Keselme Jul 07 '20 at 11:54