Improve setNotifiation completion (#42)
* Improve setNotifiation completion * Implement Windows * Minor android fixes * Few more fixes * Update changelog --------- Co-authored-by: Foti Dim <fdimanidis@gmail.com>
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
// Autogenerated from Pigeon (v17.1.1), do not edit directly.
|
||||
// Autogenerated from Pigeon (v18.0.1), do not edit directly.
|
||||
// See also: https://pub.dev/packages/pigeon
|
||||
@file:Suppress("UNCHECKED_CAST", "ArrayInDataClass")
|
||||
|
||||
package com.navideck.universal_ble
|
||||
|
||||
@@ -16,14 +17,14 @@ private fun wrapResult(result: Any?): List<Any?> {
|
||||
}
|
||||
|
||||
private fun wrapError(exception: Throwable): List<Any?> {
|
||||
if (exception is FlutterError) {
|
||||
return listOf(
|
||||
return if (exception is FlutterError) {
|
||||
listOf(
|
||||
exception.code,
|
||||
exception.message,
|
||||
exception.details
|
||||
)
|
||||
} else {
|
||||
return listOf(
|
||||
listOf(
|
||||
exception.javaClass.simpleName,
|
||||
exception.toString(),
|
||||
"Cause: " + exception.cause + ", Stacktrace: " + Log.getStackTraceString(exception)
|
||||
@@ -58,15 +59,15 @@ data class UniversalBleScanResult (
|
||||
|
||||
) {
|
||||
companion object {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
fun fromList(list: List<Any?>): UniversalBleScanResult {
|
||||
val deviceId = list[0] as String
|
||||
val name = list[1] as String?
|
||||
val isPaired = list[2] as Boolean?
|
||||
val rssi = list[3].let { if (it is Int) it.toLong() else it as Long? }
|
||||
val manufacturerData = list[4] as ByteArray?
|
||||
val manufacturerDataHead = list[5] as ByteArray?
|
||||
val services = list[6] as List<String?>?
|
||||
@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 manufacturerData = __pigeon_list[4] as ByteArray?
|
||||
val manufacturerDataHead = __pigeon_list[5] as ByteArray?
|
||||
val services = __pigeon_list[6] as List<String?>?
|
||||
return UniversalBleScanResult(deviceId, name, isPaired, rssi, manufacturerData, manufacturerDataHead, services)
|
||||
}
|
||||
}
|
||||
@@ -90,10 +91,10 @@ data class UniversalBleService (
|
||||
|
||||
) {
|
||||
companion object {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
fun fromList(list: List<Any?>): UniversalBleService {
|
||||
val uuid = list[0] as String
|
||||
val characteristics = list[1] as List<UniversalBleCharacteristic?>?
|
||||
@Suppress("LocalVariableName")
|
||||
fun fromList(__pigeon_list: List<Any?>): UniversalBleService {
|
||||
val uuid = __pigeon_list[0] as String
|
||||
val characteristics = __pigeon_list[1] as List<UniversalBleCharacteristic?>?
|
||||
return UniversalBleService(uuid, characteristics)
|
||||
}
|
||||
}
|
||||
@@ -112,10 +113,10 @@ data class UniversalBleCharacteristic (
|
||||
|
||||
) {
|
||||
companion object {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
fun fromList(list: List<Any?>): UniversalBleCharacteristic {
|
||||
val uuid = list[0] as String
|
||||
val properties = list[1] as List<Long?>
|
||||
@Suppress("LocalVariableName")
|
||||
fun fromList(__pigeon_list: List<Any?>): UniversalBleCharacteristic {
|
||||
val uuid = __pigeon_list[0] as String
|
||||
val properties = __pigeon_list[1] as List<Long?>
|
||||
return UniversalBleCharacteristic(uuid, properties)
|
||||
}
|
||||
}
|
||||
@@ -138,10 +139,10 @@ data class UniversalScanFilter (
|
||||
|
||||
) {
|
||||
companion object {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
fun fromList(list: List<Any?>): UniversalScanFilter {
|
||||
val withServices = list[0] as List<String?>
|
||||
val withManufacturerData = list[1] as List<UniversalManufacturerDataFilter?>
|
||||
@Suppress("LocalVariableName")
|
||||
fun fromList(__pigeon_list: List<Any?>): UniversalScanFilter {
|
||||
val withServices = __pigeon_list[0] as List<String?>
|
||||
val withManufacturerData = __pigeon_list[1] as List<UniversalManufacturerDataFilter?>
|
||||
return UniversalScanFilter(withServices, withManufacturerData)
|
||||
}
|
||||
}
|
||||
@@ -161,11 +162,11 @@ data class UniversalManufacturerDataFilter (
|
||||
|
||||
) {
|
||||
companion object {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
fun fromList(list: List<Any?>): UniversalManufacturerDataFilter {
|
||||
val companyIdentifier = list[0].let { if (it is Int) it.toLong() else it as Long? }
|
||||
val data = list[1] as ByteArray?
|
||||
val mask = list[2] as ByteArray?
|
||||
@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?
|
||||
return UniversalManufacturerDataFilter(companyIdentifier, data, mask)
|
||||
}
|
||||
}
|
||||
@@ -178,7 +179,6 @@ data class UniversalManufacturerDataFilter (
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
private object UniversalBlePlatformChannelCodec : StandardMessageCodec() {
|
||||
override fun readValueOfType(type: Byte, buffer: ByteBuffer): Any? {
|
||||
return when (type) {
|
||||
@@ -249,7 +249,7 @@ interface UniversalBlePlatformChannel {
|
||||
fun stopScan()
|
||||
fun connect(deviceId: String)
|
||||
fun disconnect(deviceId: String)
|
||||
fun setNotifiable(deviceId: String, service: String, characteristic: String, bleInputProperty: Long)
|
||||
fun setNotifiable(deviceId: String, service: String, characteristic: String, bleInputProperty: Long, callback: (Result<Unit>) -> Unit)
|
||||
fun discoverServices(deviceId: String, callback: (Result<List<UniversalBleService>>) -> Unit)
|
||||
fun readValue(deviceId: String, service: String, characteristic: String, callback: (Result<ByteArray>) -> Unit)
|
||||
fun requestMtu(deviceId: String, expectedMtu: Long, callback: (Result<Long>) -> Unit)
|
||||
@@ -265,10 +265,10 @@ interface UniversalBlePlatformChannel {
|
||||
UniversalBlePlatformChannelCodec
|
||||
}
|
||||
/** Sets up an instance of `UniversalBlePlatformChannel` to handle messages through the `binaryMessenger`. */
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
fun setUp(binaryMessenger: BinaryMessenger, api: UniversalBlePlatformChannel?) {
|
||||
fun setUp(binaryMessenger: BinaryMessenger, api: UniversalBlePlatformChannel?, messageChannelSuffix: String = "") {
|
||||
val separatedMessageChannelSuffix = if (messageChannelSuffix.isNotEmpty()) ".$messageChannelSuffix" else ""
|
||||
run {
|
||||
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.getBluetoothAvailabilityState", codec)
|
||||
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.getBluetoothAvailabilityState$separatedMessageChannelSuffix", codec)
|
||||
if (api != null) {
|
||||
channel.setMessageHandler { _, reply ->
|
||||
api.getBluetoothAvailabilityState() { result: Result<Long> ->
|
||||
@@ -286,7 +286,7 @@ interface UniversalBlePlatformChannel {
|
||||
}
|
||||
}
|
||||
run {
|
||||
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.enableBluetooth", codec)
|
||||
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.enableBluetooth$separatedMessageChannelSuffix", codec)
|
||||
if (api != null) {
|
||||
channel.setMessageHandler { _, reply ->
|
||||
api.enableBluetooth() { result: Result<Boolean> ->
|
||||
@@ -304,17 +304,16 @@ interface UniversalBlePlatformChannel {
|
||||
}
|
||||
}
|
||||
run {
|
||||
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.startScan", codec)
|
||||
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.startScan$separatedMessageChannelSuffix", codec)
|
||||
if (api != null) {
|
||||
channel.setMessageHandler { message, reply ->
|
||||
val args = message as List<Any?>
|
||||
val filterArg = args[0] as UniversalScanFilter?
|
||||
var wrapped: List<Any?>
|
||||
try {
|
||||
val wrapped: List<Any?> = try {
|
||||
api.startScan(filterArg)
|
||||
wrapped = listOf<Any?>(null)
|
||||
listOf<Any?>(null)
|
||||
} catch (exception: Throwable) {
|
||||
wrapped = wrapError(exception)
|
||||
wrapError(exception)
|
||||
}
|
||||
reply.reply(wrapped)
|
||||
}
|
||||
@@ -323,15 +322,14 @@ interface UniversalBlePlatformChannel {
|
||||
}
|
||||
}
|
||||
run {
|
||||
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.stopScan", codec)
|
||||
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.stopScan$separatedMessageChannelSuffix", codec)
|
||||
if (api != null) {
|
||||
channel.setMessageHandler { _, reply ->
|
||||
var wrapped: List<Any?>
|
||||
try {
|
||||
val wrapped: List<Any?> = try {
|
||||
api.stopScan()
|
||||
wrapped = listOf<Any?>(null)
|
||||
listOf<Any?>(null)
|
||||
} catch (exception: Throwable) {
|
||||
wrapped = wrapError(exception)
|
||||
wrapError(exception)
|
||||
}
|
||||
reply.reply(wrapped)
|
||||
}
|
||||
@@ -340,17 +338,16 @@ interface UniversalBlePlatformChannel {
|
||||
}
|
||||
}
|
||||
run {
|
||||
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.connect", codec)
|
||||
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.connect$separatedMessageChannelSuffix", codec)
|
||||
if (api != null) {
|
||||
channel.setMessageHandler { message, reply ->
|
||||
val args = message as List<Any?>
|
||||
val deviceIdArg = args[0] as String
|
||||
var wrapped: List<Any?>
|
||||
try {
|
||||
val wrapped: List<Any?> = try {
|
||||
api.connect(deviceIdArg)
|
||||
wrapped = listOf<Any?>(null)
|
||||
listOf<Any?>(null)
|
||||
} catch (exception: Throwable) {
|
||||
wrapped = wrapError(exception)
|
||||
wrapError(exception)
|
||||
}
|
||||
reply.reply(wrapped)
|
||||
}
|
||||
@@ -359,17 +356,16 @@ interface UniversalBlePlatformChannel {
|
||||
}
|
||||
}
|
||||
run {
|
||||
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.disconnect", codec)
|
||||
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.disconnect$separatedMessageChannelSuffix", codec)
|
||||
if (api != null) {
|
||||
channel.setMessageHandler { message, reply ->
|
||||
val args = message as List<Any?>
|
||||
val deviceIdArg = args[0] as String
|
||||
var wrapped: List<Any?>
|
||||
try {
|
||||
val wrapped: List<Any?> = try {
|
||||
api.disconnect(deviceIdArg)
|
||||
wrapped = listOf<Any?>(null)
|
||||
listOf<Any?>(null)
|
||||
} catch (exception: Throwable) {
|
||||
wrapped = wrapError(exception)
|
||||
wrapError(exception)
|
||||
}
|
||||
reply.reply(wrapped)
|
||||
}
|
||||
@@ -378,29 +374,29 @@ interface UniversalBlePlatformChannel {
|
||||
}
|
||||
}
|
||||
run {
|
||||
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.setNotifiable", codec)
|
||||
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.setNotifiable$separatedMessageChannelSuffix", codec)
|
||||
if (api != null) {
|
||||
channel.setMessageHandler { message, reply ->
|
||||
val args = message as List<Any?>
|
||||
val deviceIdArg = args[0] as String
|
||||
val serviceArg = args[1] as String
|
||||
val characteristicArg = args[2] as String
|
||||
val bleInputPropertyArg = args[3].let { if (it is Int) it.toLong() else it as Long }
|
||||
var wrapped: List<Any?>
|
||||
try {
|
||||
api.setNotifiable(deviceIdArg, serviceArg, characteristicArg, bleInputPropertyArg)
|
||||
wrapped = listOf<Any?>(null)
|
||||
} catch (exception: Throwable) {
|
||||
wrapped = wrapError(exception)
|
||||
val bleInputPropertyArg = args[3].let { num -> if (num is Int) num.toLong() else num as Long }
|
||||
api.setNotifiable(deviceIdArg, serviceArg, characteristicArg, bleInputPropertyArg) { result: Result<Unit> ->
|
||||
val error = result.exceptionOrNull()
|
||||
if (error != null) {
|
||||
reply.reply(wrapError(error))
|
||||
} else {
|
||||
reply.reply(wrapResult(null))
|
||||
}
|
||||
}
|
||||
reply.reply(wrapped)
|
||||
}
|
||||
} else {
|
||||
channel.setMessageHandler(null)
|
||||
}
|
||||
}
|
||||
run {
|
||||
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.discoverServices", codec)
|
||||
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.discoverServices$separatedMessageChannelSuffix", codec)
|
||||
if (api != null) {
|
||||
channel.setMessageHandler { message, reply ->
|
||||
val args = message as List<Any?>
|
||||
@@ -420,7 +416,7 @@ interface UniversalBlePlatformChannel {
|
||||
}
|
||||
}
|
||||
run {
|
||||
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.readValue", codec)
|
||||
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.readValue$separatedMessageChannelSuffix", codec)
|
||||
if (api != null) {
|
||||
channel.setMessageHandler { message, reply ->
|
||||
val args = message as List<Any?>
|
||||
@@ -442,12 +438,12 @@ interface UniversalBlePlatformChannel {
|
||||
}
|
||||
}
|
||||
run {
|
||||
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.requestMtu", codec)
|
||||
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.requestMtu$separatedMessageChannelSuffix", codec)
|
||||
if (api != null) {
|
||||
channel.setMessageHandler { message, reply ->
|
||||
val args = message as List<Any?>
|
||||
val deviceIdArg = args[0] as String
|
||||
val expectedMtuArg = args[1].let { if (it is Int) it.toLong() else it as Long }
|
||||
val expectedMtuArg = args[1].let { num -> if (num is Int) num.toLong() else num as Long }
|
||||
api.requestMtu(deviceIdArg, expectedMtuArg) { result: Result<Long> ->
|
||||
val error = result.exceptionOrNull()
|
||||
if (error != null) {
|
||||
@@ -463,7 +459,7 @@ interface UniversalBlePlatformChannel {
|
||||
}
|
||||
}
|
||||
run {
|
||||
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.writeValue", codec)
|
||||
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.writeValue$separatedMessageChannelSuffix", codec)
|
||||
if (api != null) {
|
||||
channel.setMessageHandler { message, reply ->
|
||||
val args = message as List<Any?>
|
||||
@@ -471,7 +467,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 { if (it is Int) it.toLong() else it as Long }
|
||||
val bleOutputPropertyArg = args[4].let { num -> if (num is Int) num.toLong() else num as Long }
|
||||
api.writeValue(deviceIdArg, serviceArg, characteristicArg, valueArg, bleOutputPropertyArg) { result: Result<Unit> ->
|
||||
val error = result.exceptionOrNull()
|
||||
if (error != null) {
|
||||
@@ -486,7 +482,7 @@ interface UniversalBlePlatformChannel {
|
||||
}
|
||||
}
|
||||
run {
|
||||
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.isPaired", codec)
|
||||
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.isPaired$separatedMessageChannelSuffix", codec)
|
||||
if (api != null) {
|
||||
channel.setMessageHandler { message, reply ->
|
||||
val args = message as List<Any?>
|
||||
@@ -506,17 +502,16 @@ interface UniversalBlePlatformChannel {
|
||||
}
|
||||
}
|
||||
run {
|
||||
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.pair", codec)
|
||||
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.pair$separatedMessageChannelSuffix", codec)
|
||||
if (api != null) {
|
||||
channel.setMessageHandler { message, reply ->
|
||||
val args = message as List<Any?>
|
||||
val deviceIdArg = args[0] as String
|
||||
var wrapped: List<Any?>
|
||||
try {
|
||||
val wrapped: List<Any?> = try {
|
||||
api.pair(deviceIdArg)
|
||||
wrapped = listOf<Any?>(null)
|
||||
listOf<Any?>(null)
|
||||
} catch (exception: Throwable) {
|
||||
wrapped = wrapError(exception)
|
||||
wrapError(exception)
|
||||
}
|
||||
reply.reply(wrapped)
|
||||
}
|
||||
@@ -525,17 +520,16 @@ interface UniversalBlePlatformChannel {
|
||||
}
|
||||
}
|
||||
run {
|
||||
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.unPair", codec)
|
||||
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.unPair$separatedMessageChannelSuffix", codec)
|
||||
if (api != null) {
|
||||
channel.setMessageHandler { message, reply ->
|
||||
val args = message as List<Any?>
|
||||
val deviceIdArg = args[0] as String
|
||||
var wrapped: List<Any?>
|
||||
try {
|
||||
val wrapped: List<Any?> = try {
|
||||
api.unPair(deviceIdArg)
|
||||
wrapped = listOf<Any?>(null)
|
||||
listOf<Any?>(null)
|
||||
} catch (exception: Throwable) {
|
||||
wrapped = wrapError(exception)
|
||||
wrapError(exception)
|
||||
}
|
||||
reply.reply(wrapped)
|
||||
}
|
||||
@@ -544,7 +538,7 @@ interface UniversalBlePlatformChannel {
|
||||
}
|
||||
}
|
||||
run {
|
||||
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.getConnectedDevices", codec)
|
||||
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.getConnectedDevices$separatedMessageChannelSuffix", codec)
|
||||
if (api != null) {
|
||||
channel.setMessageHandler { message, reply ->
|
||||
val args = message as List<Any?>
|
||||
@@ -566,7 +560,6 @@ interface UniversalBlePlatformChannel {
|
||||
}
|
||||
}
|
||||
}
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
private object UniversalBleCallbackChannelCodec : StandardMessageCodec() {
|
||||
override fun readValueOfType(type: Byte, buffer: ByteBuffer): Any? {
|
||||
return when (type) {
|
||||
@@ -594,8 +587,7 @@ private object UniversalBleCallbackChannelCodec : StandardMessageCodec() {
|
||||
*
|
||||
* Generated class from Pigeon that represents Flutter messages that can be called from Kotlin.
|
||||
*/
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
class UniversalBleCallbackChannel(private val binaryMessenger: BinaryMessenger) {
|
||||
class UniversalBleCallbackChannel(private val binaryMessenger: BinaryMessenger, private val messageChannelSuffix: String = "") {
|
||||
companion object {
|
||||
/** The codec used by UniversalBleCallbackChannel. */
|
||||
val codec: MessageCodec<Any?> by lazy {
|
||||
@@ -604,7 +596,8 @@ class UniversalBleCallbackChannel(private val binaryMessenger: BinaryMessenger)
|
||||
}
|
||||
fun onAvailabilityChanged(stateArg: Long, callback: (Result<Unit>) -> Unit)
|
||||
{
|
||||
val channelName = "dev.flutter.pigeon.universal_ble.UniversalBleCallbackChannel.onAvailabilityChanged"
|
||||
val separatedMessageChannelSuffix = if (messageChannelSuffix.isNotEmpty()) ".$messageChannelSuffix" else ""
|
||||
val channelName = "dev.flutter.pigeon.universal_ble.UniversalBleCallbackChannel.onAvailabilityChanged$separatedMessageChannelSuffix"
|
||||
val channel = BasicMessageChannel<Any?>(binaryMessenger, channelName, codec)
|
||||
channel.send(listOf(stateArg)) {
|
||||
if (it is List<*>) {
|
||||
@@ -620,7 +613,8 @@ class UniversalBleCallbackChannel(private val binaryMessenger: BinaryMessenger)
|
||||
}
|
||||
fun onPairStateChange(deviceIdArg: String, isPairedArg: Boolean, errorArg: String?, callback: (Result<Unit>) -> Unit)
|
||||
{
|
||||
val channelName = "dev.flutter.pigeon.universal_ble.UniversalBleCallbackChannel.onPairStateChange"
|
||||
val separatedMessageChannelSuffix = if (messageChannelSuffix.isNotEmpty()) ".$messageChannelSuffix" else ""
|
||||
val channelName = "dev.flutter.pigeon.universal_ble.UniversalBleCallbackChannel.onPairStateChange$separatedMessageChannelSuffix"
|
||||
val channel = BasicMessageChannel<Any?>(binaryMessenger, channelName, codec)
|
||||
channel.send(listOf(deviceIdArg, isPairedArg, errorArg)) {
|
||||
if (it is List<*>) {
|
||||
@@ -636,7 +630,8 @@ class UniversalBleCallbackChannel(private val binaryMessenger: BinaryMessenger)
|
||||
}
|
||||
fun onScanResult(resultArg: UniversalBleScanResult, callback: (Result<Unit>) -> Unit)
|
||||
{
|
||||
val channelName = "dev.flutter.pigeon.universal_ble.UniversalBleCallbackChannel.onScanResult"
|
||||
val separatedMessageChannelSuffix = if (messageChannelSuffix.isNotEmpty()) ".$messageChannelSuffix" else ""
|
||||
val channelName = "dev.flutter.pigeon.universal_ble.UniversalBleCallbackChannel.onScanResult$separatedMessageChannelSuffix"
|
||||
val channel = BasicMessageChannel<Any?>(binaryMessenger, channelName, codec)
|
||||
channel.send(listOf(resultArg)) {
|
||||
if (it is List<*>) {
|
||||
@@ -652,7 +647,8 @@ class UniversalBleCallbackChannel(private val binaryMessenger: BinaryMessenger)
|
||||
}
|
||||
fun onValueChanged(deviceIdArg: String, characteristicIdArg: String, valueArg: ByteArray, callback: (Result<Unit>) -> Unit)
|
||||
{
|
||||
val channelName = "dev.flutter.pigeon.universal_ble.UniversalBleCallbackChannel.onValueChanged"
|
||||
val separatedMessageChannelSuffix = if (messageChannelSuffix.isNotEmpty()) ".$messageChannelSuffix" else ""
|
||||
val channelName = "dev.flutter.pigeon.universal_ble.UniversalBleCallbackChannel.onValueChanged$separatedMessageChannelSuffix"
|
||||
val channel = BasicMessageChannel<Any?>(binaryMessenger, channelName, codec)
|
||||
channel.send(listOf(deviceIdArg, characteristicIdArg, valueArg)) {
|
||||
if (it is List<*>) {
|
||||
@@ -668,7 +664,8 @@ class UniversalBleCallbackChannel(private val binaryMessenger: BinaryMessenger)
|
||||
}
|
||||
fun onConnectionChanged(deviceIdArg: String, stateArg: Long, callback: (Result<Unit>) -> Unit)
|
||||
{
|
||||
val channelName = "dev.flutter.pigeon.universal_ble.UniversalBleCallbackChannel.onConnectionChanged"
|
||||
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, stateArg)) {
|
||||
if (it is List<*>) {
|
||||
|
||||
@@ -15,6 +15,7 @@ import android.bluetooth.le.ScanCallback.SCAN_FAILED_OUT_OF_HARDWARE_RESOURCES
|
||||
import android.bluetooth.le.ScanCallback.SCAN_FAILED_SCANNING_TOO_FREQUENTLY
|
||||
import android.bluetooth.le.ScanFilter
|
||||
import android.bluetooth.le.ScanResult
|
||||
import android.os.Build
|
||||
import android.os.ParcelUuid
|
||||
import android.util.Log
|
||||
import java.nio.ByteBuffer
|
||||
@@ -24,7 +25,7 @@ import java.util.UUID
|
||||
private const val TAG = "UniversalBlePlugin"
|
||||
|
||||
val knownGatts = mutableListOf<BluetoothGatt>()
|
||||
|
||||
val ccdCharacteristic = "00002902-0000-1000-8000-00805f9b34fb"
|
||||
|
||||
enum class BleConnectionState(val value: Long) {
|
||||
Connected(0),
|
||||
@@ -216,12 +217,12 @@ fun BluetoothGatt.getCharacteristic(
|
||||
|
||||
|
||||
@SuppressLint("MissingPermission")
|
||||
@Suppress("DEPRECATION")
|
||||
fun BluetoothGatt.setNotifiable(
|
||||
gattCharacteristic: BluetoothGattCharacteristic,
|
||||
bleInputProperty: Long,
|
||||
) {
|
||||
val descClientCharConfirmation =
|
||||
UUID.fromString("00002902-0000-1000-8000-00805f9b34fb")
|
||||
): Boolean {
|
||||
val descClientCharConfirmation = UUID.fromString(ccdCharacteristic)
|
||||
val descriptor = gattCharacteristic.getDescriptor(descClientCharConfirmation)
|
||||
val bleInputPropertyEnum: BleInputProperty =
|
||||
BleInputProperty.values().first { it.value == bleInputProperty }
|
||||
@@ -230,8 +231,13 @@ fun BluetoothGatt.setNotifiable(
|
||||
BleInputProperty.Indication -> BluetoothGattDescriptor.ENABLE_INDICATION_VALUE to true
|
||||
else -> BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE to false
|
||||
}
|
||||
descriptor.value = value
|
||||
setCharacteristicNotification(descriptor.characteristic, enable) && writeDescriptor(descriptor)
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||
writeDescriptor(descriptor, value)
|
||||
} else {
|
||||
descriptor.value = value
|
||||
writeDescriptor(descriptor)
|
||||
}
|
||||
return setCharacteristicNotification(descriptor.characteristic, enable)
|
||||
}
|
||||
|
||||
fun BluetoothDevice.removeBond() {
|
||||
@@ -303,4 +309,11 @@ class WriteResultFuture(
|
||||
val characteristicId: String,
|
||||
val serviceId: String,
|
||||
val result: (Result<Unit>) -> Unit,
|
||||
)
|
||||
|
||||
class CharacteristicSubscriptionFuture(
|
||||
val deviceId: String,
|
||||
val characteristicId: String,
|
||||
val serviceId: String,
|
||||
val result: (Result<Unit>) -> Unit,
|
||||
)
|
||||
@@ -17,13 +17,11 @@ import android.content.IntentFilter
|
||||
import android.os.Build
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.os.ParcelUuid
|
||||
import android.util.Log
|
||||
import io.flutter.embedding.engine.plugins.FlutterPlugin
|
||||
import io.flutter.embedding.engine.plugins.activity.ActivityAware
|
||||
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
|
||||
import io.flutter.plugin.common.*
|
||||
import java.util.*
|
||||
import java.util.concurrent.CountDownLatch
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
@@ -42,6 +40,8 @@ class UniversalBlePlugin : UniversalBlePlatformChannel, BluetoothGattCallback(),
|
||||
private val mtuResultFutureList = mutableListOf<MtuResultFuture>()
|
||||
private val bleCharacteristicFutureList = mutableListOf<BleCharacteristicFuture>()
|
||||
private val discoverServicesFutureList = mutableListOf<DiscoverServicesFuture>()
|
||||
private val characteristicSubscriptionFutureList =
|
||||
mutableListOf<CharacteristicSubscriptionFuture>()
|
||||
private val writeResultFutureList = mutableListOf<WriteResultFuture>()
|
||||
private val cachedServicesMap = mutableMapOf<String, List<String>>()
|
||||
private val devicesStateMap = mutableMapOf<String, Int>()
|
||||
@@ -56,7 +56,7 @@ class UniversalBlePlugin : UniversalBlePlatformChannel, BluetoothGattCallback(),
|
||||
|
||||
val intentFilter = IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED)
|
||||
intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED)
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||
context.registerReceiver(broadcastReceiver, intentFilter, RECEIVER_EXPORTED)
|
||||
} else {
|
||||
context.registerReceiver(broadcastReceiver, intentFilter)
|
||||
@@ -225,15 +225,50 @@ class UniversalBlePlugin : UniversalBlePlatformChannel, BluetoothGattCallback(),
|
||||
service: String,
|
||||
characteristic: String,
|
||||
bleInputProperty: Long,
|
||||
callback: (Result<Unit>) -> Unit,
|
||||
) {
|
||||
val gatt = deviceId.toBluetoothGatt()
|
||||
val gattCharacteristic = gatt.getCharacteristic(service, characteristic)
|
||||
?: throw FlutterError(
|
||||
"IllegalArgument",
|
||||
"Unknown characteristic: $characteristic",
|
||||
null
|
||||
try {
|
||||
val gatt = deviceId.toBluetoothGatt()
|
||||
val gattCharacteristic = gatt.getCharacteristic(service, characteristic)
|
||||
?: throw FlutterError(
|
||||
"IllegalArgument",
|
||||
"Unknown characteristic: $characteristic",
|
||||
null
|
||||
)
|
||||
|
||||
if (gatt.setNotifiable(gattCharacteristic, bleInputProperty)) {
|
||||
characteristicSubscriptionFutureList.add(
|
||||
CharacteristicSubscriptionFuture(
|
||||
gatt.device.address,
|
||||
gattCharacteristic.uuid.toString(),
|
||||
gattCharacteristic.service.uuid.toString(),
|
||||
callback
|
||||
)
|
||||
)
|
||||
} else {
|
||||
callback(
|
||||
Result.failure(
|
||||
FlutterError(
|
||||
"Failed",
|
||||
"Failed to update subscription state",
|
||||
null
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
} catch (e: FlutterError) {
|
||||
callback(Result.failure(e))
|
||||
} catch (e: Exception) {
|
||||
callback(
|
||||
Result.failure(
|
||||
FlutterError(
|
||||
"Failed",
|
||||
"Failed to update subscription state",
|
||||
e.toString()
|
||||
)
|
||||
)
|
||||
)
|
||||
gatt.setNotifiable(gattCharacteristic, bleInputProperty)
|
||||
}
|
||||
}
|
||||
|
||||
override fun readValue(
|
||||
@@ -268,6 +303,16 @@ class UniversalBlePlugin : UniversalBlePlatformChannel, BluetoothGattCallback(),
|
||||
)
|
||||
} catch (e: FlutterError) {
|
||||
callback(Result.failure(e))
|
||||
} catch (e: Exception) {
|
||||
callback(
|
||||
Result.failure(
|
||||
FlutterError(
|
||||
"Failed",
|
||||
"Failed to read value",
|
||||
e.toString()
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -300,16 +345,6 @@ class UniversalBlePlugin : UniversalBlePlatformChannel, BluetoothGattCallback(),
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCharacteristicRead(
|
||||
gatt: BluetoothGatt,
|
||||
characteristic: BluetoothGattCharacteristic,
|
||||
status: Int,
|
||||
) {
|
||||
// Pass data to new api
|
||||
onCharacteristicRead(gatt, characteristic, characteristic.value, status)
|
||||
}
|
||||
|
||||
|
||||
override fun writeValue(
|
||||
deviceId: String,
|
||||
service: String,
|
||||
@@ -356,18 +391,23 @@ class UniversalBlePlugin : UniversalBlePlatformChannel, BluetoothGattCallback(),
|
||||
}
|
||||
writeType = BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE
|
||||
}
|
||||
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||
// val result = gatt.writeCharacteristic(gattCharacteristic, value, writeType)
|
||||
// Log.v(TAG,"writeResult $characteristic: $result => ${result.parseBluetoothStatusCodeError()}")
|
||||
// return
|
||||
// }
|
||||
gattCharacteristic.value = value
|
||||
gattCharacteristic.writeType = writeType
|
||||
val result = gatt.writeCharacteristic(gattCharacteristic)
|
||||
|
||||
val result = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||
val writeResult = gatt.writeCharacteristic(gattCharacteristic, value, writeType)
|
||||
writeResult == BluetoothGatt.GATT_SUCCESS
|
||||
} else {
|
||||
@Suppress("DEPRECATION")
|
||||
gattCharacteristic.value = value
|
||||
gattCharacteristic.writeType = writeType
|
||||
@Suppress("DEPRECATION")
|
||||
gatt.writeCharacteristic(gattCharacteristic)
|
||||
}
|
||||
|
||||
if (!result) {
|
||||
callback(Result.failure(FlutterError("Failed", "Failed to write", null)))
|
||||
return
|
||||
}
|
||||
|
||||
if (writeType == BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT) {
|
||||
// wait for the result
|
||||
writeResultFutureList.add(
|
||||
@@ -535,13 +575,13 @@ class UniversalBlePlugin : UniversalBlePlatformChannel, BluetoothGattCallback(),
|
||||
}
|
||||
|
||||
// If its a known gatt, just discover services
|
||||
knownGatts.find { it.device.address == device.address }?.let {
|
||||
it.services?.let { services ->
|
||||
knownGatts.find { it.device.address == device.address }?.let { gatt ->
|
||||
gatt.services?.let { services ->
|
||||
updateCallback(services.map { service -> service.uuid.toString() })
|
||||
return
|
||||
}
|
||||
|
||||
if (it.discoverServices()) {
|
||||
if (gatt.discoverServices()) {
|
||||
discoverServicesFutureList.add(
|
||||
DiscoverServicesFuture(device.address) { uuids: Result<List<UniversalBleService>> ->
|
||||
if (uuids.isSuccess) {
|
||||
@@ -616,7 +656,16 @@ class UniversalBlePlugin : UniversalBlePlatformChannel, BluetoothGattCallback(),
|
||||
}
|
||||
} else if (intent.action == BluetoothDevice.ACTION_BOND_STATE_CHANGED) {
|
||||
val device: BluetoothDevice =
|
||||
intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE) ?: return
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||
intent.getParcelableExtra(
|
||||
BluetoothDevice.EXTRA_DEVICE,
|
||||
BluetoothDevice::class.java
|
||||
)
|
||||
?: return
|
||||
} else {
|
||||
@Suppress("DEPRECATION")
|
||||
intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE) ?: return
|
||||
}
|
||||
// get pairing failed error
|
||||
when (intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR)) {
|
||||
BluetoothDevice.BOND_BONDING -> {
|
||||
@@ -665,7 +714,7 @@ class UniversalBlePlugin : UniversalBlePlatformChannel, BluetoothGattCallback(),
|
||||
|
||||
override fun onScanResult(callbackType: Int, result: ScanResult) {
|
||||
// Log.v(TAG, "onScanResult: $result")
|
||||
var serviceUuids: Array<String> = arrayOf<String>()
|
||||
var serviceUuids: Array<String> = arrayOf()
|
||||
result.device.uuids?.forEach {
|
||||
serviceUuids += it.uuid.toString()
|
||||
}
|
||||
@@ -687,7 +736,7 @@ class UniversalBlePlugin : UniversalBlePlatformChannel, BluetoothGattCallback(),
|
||||
) {}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
override fun onBatchScanResults(results: MutableList<ScanResult>?) {
|
||||
Log.v(TAG, "onBatchScanResults: $results")
|
||||
}
|
||||
@@ -717,16 +766,62 @@ class UniversalBlePlugin : UniversalBlePlatformChannel, BluetoothGattCallback(),
|
||||
override fun onCharacteristicChanged(
|
||||
gatt: BluetoothGatt,
|
||||
characteristic: BluetoothGattCharacteristic,
|
||||
value: ByteArray,
|
||||
) {
|
||||
mainThreadHandler?.post {
|
||||
callbackChannel?.onValueChanged(
|
||||
deviceIdArg = gatt.device.address,
|
||||
characteristicIdArg = characteristic.uuid.toString(),
|
||||
valueArg = characteristic.value
|
||||
valueArg = value
|
||||
) {}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDescriptorWrite(
|
||||
gatt: BluetoothGatt?,
|
||||
descriptor: BluetoothGattDescriptor?,
|
||||
status: Int,
|
||||
) {
|
||||
super.onDescriptorWrite(gatt, descriptor, status)
|
||||
if (descriptor?.uuid.toString() == ccdCharacteristic) {
|
||||
val char: String? = descriptor?.characteristic?.uuid?.toString()
|
||||
val service: String? = descriptor?.characteristic?.service?.uuid?.toString()
|
||||
val deviceId: String? = gatt?.device?.address;
|
||||
if (deviceId != null && char != null && service != null) {
|
||||
updateSubscriptionState(deviceId, char, service, status)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateSubscriptionState(
|
||||
deviceId: String,
|
||||
characteristic: String,
|
||||
service: String,
|
||||
status: Int,
|
||||
) {
|
||||
characteristicSubscriptionFutureList.filter {
|
||||
it.deviceId == deviceId &&
|
||||
it.characteristicId == characteristic &&
|
||||
it.serviceId == service
|
||||
}.forEach {
|
||||
characteristicSubscriptionFutureList.remove(it)
|
||||
val error: String? = status.parseGattErrorCode()
|
||||
if (error != null) {
|
||||
it.result(
|
||||
Result.failure(
|
||||
FlutterError(
|
||||
status.toString(),
|
||||
"Failed to update subscription state: $error",
|
||||
null,
|
||||
)
|
||||
)
|
||||
)
|
||||
} else {
|
||||
it.result(Result.success(Unit))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun isBluetoothAvailable(): Boolean {
|
||||
return bluetoothManager.adapter.isEnabled
|
||||
}
|
||||
@@ -750,6 +845,24 @@ class UniversalBlePlugin : UniversalBlePlatformChannel, BluetoothGattCallback(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Depreciated Members, ( Requires to support older android devices )
|
||||
@Suppress("OVERRIDE_DEPRECATION", "DEPRECATION")
|
||||
override fun onCharacteristicRead(
|
||||
gatt: BluetoothGatt,
|
||||
characteristic: BluetoothGattCharacteristic,
|
||||
status: Int,
|
||||
) {
|
||||
onCharacteristicRead(gatt, characteristic, characteristic.value, status)
|
||||
}
|
||||
|
||||
@Suppress("OVERRIDE_DEPRECATION", "DEPRECATION")
|
||||
override fun onCharacteristicChanged(
|
||||
gatt: BluetoothGatt,
|
||||
characteristic: BluetoothGattCharacteristic,
|
||||
) {
|
||||
onCharacteristicChanged(gatt, characteristic, characteristic.value)
|
||||
}
|
||||
|
||||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?): Boolean {
|
||||
if (requestCode == bluetoothEnableRequestCode) {
|
||||
val future = bluetoothEnableRequestFuture ?: return false
|
||||
|
||||
Reference in New Issue
Block a user