Ble connection retry
This commit is contained in:
parent
fb616a9f29
commit
824e6f0064
|
|
@ -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 {
|
||||||
|
|
|
||||||
|
|
@ -1022,143 +1022,158 @@ 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: ByteArray?
|
var result: Result<ByteArray, BleException> = Result.failure(BleException.UnexpectedResponse)
|
||||||
|
|
||||||
val callback = object : BluetoothGattCallback() {
|
repeat(5){
|
||||||
|
result = suspendCancellableCoroutine {
|
||||||
|
|
||||||
override fun onConnectionStateChange(
|
var result: ByteArray?
|
||||||
gatt: BluetoothGatt,
|
|
||||||
status: Int,
|
|
||||||
newState: Int
|
|
||||||
) {
|
|
||||||
|
|
||||||
if(status == BluetoothGatt.GATT_SUCCESS) {
|
val callback = object : BluetoothGattCallback() {
|
||||||
|
|
||||||
if (newState == BluetoothProfile.STATE_CONNECTED) {
|
override fun onConnectionStateChange(
|
||||||
|
gatt: BluetoothGatt,
|
||||||
|
status: Int,
|
||||||
|
newState: Int
|
||||||
|
) {
|
||||||
|
|
||||||
if (checkPermission()) {
|
if(status == BluetoothGatt.GATT_SUCCESS) {
|
||||||
|
|
||||||
gatt.discoverServices()
|
if (newState == BluetoothProfile.STATE_CONNECTED) {
|
||||||
|
|
||||||
|
if (checkPermission()) {
|
||||||
|
|
||||||
|
gatt.discoverServices()
|
||||||
|
|
||||||
|
} else {
|
||||||
|
it.resume(Result.failure(BleException.PermissionDenied))
|
||||||
|
gatt.disconnect()
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
gatt.close()
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
it.resume(Result.failure(BleException.PermissionDenied))
|
it.resume(Result.failure(BleException.PermissionDenied))
|
||||||
gatt.disconnect()
|
gatt.disconnect()
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onServicesDiscovered(
|
||||||
|
gatt: BluetoothGatt,
|
||||||
|
status: Int
|
||||||
|
) {
|
||||||
|
super.onServicesDiscovered(gatt, status)
|
||||||
|
|
||||||
|
if (status == BluetoothGatt.GATT_SUCCESS) {
|
||||||
|
|
||||||
|
gatt.services?.firstOrNull { service ->
|
||||||
|
service.uuid == serviceId
|
||||||
|
}?.characteristics?.firstOrNull { characteristic ->
|
||||||
|
characteristic.uuid == characteristicId
|
||||||
|
}?.let { char ->
|
||||||
|
|
||||||
|
if (checkPermission()) {
|
||||||
|
|
||||||
|
gatt.readCharacteristic(char)
|
||||||
|
|
||||||
|
} else {
|
||||||
|
|
||||||
|
gatt.disconnect()
|
||||||
|
it.resume(Result.failure(BleException.PermissionDenied))
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
gatt.disconnect()
|
||||||
|
it.resume(Result.failure(BleException.UnexpectedResponse))
|
||||||
|
|
||||||
|
} else {
|
||||||
|
|
||||||
|
gatt.disconnect()
|
||||||
|
it.resume(Result.failure(BleException.UnexpectedResponse))
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onCharacteristicRead(
|
||||||
|
gatt: BluetoothGatt,
|
||||||
|
characteristic: BluetoothGattCharacteristic,
|
||||||
|
status: Int
|
||||||
|
) {
|
||||||
|
|
||||||
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
|
||||||
|
super.onCharacteristicRead(gatt, characteristic, status)
|
||||||
|
onCommonCharacteristicRead(gatt, characteristic, characteristic.value, status)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onCharacteristicRead(
|
||||||
|
gatt: BluetoothGatt,
|
||||||
|
characteristic: BluetoothGattCharacteristic,
|
||||||
|
value: ByteArray,
|
||||||
|
status: Int
|
||||||
|
) {
|
||||||
|
super.onCharacteristicRead(gatt, characteristic, value, status)
|
||||||
|
onCommonCharacteristicRead(gatt, characteristic, value, status)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onCommonCharacteristicRead(
|
||||||
|
gatt: BluetoothGatt,
|
||||||
|
characteristic: BluetoothGattCharacteristic,
|
||||||
|
value: ByteArray,
|
||||||
|
status: Int
|
||||||
|
) {
|
||||||
|
|
||||||
|
Log.d("read", "onCharacteristicRead $status")
|
||||||
|
|
||||||
|
if(status == BluetoothGatt.GATT_SUCCESS) {
|
||||||
|
|
||||||
|
if (checkPermission()) {
|
||||||
|
result = value
|
||||||
|
it.resume(Result.success(result!!))
|
||||||
|
|
||||||
|
} else {
|
||||||
|
it.resume(Result.failure(BleException.PermissionDenied))
|
||||||
|
|
||||||
|
}
|
||||||
|
gatt.disconnect()
|
||||||
|
} else {
|
||||||
|
|
||||||
|
it.resume(Result.failure(BleException.UnexpectedResponse))
|
||||||
|
gatt.disconnect()
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
|
||||||
gatt.close()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
}
|
||||||
|
|
||||||
|
if (checkPermission()) {
|
||||||
|
device.connectGatt(app, false, callback)
|
||||||
|
} else {
|
||||||
it.resume(Result.failure(BleException.PermissionDenied))
|
it.resume(Result.failure(BleException.PermissionDenied))
|
||||||
gatt.disconnect()
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onServicesDiscovered(
|
if(result.isSuccess || (result.getErrorOrNull() is BleException.UnexpectedResponse).not()){
|
||||||
gatt: BluetoothGatt,
|
return result
|
||||||
status: Int
|
|
||||||
) {
|
|
||||||
super.onServicesDiscovered(gatt, status)
|
|
||||||
|
|
||||||
if (status == BluetoothGatt.GATT_SUCCESS) {
|
|
||||||
|
|
||||||
gatt.services?.firstOrNull { service ->
|
|
||||||
service.uuid == serviceId
|
|
||||||
}?.characteristics?.firstOrNull { characteristic ->
|
|
||||||
characteristic.uuid == characteristicId
|
|
||||||
}?.let { char ->
|
|
||||||
|
|
||||||
if (checkPermission()) {
|
|
||||||
|
|
||||||
gatt.readCharacteristic(char)
|
|
||||||
|
|
||||||
} else {
|
|
||||||
|
|
||||||
gatt.disconnect()
|
|
||||||
it.resume(Result.failure(BleException.PermissionDenied))
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
gatt.disconnect()
|
|
||||||
it.resume(Result.failure(BleException.UnexpectedResponse))
|
|
||||||
|
|
||||||
} else {
|
|
||||||
|
|
||||||
gatt.disconnect()
|
|
||||||
it.resume(Result.failure(BleException.UnexpectedResponse))
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onCharacteristicRead(
|
|
||||||
gatt: BluetoothGatt,
|
|
||||||
characteristic: BluetoothGattCharacteristic,
|
|
||||||
status: Int
|
|
||||||
) {
|
|
||||||
|
|
||||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
|
|
||||||
super.onCharacteristicRead(gatt, characteristic, status)
|
|
||||||
onCommonCharacteristicRead(gatt, characteristic, characteristic.value, status)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onCharacteristicRead(
|
|
||||||
gatt: BluetoothGatt,
|
|
||||||
characteristic: BluetoothGattCharacteristic,
|
|
||||||
value: ByteArray,
|
|
||||||
status: Int
|
|
||||||
) {
|
|
||||||
super.onCharacteristicRead(gatt, characteristic, value, status)
|
|
||||||
onCommonCharacteristicRead(gatt, characteristic, value, status)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun onCommonCharacteristicRead(
|
|
||||||
gatt: BluetoothGatt,
|
|
||||||
characteristic: BluetoothGattCharacteristic,
|
|
||||||
value: ByteArray,
|
|
||||||
status: Int
|
|
||||||
) {
|
|
||||||
|
|
||||||
Log.d("read", "onCharacteristicRead $status")
|
|
||||||
|
|
||||||
if(status == BluetoothGatt.GATT_SUCCESS) {
|
|
||||||
|
|
||||||
if (checkPermission()) {
|
|
||||||
result = value
|
|
||||||
it.resume(Result.success(result!!))
|
|
||||||
|
|
||||||
} else {
|
|
||||||
it.resume(Result.failure(BleException.PermissionDenied))
|
|
||||||
|
|
||||||
}
|
|
||||||
gatt.disconnect()
|
|
||||||
} else {
|
|
||||||
|
|
||||||
it.resume(Result.failure(BleException.UnexpectedResponse))
|
|
||||||
gatt.disconnect()
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (checkPermission()) {
|
return result
|
||||||
device.connectGatt(app, false, callback)
|
|
||||||
} else {
|
|
||||||
it.resume(Result.failure(BleException.PermissionDenied))
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1167,109 +1182,130 @@ class BleRepositoryImpl @Inject constructor(
|
||||||
serviceId: UUID,
|
serviceId: UUID,
|
||||||
characteristicId: UUID,
|
characteristicId: UUID,
|
||||||
writeData: ByteArray
|
writeData: ByteArray
|
||||||
): Result<Unit, BleException> = suspendCancellableCoroutine {
|
): Result<Unit, BleException> {
|
||||||
|
|
||||||
var bleGatt: BluetoothGatt? = null
|
var result: Result<Unit, BleException> = Result.failure(BleException.UnexpectedResponse)
|
||||||
|
|
||||||
val callback = object : BluetoothGattCallback() {
|
repeat(5){
|
||||||
|
result = suspendCancellableCoroutine {
|
||||||
|
|
||||||
override fun onConnectionStateChange(
|
var bleGatt: BluetoothGatt? = null
|
||||||
gatt: BluetoothGatt,
|
|
||||||
status: Int,
|
|
||||||
newState: Int
|
|
||||||
) {
|
|
||||||
|
|
||||||
if (status == BluetoothGatt.GATT_SUCCESS) {
|
val callback = object : BluetoothGattCallback() {
|
||||||
|
|
||||||
if (newState == BluetoothProfile.STATE_CONNECTED) {
|
override fun onConnectionStateChange(
|
||||||
|
gatt: BluetoothGatt,
|
||||||
|
status: Int,
|
||||||
|
newState: Int
|
||||||
|
) {
|
||||||
|
|
||||||
if (checkPermission()) {
|
if (status == BluetoothGatt.GATT_SUCCESS) {
|
||||||
|
|
||||||
gatt.discoverServices()
|
if (newState == BluetoothProfile.STATE_CONNECTED) {
|
||||||
|
|
||||||
|
if (checkPermission()) {
|
||||||
|
|
||||||
|
gatt.discoverServices()
|
||||||
|
|
||||||
|
} else {
|
||||||
|
|
||||||
|
bleGatt?.disconnect()
|
||||||
|
it.resume(Result.failure(BleException.PermissionDenied))
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
|
||||||
|
bleGatt?.close()
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
bleGatt?.disconnect()
|
bleGatt?.disconnect()
|
||||||
it.resume(Result.failure(BleException.PermissionDenied))
|
it.resume(Result.failure(BleException.UnexpectedResponse))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
|
||||||
|
|
||||||
bleGatt?.close()
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
override fun onServicesDiscovered(
|
||||||
|
gatt: BluetoothGatt,
|
||||||
|
status: Int
|
||||||
|
) {
|
||||||
|
super.onServicesDiscovered(gatt, status)
|
||||||
|
|
||||||
bleGatt?.disconnect()
|
if (status == BluetoothGatt.GATT_SUCCESS) {
|
||||||
it.resume(Result.failure(BleException.UnexpectedResponse))
|
|
||||||
|
|
||||||
}
|
gatt.services?.firstOrNull { service ->
|
||||||
|
service.uuid == serviceId
|
||||||
|
}?.characteristics?.firstOrNull { characteristic ->
|
||||||
|
characteristic.uuid == characteristicId
|
||||||
|
}?.let { char ->
|
||||||
|
|
||||||
}
|
if (checkPermission()) {
|
||||||
|
|
||||||
override fun onServicesDiscovered(
|
gatt.writeCharacteristic(char, writeData)
|
||||||
gatt: BluetoothGatt,
|
|
||||||
status: Int
|
|
||||||
) {
|
|
||||||
super.onServicesDiscovered(gatt, status)
|
|
||||||
|
|
||||||
if (status == BluetoothGatt.GATT_SUCCESS) {
|
} else {
|
||||||
|
|
||||||
gatt.services?.firstOrNull { service ->
|
bleGatt?.disconnect()
|
||||||
service.uuid == serviceId
|
it.resume(Result.failure(BleException.PermissionDenied))
|
||||||
}?.characteristics?.firstOrNull { characteristic ->
|
|
||||||
characteristic.uuid == characteristicId
|
|
||||||
}?.let { char ->
|
|
||||||
|
|
||||||
if (checkPermission()) {
|
}
|
||||||
|
|
||||||
gatt.writeCharacteristic(char, writeData)
|
return
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
Log.d("write", "service not found")
|
||||||
|
|
||||||
|
bleGatt?.disconnect()
|
||||||
|
it.resume(Result.failure(BleException.UnexpectedResponse))
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
bleGatt?.disconnect()
|
bleGatt?.disconnect()
|
||||||
|
it.resume(Result.failure(BleException.UnexpectedResponse))
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onCharacteristicWrite(
|
||||||
|
gatt: BluetoothGatt,
|
||||||
|
characteristic: BluetoothGattCharacteristic,
|
||||||
|
status: Int
|
||||||
|
) {
|
||||||
|
super.onCharacteristicWrite(gatt, characteristic, status)
|
||||||
|
|
||||||
|
if (checkPermission()) {
|
||||||
|
|
||||||
|
if(status == BluetoothGatt.GATT_SUCCESS) {
|
||||||
|
|
||||||
|
it.resume(Result.success(Unit))
|
||||||
|
|
||||||
|
} else {
|
||||||
|
|
||||||
|
it.resume(Result.failure(BleException.UnexpectedResponse))
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
|
||||||
it.resume(Result.failure(BleException.PermissionDenied))
|
it.resume(Result.failure(BleException.PermissionDenied))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
gatt.disconnect()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Log.d("write", "service not found")
|
|
||||||
|
|
||||||
bleGatt?.disconnect()
|
|
||||||
it.resume(Result.failure(BleException.UnexpectedResponse))
|
|
||||||
|
|
||||||
} else {
|
|
||||||
|
|
||||||
bleGatt?.disconnect()
|
|
||||||
it.resume(Result.failure(BleException.UnexpectedResponse))
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onCharacteristicWrite(
|
|
||||||
gatt: BluetoothGatt,
|
|
||||||
characteristic: BluetoothGattCharacteristic,
|
|
||||||
status: Int
|
|
||||||
) {
|
|
||||||
super.onCharacteristicWrite(gatt, characteristic, status)
|
|
||||||
|
|
||||||
if (checkPermission()) {
|
if (checkPermission()) {
|
||||||
|
|
||||||
if(status == BluetoothGatt.GATT_SUCCESS) {
|
bleGatt = device.connectGatt(app, false, callback)
|
||||||
|
|
||||||
it.resume(Result.success(Unit))
|
|
||||||
|
|
||||||
} else {
|
|
||||||
|
|
||||||
it.resume(Result.failure(BleException.UnexpectedResponse))
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
|
|
@ -1277,21 +1313,15 @@ class BleRepositoryImpl @Inject constructor(
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
gatt.disconnect()
|
}
|
||||||
|
|
||||||
|
if(result.isSuccess || (result.getErrorOrNull() is BleException.UnexpectedResponse).not()){
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (checkPermission()) {
|
return result
|
||||||
|
|
||||||
bleGatt = device.connectGatt(app, false, callback)
|
|
||||||
|
|
||||||
} else {
|
|
||||||
|
|
||||||
it.resume(Result.failure(BleException.PermissionDenied))
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue