From b8a09183daf40346339b1fd19f651807b3e21d95 Mon Sep 17 00:00:00 2001 From: miyeongkim Date: Mon, 20 Feb 2023 11:33:20 +0900 Subject: [PATCH] add bluetooth bond state check --- .../pump/diaconn/service/BLECommonService.kt | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pump/diaconn/src/main/java/info/nightscout/pump/diaconn/service/BLECommonService.kt b/pump/diaconn/src/main/java/info/nightscout/pump/diaconn/service/BLECommonService.kt index 102b456358..3664129047 100644 --- a/pump/diaconn/src/main/java/info/nightscout/pump/diaconn/service/BLECommonService.kt +++ b/pump/diaconn/src/main/java/info/nightscout/pump/diaconn/service/BLECommonService.kt @@ -3,6 +3,7 @@ package info.nightscout.pump.diaconn.service import android.Manifest import android.annotation.SuppressLint import android.bluetooth.BluetoothAdapter +import android.bluetooth.BluetoothDevice import android.bluetooth.BluetoothGatt import android.bluetooth.BluetoothGattCallback import android.bluetooth.BluetoothGattCharacteristic @@ -117,6 +118,15 @@ class BLECommonService @Inject internal constructor( aapsLogger.error("Device not found. Unable to connect from: $from") return false } + + if (device.bondState == BluetoothDevice.BOND_NONE) { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S || ActivityCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_CONNECT) == PackageManager.PERMISSION_GRANTED) { + device.createBond() + SystemClock.sleep(10000) + } + return false + } + aapsLogger.debug(LTag.PUMPBTCOMM, "Trying to create a new connection from: $from") connectDeviceName = device.name bluetoothGatt = device.connectGatt(context, false, mGattCallback)