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.