Я пытаюсь подключиться к 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");
}
};
}
Я хочу поддерживать соединение как можно дольше, в течение нескольких часов, я имею в виду, как подключить смарт-браслет к телефону через Bluetooth, пока у вас есть батарея, он остается подключенным.