Ble connection retry

This commit is contained in:
Vineyro 2023-11-21 16:22:38 +07:00
parent fb616a9f29
commit 824e6f0064
2 changed files with 218 additions and 188 deletions

View File

@ -13,8 +13,8 @@ android {
applicationId "llc.arma.ble" applicationId "llc.arma.ble"
minSdk 26 minSdk 26
targetSdk 33 targetSdk 33
versionCode 12 versionCode 13
versionName "1.2.12" versionName "1.2.13"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables { vectorDrawables {

View File

@ -1022,7 +1022,12 @@ class BleRepositoryImpl @Inject constructor(
device: BluetoothDevice, device: BluetoothDevice,
serviceId: UUID, serviceId: UUID,
characteristicId: UUID characteristicId: UUID
): Result<ByteArray, BleException> = suspendCancellableCoroutine { ): Result<ByteArray, BleException> {
var result: Result<ByteArray, BleException> = Result.failure(BleException.UnexpectedResponse)
repeat(5){
result = suspendCancellableCoroutine {
var result: ByteArray? var result: ByteArray?
@ -1162,12 +1167,27 @@ class BleRepositoryImpl @Inject constructor(
} }
if(result.isSuccess || (result.getErrorOrNull() is BleException.UnexpectedResponse).not()){
return result
}
}
return result
}
private suspend fun writeCharacteristic( private suspend fun writeCharacteristic(
device: BluetoothDevice, device: BluetoothDevice,
serviceId: UUID, serviceId: UUID,
characteristicId: UUID, characteristicId: UUID,
writeData: ByteArray writeData: ByteArray
): Result<Unit, BleException> = suspendCancellableCoroutine { ): Result<Unit, BleException> {
var result: Result<Unit, BleException> = Result.failure(BleException.UnexpectedResponse)
repeat(5){
result = suspendCancellableCoroutine {
var bleGatt: BluetoothGatt? = null var bleGatt: BluetoothGatt? = null
@ -1295,6 +1315,16 @@ class BleRepositoryImpl @Inject constructor(
} }
if(result.isSuccess || (result.getErrorOrNull() is BleException.UnexpectedResponse).not()){
return result
}
}
return result
}
fun checkPermission(): Boolean { fun checkPermission(): Boolean {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {