feat: 支持获取 maximumWrtieLength (#14)
This commit is contained in:
@ -1,20 +1,24 @@
|
||||
## 2.2.0
|
||||
|
||||
* Add `CentralController#getMaximumWriteLength` method.
|
||||
|
||||
## 2.0.3
|
||||
|
||||
- `Android` Migrate to Android 13.
|
||||
- `Android` Fix the issuce that receive wrong values caused by unsafe memory, see https://developer.android.com/reference/android/bluetooth/BluetoothGattCallback#onCharacteristicChanged(android.bluetooth.BluetoothGatt,%20android.bluetooth.BluetoothGattCharacteristic)
|
||||
* `Android` Migrate to Android 13.
|
||||
* `Android` Fix the issuce that receive wrong values caused by unsafe memory, see https://developer.android.com/reference/android/bluetooth/BluetoothGattCallback#onCharacteristicChanged(android.bluetooth.BluetoothGatt,%20android.bluetooth.BluetoothGattCharacteristic)
|
||||
|
||||
## 2.0.2
|
||||
|
||||
- Combine iOS and macOS projects.
|
||||
- Optimize project structure.
|
||||
* Combine iOS and macOS projects.
|
||||
* Optimize project structure.
|
||||
|
||||
## 2.0.1
|
||||
|
||||
- Fix the issue that GATTs is cleared after peripheral disconnected on iOS and macOS.
|
||||
- Fix the issue that create UUID form peripheral's address failed on Linux.
|
||||
- Fix the issue that instance match failed on Linux.
|
||||
* Fix the issue that GATTs is cleared after peripheral disconnected on iOS and macOS.
|
||||
* Fix the issue that create UUID form peripheral's address failed on Linux.
|
||||
* Fix the issue that instance match failed on Linux.
|
||||
|
||||
## 2.0.0
|
||||
|
||||
- Rewrite the whole project with federated plugins.
|
||||
- Support macOS and Linux.
|
||||
* Rewrite the whole project with federated plugins.
|
||||
* Support macOS and Linux.
|
||||
|
@ -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) {
|
||||
|
@ -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 {
|
||||
|
@ -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)
|
||||
|
@ -380,6 +380,33 @@ class MyCentralControllerHostApi {
|
||||
}
|
||||
}
|
||||
|
||||
Future<int> getMaximumWriteLength(int arg_myPeripheralKey) async {
|
||||
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
|
||||
'dev.flutter.pigeon.bluetooth_low_energy_android.MyCentralControllerHostApi.getMaximumWriteLength', codec,
|
||||
binaryMessenger: _binaryMessenger);
|
||||
final List<Object?>? replyList =
|
||||
await channel.send(<Object?>[arg_myPeripheralKey]) as List<Object?>?;
|
||||
if (replyList == null) {
|
||||
throw PlatformException(
|
||||
code: 'channel-error',
|
||||
message: 'Unable to establish connection on channel.',
|
||||
);
|
||||
} else if (replyList.length > 1) {
|
||||
throw PlatformException(
|
||||
code: replyList[0]! as String,
|
||||
message: replyList[1] as String?,
|
||||
details: replyList[2],
|
||||
);
|
||||
} else if (replyList[0] == null) {
|
||||
throw PlatformException(
|
||||
code: 'null-error',
|
||||
message: 'Host platform returned null value for non-null return value.',
|
||||
);
|
||||
} else {
|
||||
return (replyList[0] as int?)!;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> discoverGATT(int arg_myPeripheralKey) async {
|
||||
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
|
||||
'dev.flutter.pigeon.bluetooth_low_energy_android.MyCentralControllerHostApi.discoverGATT', codec,
|
||||
|
@ -114,6 +114,19 @@ class MyCentralController extends CentralController
|
||||
await _myApi.disconnect(myPeripheral.hashCode);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<int> getMaximumWriteLength(
|
||||
Peripheral peripheral, {
|
||||
required GattCharacteristicWriteType type,
|
||||
}) async {
|
||||
await _throwWithoutState(CentralState.poweredOn);
|
||||
final myPeripheral = peripheral as MyPeripheral;
|
||||
final maximumWriteLength = await _myApi.getMaximumWriteLength(
|
||||
myPeripheral.hashCode,
|
||||
);
|
||||
return maximumWriteLength;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> discoverGATT(Peripheral peripheral) async {
|
||||
await _throwWithoutState(CentralState.poweredOn);
|
||||
@ -205,14 +218,14 @@ class MyCentralController extends CentralController
|
||||
final myCharacteristic = characteristic as MyGattCharacteristic;
|
||||
final myService = myCharacteristic.myService;
|
||||
final myPeripheral = myService.myPeripheral;
|
||||
final typeArgs = type.toMyArgs();
|
||||
final typeNumber = typeArgs.index;
|
||||
final myTypeArgs = type.toMyArgs();
|
||||
final myTypeNumber = myTypeArgs.index;
|
||||
await _myApi.writeCharacteristic(
|
||||
myPeripheral.hashCode,
|
||||
myService.hashCode,
|
||||
myCharacteristic.hashCode,
|
||||
value,
|
||||
typeNumber,
|
||||
myTypeNumber,
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -24,6 +24,8 @@ abstract class MyCentralControllerHostApi {
|
||||
@async
|
||||
void disconnect(int myPeripheralKey);
|
||||
@async
|
||||
int getMaximumWriteLength(int myPeripheralKey);
|
||||
@async
|
||||
void discoverGATT(int myPeripheralKey);
|
||||
List<MyGattServiceArgs> getServices(int myPeripheralKey);
|
||||
List<MyGattCharacteristicArgs> getCharacteristics(int myServiceKey);
|
||||
|
@ -1,6 +1,6 @@
|
||||
name: bluetooth_low_energy_android
|
||||
description: Android implementation of the bluetooth_low_energy plugin.
|
||||
version: 2.0.3
|
||||
version: 2.2.0
|
||||
homepage: https://github.com/yanshouwang/bluetooth_low_energy
|
||||
|
||||
environment:
|
||||
@ -10,7 +10,7 @@ environment:
|
||||
dependencies:
|
||||
flutter:
|
||||
sdk: flutter
|
||||
bluetooth_low_energy_platform_interface: ^2.0.3
|
||||
bluetooth_low_energy_platform_interface: ^2.2.0
|
||||
|
||||
dev_dependencies:
|
||||
flutter_test:
|
||||
|
Reference in New Issue
Block a user