Improve native error handling (#10)

This commit is contained in:
Rohit Sangwan
2024-02-14 21:10:04 +05:30
committed by GitHub
parent edd5ec20a5
commit d187c8de83
5 changed files with 61 additions and 12 deletions
@@ -106,6 +106,33 @@ fun Int.parseBluetoothStatusCodeError(): String? {
}
}
fun Int.parseGattErrorCode(): String? {
return when (this) {
BluetoothGatt.GATT_SUCCESS -> null
BluetoothGatt.GATT_READ_NOT_PERMITTED -> "GATT_READ_NOT_PERMITTED"
BluetoothGatt.GATT_WRITE_NOT_PERMITTED -> "GATT_WRITE_NOT_PERMITTED"
BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION -> "GATT_INSUFFICIENT_AUTHENTICATION"
BluetoothGatt.GATT_REQUEST_NOT_SUPPORTED -> "GATT_REQUEST_NOT_SUPPORTED"
BluetoothGatt.GATT_INVALID_OFFSET -> "GATT_INVALID_OFFSET"
BluetoothGatt.GATT_INSUFFICIENT_AUTHORIZATION -> "GATT_INSUFFICIENT_AUTHORIZATION"
BluetoothGatt.GATT_INVALID_ATTRIBUTE_LENGTH -> "GATT_INVALID_ATTRIBUTE_LENGTH"
BluetoothGatt.GATT_INSUFFICIENT_ENCRYPTION -> "GATT_INSUFFICIENT_ENCRYPTION"
BluetoothGatt.GATT_CONNECTION_CONGESTED -> "GATT_CONNECTION_CONGESTED"
BluetoothGatt.GATT_FAILURE -> "GATT_FAILURE"
0x01 -> "GATT_INVALID_HANDLE"
0x04 -> "GATT_INVALID_PDU"
0x09 -> "GATT_PREPARE_QUEUE_FULL"
0x0a -> "GATT_ATTR_NOT_FOUND"
0x0b -> "GATT_ATTR_NOT_LONG"
0x0c -> "GATT_INSUFFICIENT_KEY_SIZE"
0x0e -> "GATT_UNLIKELY"
0x10 -> "GATT_UNSUPPORTED_GROUP"
0x11 -> "GATT_INSUFFICIENT_RESOURCES"
else -> "Unknown Error: $this"
}
}
val ScanResult.manufacturerDataHead: ByteArray?
get() {
val sparseArray = scanRecord?.manufacturerSpecificData ?: return null
@@ -267,7 +267,20 @@ class UniversalBlePlugin : UniversalBlePlatformChannel, BluetoothGattCallback(),
it.serviceId == characteristic.service.uuid.toString()
}.forEach {
bleCharacteristicFutureList.remove(it)
it.result(Result.success(value))
if (status == BluetoothGatt.GATT_SUCCESS) {
it.result(Result.success(value))
} else {
it.result(
Result.failure(
FlutterError(
status.toString(),
"Failed to read: (${status.parseGattErrorCode()})",
null,
)
)
)
}
}
}
@@ -374,9 +387,9 @@ class UniversalBlePlugin : UniversalBlePlatformChannel, BluetoothGattCallback(),
it.result(
Result.failure(
FlutterError(
"Failed",
"Failed to write ($status)",
"status $status"
status.toString(),
"Failed to write: (${status.parseGattErrorCode()})",
null,
)
)
)