Skip to content

Commit

Permalink
Add error in connection updates
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitsangwan01 committed Sep 25, 2024
1 parent 4914687 commit cf1e959
Show file tree
Hide file tree
Showing 17 changed files with 630 additions and 575 deletions.
110 changes: 52 additions & 58 deletions android/src/main/kotlin/com/navideck/universal_ble/UniversalBle.g.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Autogenerated from Pigeon (v21.1.0), do not edit directly.
// Autogenerated from Pigeon (v22.4.0), do not edit directly.
// See also: https://pub.dev/packages/pigeon
@file:Suppress("UNCHECKED_CAST", "ArrayInDataClass")

Expand Down Expand Up @@ -53,19 +53,18 @@ data class UniversalBleScanResult (
val name: String? = null,
val isPaired: Boolean? = null,
val rssi: Long? = null,
val manufacturerDataList: List<UniversalManufacturerData?>? = null,
val services: List<String?>? = null

) {
val manufacturerDataList: List<UniversalManufacturerData>? = null,
val services: List<String>? = null
)
{
companion object {
@Suppress("LocalVariableName")
fun fromList(__pigeon_list: List<Any?>): UniversalBleScanResult {
val deviceId = __pigeon_list[0] as String
val name = __pigeon_list[1] as String?
val isPaired = __pigeon_list[2] as Boolean?
val rssi = __pigeon_list[3].let { num -> if (num is Int) num.toLong() else num as Long? }
val manufacturerDataList = __pigeon_list[4] as List<UniversalManufacturerData?>?
val services = __pigeon_list[5] as List<String?>?
fun fromList(pigeonVar_list: List<Any?>): UniversalBleScanResult {
val deviceId = pigeonVar_list[0] as String
val name = pigeonVar_list[1] as String?
val isPaired = pigeonVar_list[2] as Boolean?
val rssi = pigeonVar_list[3] as Long?
val manufacturerDataList = pigeonVar_list[4] as List<UniversalManufacturerData>?
val services = pigeonVar_list[5] as List<String>?
return UniversalBleScanResult(deviceId, name, isPaired, rssi, manufacturerDataList, services)
}
}
Expand All @@ -84,14 +83,13 @@ data class UniversalBleScanResult (
/** Generated class from Pigeon that represents data sent in messages. */
data class UniversalBleService (
val uuid: String,
val characteristics: List<UniversalBleCharacteristic?>? = null

) {
val characteristics: List<UniversalBleCharacteristic>? = null
)
{
companion object {
@Suppress("LocalVariableName")
fun fromList(__pigeon_list: List<Any?>): UniversalBleService {
val uuid = __pigeon_list[0] as String
val characteristics = __pigeon_list[1] as List<UniversalBleCharacteristic?>?
fun fromList(pigeonVar_list: List<Any?>): UniversalBleService {
val uuid = pigeonVar_list[0] as String
val characteristics = pigeonVar_list[1] as List<UniversalBleCharacteristic>?
return UniversalBleService(uuid, characteristics)
}
}
Expand All @@ -106,14 +104,13 @@ data class UniversalBleService (
/** Generated class from Pigeon that represents data sent in messages. */
data class UniversalBleCharacteristic (
val uuid: String,
val properties: List<Long?>

) {
val properties: List<Long>
)
{
companion object {
@Suppress("LocalVariableName")
fun fromList(__pigeon_list: List<Any?>): UniversalBleCharacteristic {
val uuid = __pigeon_list[0] as String
val properties = __pigeon_list[1] as List<Long?>
fun fromList(pigeonVar_list: List<Any?>): UniversalBleCharacteristic {
val uuid = pigeonVar_list[0] as String
val properties = pigeonVar_list[1] as List<Long>
return UniversalBleCharacteristic(uuid, properties)
}
}
Expand All @@ -131,17 +128,16 @@ data class UniversalBleCharacteristic (
* Generated class from Pigeon that represents data sent in messages.
*/
data class UniversalScanFilter (
val withServices: List<String?>,
val withNamePrefix: List<String?>,
val withManufacturerData: List<UniversalManufacturerDataFilter?>

) {
val withServices: List<String>,
val withNamePrefix: List<String>,
val withManufacturerData: List<UniversalManufacturerDataFilter>
)
{
companion object {
@Suppress("LocalVariableName")
fun fromList(__pigeon_list: List<Any?>): UniversalScanFilter {
val withServices = __pigeon_list[0] as List<String?>
val withNamePrefix = __pigeon_list[1] as List<String?>
val withManufacturerData = __pigeon_list[2] as List<UniversalManufacturerDataFilter?>
fun fromList(pigeonVar_list: List<Any?>): UniversalScanFilter {
val withServices = pigeonVar_list[0] as List<String>
val withNamePrefix = pigeonVar_list[1] as List<String>
val withManufacturerData = pigeonVar_list[2] as List<UniversalManufacturerDataFilter>
return UniversalScanFilter(withServices, withNamePrefix, withManufacturerData)
}
}
Expand All @@ -159,14 +155,13 @@ data class UniversalManufacturerDataFilter (
val companyIdentifier: Long,
val data: ByteArray? = null,
val mask: ByteArray? = null

) {
)
{
companion object {
@Suppress("LocalVariableName")
fun fromList(__pigeon_list: List<Any?>): UniversalManufacturerDataFilter {
val companyIdentifier = __pigeon_list[0].let { num -> if (num is Int) num.toLong() else num as Long }
val data = __pigeon_list[1] as ByteArray?
val mask = __pigeon_list[2] as ByteArray?
fun fromList(pigeonVar_list: List<Any?>): UniversalManufacturerDataFilter {
val companyIdentifier = pigeonVar_list[0] as Long
val data = pigeonVar_list[1] as ByteArray?
val mask = pigeonVar_list[2] as ByteArray?
return UniversalManufacturerDataFilter(companyIdentifier, data, mask)
}
}
Expand All @@ -183,13 +178,12 @@ data class UniversalManufacturerDataFilter (
data class UniversalManufacturerData (
val companyIdentifier: Long,
val data: ByteArray

) {
)
{
companion object {
@Suppress("LocalVariableName")
fun fromList(__pigeon_list: List<Any?>): UniversalManufacturerData {
val companyIdentifier = __pigeon_list[0].let { num -> if (num is Int) num.toLong() else num as Long }
val data = __pigeon_list[1] as ByteArray
fun fromList(pigeonVar_list: List<Any?>): UniversalManufacturerData {
val companyIdentifier = pigeonVar_list[0] as Long
val data = pigeonVar_list[1] as ByteArray
return UniversalManufacturerData(companyIdentifier, data)
}
}
Expand All @@ -200,7 +194,7 @@ data class UniversalManufacturerData (
)
}
}
private object UniversalBlePigeonCodec : StandardMessageCodec() {
private open class UniversalBlePigeonCodec : StandardMessageCodec() {
override fun readValueOfType(type: Byte, buffer: ByteBuffer): Any? {
return when (type) {
129.toByte() -> {
Expand Down Expand Up @@ -294,7 +288,7 @@ interface UniversalBlePlatformChannel {
companion object {
/** The codec used by UniversalBlePlatformChannel. */
val codec: MessageCodec<Any?> by lazy {
UniversalBlePigeonCodec
UniversalBlePigeonCodec()
}
/** Sets up an instance of `UniversalBlePlatformChannel` to handle messages through the `binaryMessenger`. */
@JvmOverloads
Expand Down Expand Up @@ -414,7 +408,7 @@ interface UniversalBlePlatformChannel {
val deviceIdArg = args[0] as String
val serviceArg = args[1] as String
val characteristicArg = args[2] as String
val bleInputPropertyArg = args[3].let { num -> if (num is Int) num.toLong() else num as Long }
val bleInputPropertyArg = args[3] as Long
api.setNotifiable(deviceIdArg, serviceArg, characteristicArg, bleInputPropertyArg) { result: Result<Unit> ->
val error = result.exceptionOrNull()
if (error != null) {
Expand Down Expand Up @@ -476,7 +470,7 @@ interface UniversalBlePlatformChannel {
channel.setMessageHandler { message, reply ->
val args = message as List<Any?>
val deviceIdArg = args[0] as String
val expectedMtuArg = args[1].let { num -> if (num is Int) num.toLong() else num as Long }
val expectedMtuArg = args[1] as Long
api.requestMtu(deviceIdArg, expectedMtuArg) { result: Result<Long> ->
val error = result.exceptionOrNull()
if (error != null) {
Expand All @@ -500,7 +494,7 @@ interface UniversalBlePlatformChannel {
val serviceArg = args[1] as String
val characteristicArg = args[2] as String
val valueArg = args[3] as ByteArray
val bleOutputPropertyArg = args[4].let { num -> if (num is Int) num.toLong() else num as Long }
val bleOutputPropertyArg = args[4] as Long
api.writeValue(deviceIdArg, serviceArg, characteristicArg, valueArg, bleOutputPropertyArg) { result: Result<Unit> ->
val error = result.exceptionOrNull()
if (error != null) {
Expand Down Expand Up @@ -621,7 +615,7 @@ class UniversalBleCallbackChannel(private val binaryMessenger: BinaryMessenger,
companion object {
/** The codec used by UniversalBleCallbackChannel. */
val codec: MessageCodec<Any?> by lazy {
UniversalBlePigeonCodec
UniversalBlePigeonCodec()
}
}
fun onAvailabilityChanged(stateArg: Long, callback: (Result<Unit>) -> Unit)
Expand Down Expand Up @@ -692,12 +686,12 @@ class UniversalBleCallbackChannel(private val binaryMessenger: BinaryMessenger,
}
}
}
fun onConnectionChanged(deviceIdArg: String, connectedArg: Boolean, callback: (Result<Unit>) -> Unit)
fun onConnectionChanged(deviceIdArg: String, connectedArg: Boolean, errorArg: String?, callback: (Result<Unit>) -> Unit)
{
val separatedMessageChannelSuffix = if (messageChannelSuffix.isNotEmpty()) ".$messageChannelSuffix" else ""
val channelName = "dev.flutter.pigeon.universal_ble.UniversalBleCallbackChannel.onConnectionChanged$separatedMessageChannelSuffix"
val channel = BasicMessageChannel<Any?>(binaryMessenger, channelName, codec)
channel.send(listOf(deviceIdArg, connectedArg)) {
channel.send(listOf(deviceIdArg, connectedArg, errorArg)) {
if (it is List<*>) {
if (it.size > 1) {
callback(Result.failure(FlutterError(it[0] as String, it[1] as String, it[2] as String?)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,82 @@ val DeviceDisconnectedError: FlutterError = FlutterError(
null
)

fun Int.parseHciErrorCode(): String? {
return when (this) {
BluetoothGatt.GATT_SUCCESS -> null
0x01 -> "Unknown HCI Command"
0x02 -> "Unknown Connection Identifier"
0x03 -> "Hardware Failure"
0x04 -> "Page Timeout"
0x05 -> "Authentication Failure"
0x06 -> "PIN or Key Missing"
0x07 -> "Memory Capacity Exceeded"
0x08 -> "Connection Timeout"
0x09 -> "Connection Limit Exceeded"
0x0A -> "Synchronous Connection Limit To A Device Exceeded"
0x0B -> "Connection Already Exists"
0x0C -> "Command Disallowed"
0x0D -> "Connection Rejected due to Limited Resources"
0x0E -> "Connection Rejected Due To Security Reasons"
0x0F -> "Connection Rejected due to Unacceptable BD_ADDR"
0x10 -> "Connection Accept Timeout Exceeded"
0x11 -> "Unsupported Feature or Parameter Value"
0x12 -> "Invalid HCI Command Parameters"
0x13 -> "Remote User Terminated Connection"
0x14 -> "Remote Device Terminated Connection due to Low Resources"
0x15 -> "Remote Device Terminated Connection due to Power Off"
0x16 -> "Connection Terminated By Local Host"
0x17 -> "Repeated Attempts"
0x18 -> "Pairing Not Allowed"
0x19 -> "Unknown LMP PDU"
0x1A -> "Unsupported Remote Feature / Unsupported LMP Feature"
0x1B -> "SCO Offset Rejected"
0x1C -> "SCO Interval Rejected"
0x1D -> "SCO Air Mode Rejected"
0x1E -> "Invalid LMP Parameters / Invalid LL Parameters"
0x1F -> "Unspecified Error"
0x20 -> "Unsupported LMP Parameter Value / Unsupported LL Parameter Value"
0x21 -> "Role Change Not Allowed"
0x22 -> "LMP Response Timeout / LL Response Timeout"
0x23 -> "LMP Error Transaction Collision / LL Procedure Collision"
0x24 -> "LMP PDU Not Allowed"
0x25 -> "Encryption Mode Not Acceptable"
0x26 -> "Link Key cannot be Changed"
0x27 -> "Requested QoS Not Supported"
0x28 -> "Instant Passed"
0x29 -> "Pairing With Unit Key Not Supported"
0x2A -> "Different Transaction Collision"
0x2B -> "Reserved for future use"
0x2C -> "QoS Unacceptable Parameter"
0x2D -> "QoS Rejected"
0x2E -> "Channel Classification Not Supported"
0x2F -> "Insufficient Security"
0x30 -> "Parameter Out Of Mandatory Range"
0x31 -> "Reserved for future use"
0x32 -> "Role Switch Pending"
0x33 -> "Reserved for future use"
0x34 -> "Reserved Slot Violation"
0x35 -> "Role Switch Failed"
0x36 -> "Extended Inquiry Response Too Large"
0x37 -> "Secure Simple Pairing Not Supported By Host"
0x38 -> "Host Busy - Pairing"
0x39 -> "Connection Rejected due to No Suitable Channel Found"
0x3A -> "Controller Busy"
0x3B -> "Unacceptable Connection Parameters"
0x3C -> "Advertising Timeout"
0x3D -> "Connection Terminated due to MIC Failure"
0x3E -> "Connection Failed to be Established / Synchronization Timeout"
0x3F -> "MAC Connection Failed"
0x40 -> "Coarse Clock Adjustment Rejected but Will Try to Adjust Using Clock Dragging"
0x41 -> "Type0 Submap Not Defined"
0x42 -> "Unknown Advertising Identifier"
0x43 -> "Limit Reached"
0x44 -> "Operation Cancelled by Host"
0x45 -> "Packet Too Long"
else -> "Unknown Error $this"
}
}

// Future result classes
class DiscoverServicesFuture(
val deviceId: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class UniversalBlePlugin : UniversalBlePlatformChannel, BluetoothGattCallback(),
if (currentState == BluetoothGatt.STATE_CONNECTED) {
Log.e(TAG, "$deviceId Already connected")
mainThreadHandler?.post {
callbackChannel?.onConnectionChanged(deviceId, true) {}
callbackChannel?.onConnectionChanged(deviceId, true, null) {}
}
return
} else if (currentState == BluetoothGatt.STATE_CONNECTING) {
Expand Down Expand Up @@ -864,19 +864,22 @@ class UniversalBlePlugin : UniversalBlePlatformChannel, BluetoothGattCallback(),

override fun onConnectionStateChange(gatt: BluetoothGatt, status: Int, newState: Int) {
devicesStateMap[gatt.device.address] = newState
Log.d(TAG, "onConnectionStateChange-> Status: ${status}, NewState: $newState")
Log.d(
TAG,
"onConnectionStateChange-> Status: $status ${status.parseHciErrorCode()}, NewState: $newState"
)

if (newState == BluetoothGatt.STATE_CONNECTED) {
mainThreadHandler?.post {
callbackChannel?.onConnectionChanged(
gatt.device.address, true
gatt.device.address, true, status.parseHciErrorCode()
) {}
}
} else if (newState == BluetoothGatt.STATE_DISCONNECTED) {
cleanConnection(gatt)
mainThreadHandler?.post {
callbackChannel?.onConnectionChanged(
gatt.device.address, false
gatt.device.address, false, status.parseHciErrorCode()
) {}
}
}
Expand Down
Loading

0 comments on commit cf1e959

Please sign in to comment.