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

@ -1,9 +1,13 @@
## 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.

View File

@ -254,6 +254,7 @@ protocol MyCentralControllerHostApi {
func stopDiscovery() throws
func connect(myPeripheralKey: Int64, completion: @escaping (Result<Void, Error>) -> Void)
func disconnect(myPeripheralKey: Int64, completion: @escaping (Result<Void, Error>) -> Void)
func getMaximumWriteLength(myPeripheralKey: Int64, myTypeNumber: Int64) throws -> Int64
func discoverGATT(myPeripheralKey: Int64, completion: @escaping (Result<Void, Error>) -> Void)
func getServices(myPeripheralKey: Int64) throws -> [MyGattServiceArgs]
func getCharacteristics(myServiceKey: Int64) throws -> [MyGattCharacteristicArgs]
@ -359,6 +360,22 @@ class MyCentralControllerHostApiSetup {
} else {
disconnectChannel.setMessageHandler(nil)
}
let getMaximumWriteLengthChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.bluetooth_low_energy_darwin.MyCentralControllerHostApi.getMaximumWriteLength", binaryMessenger: binaryMessenger, codec: codec)
if let api = api {
getMaximumWriteLengthChannel.setMessageHandler { message, reply in
let args = message as! [Any?]
let myPeripheralKeyArg = args[0] is Int64 ? args[0] as! Int64 : Int64(args[0] as! Int32)
let myTypeNumberArg = args[1] is Int64 ? args[1] as! Int64 : Int64(args[1] as! Int32)
do {
let result = try api.getMaximumWriteLength(myPeripheralKey: myPeripheralKeyArg, myTypeNumber: myTypeNumberArg)
reply(wrapResult(result))
} catch {
reply(wrapError(error))
}
}
} else {
getMaximumWriteLengthChannel.setMessageHandler(nil)
}
let discoverGATTChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.bluetooth_low_energy_darwin.MyCentralControllerHostApi.discoverGATT", binaryMessenger: binaryMessenger, codec: codec)
if let api = api {
discoverGATTChannel.setMessageHandler { message, reply in

View File

@ -122,6 +122,21 @@ class MyCentralController: MyCentralControllerHostApi {
}
}
func getMaximumWriteLength(myPeripheralKey: Int64, myTypeNumber: Int64) throws -> Int64 {
let peripheralKey = Int(myPeripheralKey)
guard let peripheral = cachedPeripherals[peripheralKey] else {
throw MyError.illegalArgument
}
let myTypeRawValue = Int(myTypeNumber)
guard let myTypeArgs = MyGattCharacteristicWriteTypeArgs(rawValue: myTypeRawValue) else {
throw MyError.illegalArgument
}
let type = myTypeArgs.toType()
let maximumWriteLength32 = peripheral.maximumWriteValueLength(for: type)
let maximumWriteLength = Int64(maximumWriteLength32)
return maximumWriteLength
}
func discoverGATT(myPeripheralKey: Int64, completion: @escaping (Result<Void, Error>) -> Void) {
do {
let peripheralKey = Int(myPeripheralKey)

View File

@ -380,6 +380,33 @@ class MyCentralControllerHostApi {
}
}
Future<int> getMaximumWriteLength(int arg_myPeripheralKey, int arg_myTypeNumber) async {
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
'dev.flutter.pigeon.bluetooth_low_energy_darwin.MyCentralControllerHostApi.getMaximumWriteLength', codec,
binaryMessenger: _binaryMessenger);
final List<Object?>? replyList =
await channel.send(<Object?>[arg_myPeripheralKey, arg_myTypeNumber]) 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_darwin.MyCentralControllerHostApi.discoverGATT', codec,

View File

@ -114,6 +114,22 @@ 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 myTypeArgs = type.toMyArgs();
final myTypeNumber = myTypeArgs.index;
final maximumWriteLength = await _myApi.getMaximumWriteLength(
myPeripheral.hashCode,
myTypeNumber,
);
return maximumWriteLength;
}
@override
Future<void> discoverGATT(Peripheral peripheral) async {
await _throwWithoutState(CentralState.poweredOn);
@ -206,14 +222,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,
);
}

View File

@ -19,6 +19,7 @@ abstract class MyCentralControllerHostApi {
void connect(int myPeripheralKey);
@async
void disconnect(int myPeripheralKey);
int getMaximumWriteLength(int myPeripheralKey, int myTypeNumber);
@async
void discoverGATT(int myPeripheralKey);
List<MyGattServiceArgs> getServices(int myPeripheralKey);

View File

@ -1,6 +1,6 @@
name: bluetooth_low_energy_darwin
description: iOS and macOS 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: