feat: 支持获取 maximumWrtieLength (#14)

This commit is contained in:
Mr剑侠客
2023-09-08 15:25:59 +08:00
committed by GitHub
parent 219bd73c33
commit 78bcb88563
28 changed files with 417 additions and 112 deletions

View File

@ -278,6 +278,7 @@ interface MyCentralControllerHostApi {
fun stopDiscovery()
fun connect(myPeripheralKey: Long, callback: (Result<Unit>) -> Unit)
fun disconnect(myPeripheralKey: Long, callback: (Result<Unit>) -> Unit)
fun getMaximumWriteLength(myPeripheralKey: Long, callback: (Result<Long>) -> Unit)
fun discoverGATT(myPeripheralKey: Long, callback: (Result<Unit>) -> Unit)
fun getServices(myPeripheralKey: Long): List<MyGattServiceArgs>
fun getCharacteristics(myServiceKey: Long): List<MyGattCharacteristicArgs>
@ -403,6 +404,26 @@ interface MyCentralControllerHostApi {
channel.setMessageHandler(null)
}
}
run {
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.bluetooth_low_energy_android.MyCentralControllerHostApi.getMaximumWriteLength", codec)
if (api != null) {
channel.setMessageHandler { message, reply ->
val args = message as List<Any?>
val myPeripheralKeyArg = args[0].let { if (it is Int) it.toLong() else it as Long }
api.getMaximumWriteLength(myPeripheralKeyArg) { result: Result<Long> ->
val error = result.exceptionOrNull()
if (error != null) {
reply.reply(wrapError(error))
} else {
val data = result.getOrNull()
reply.reply(wrapResult(data))
}
}
}
} else {
channel.setMessageHandler(null)
}
}
run {
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.bluetooth_low_energy_android.MyCentralControllerHostApi.discoverGATT", codec)
if (api != null) {

View File

@ -15,6 +15,13 @@ class MyBluetoothGattCallback(private val myCentralController: MyCentralControll
}
}
override fun onMtuChanged(gatt: BluetoothGatt, mtu: Int, status: Int) {
super.onMtuChanged(gatt, mtu, status)
executor.execute {
myCentralController.onMtuChanged(gatt, mtu, status)
}
}
override fun onServicesDiscovered(gatt: BluetoothGatt, status: Int) {
super.onServicesDiscovered(gatt, status)
executor.execute {

View File

@ -59,6 +59,7 @@ class MyCentralController(private val context: Context, binaryMessenger: BinaryM
private var startDiscoveryCallback: ((Result<Unit>) -> Unit)? = null
private val connectCallbacks = mutableMapOf<Int, (Result<Unit>) -> Unit>()
private val disconnectCallbacks = mutableMapOf<Int, (Result<Unit>) -> Unit>()
private val getMaximumWriteLengthCallbacks = mutableMapOf<Int, (Result<Long>) -> Unit>()
private val discoverGattCallbacks = mutableMapOf<Int, (Result<Unit>) -> Unit>()
private val readCharacteristicCallbacks = mutableMapOf<Int, (Result<ByteArray>) -> Unit>()
private val writeCharacteristicCallbacks = mutableMapOf<Int, (Result<Unit>) -> Unit>()
@ -179,6 +180,24 @@ class MyCentralController(private val context: Context, binaryMessenger: BinaryM
}
}
override fun getMaximumWriteLength(myPeripheralKey: Long, callback: (Result<Long>) -> Unit) {
try {
val deviceKey = myPeripheralKey.toInt()
val unfinishedCallback = getMaximumWriteLengthCallbacks[deviceKey]
if (unfinishedCallback != null) {
throw IllegalStateException()
}
val gatt = cachedGATTs[deviceKey] as BluetoothGatt
val requesting = gatt.requestMtu(512)
if (!requesting) {
throw IllegalStateException()
}
getMaximumWriteLengthCallbacks[deviceKey] = callback
} catch (e: Throwable) {
callback(Result.failure(e))
}
}
override fun discoverGATT(myPeripheralKey: Long, callback: (Result<Unit>) -> Unit) {
try {
val deviceKey = myPeripheralKey.toInt()
@ -493,6 +512,19 @@ class MyCentralController(private val context: Context, binaryMessenger: BinaryM
}
}
fun onMtuChanged(gatt: BluetoothGatt, mtu: Int, status: Int) {
val device = gatt.device
val deviceKey = device.hashCode()
val callback = getMaximumWriteLengthCallbacks.remove(deviceKey) ?: return
if (status == BluetoothGatt.GATT_SUCCESS) {
val maximumWriteLength = (mtu - 3).toLong()
callback(Result.success(maximumWriteLength))
} else {
val error = IllegalStateException("Get maximum write length failed with status: $status")
callback(Result.failure(error))
}
}
fun onServicesDiscovered(gatt: BluetoothGatt, status: Int) {
val device = gatt.device
val deviceKey = device.hashCode()
@ -609,7 +641,8 @@ private val ScanResult.myAdvertisementArgs: MyAdvertisementArgs
} else {
val name = record.deviceName
val manufacturerSpecificData = record.manufacturerSpecificData.toMyArgs()
val serviceUUIDs = record.serviceUuids?.map { uuid -> uuid.toString() } ?: emptyList()
val serviceUUIDs = record.serviceUuids?.map { uuid -> uuid.toString() }
?: emptyList()
val pairs = record.serviceData.entries.map { (uuid, value) ->
val key = uuid.toString()
return@map Pair(key, value)