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,51 +1,55 @@
## 2.2.0
* Add `CentralController#getMaximumWriteLength` method.
## 2.0.3 ## 2.0.3
- `Android` Migrate to Android 13. * `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` 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 ## 2.0.2
- Combine iOS and macOS projects. * Combine iOS and macOS projects.
- Optimize project structure. * Optimize project structure.
## 2.0.1 ## 2.0.1
- Fix the issue that GATTs is cleared after peripheral disconnected on iOS and macOS. * 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 create UUID form peripheral's address failed on Linux.
- Fix the issue that instance match failed on Linux. * Fix the issue that instance match failed on Linux.
## 2.0.0 ## 2.0.0
- Rewrite the whole project with federated plugins. * Rewrite the whole project with federated plugins.
- Support macOS and Linux. * Support macOS and Linux.
## 1.1.0 ## 1.1.0
- Fix the crash by onMtuChanged called multi-times on Android. * Fix the crash by onMtuChanged called multi-times on Android.
- Fix the finalizer doesn't work issue. * Fix the finalizer doesn't work issue.
- Make some break changes. * Make some break changes.
## 1.0.0 ## 1.0.0
- Upgrade to flutter 3.x. * Upgrade to flutter 3.x.
- Rewrite the whole project with pigeon. * Rewrite the whole project with pigeon.
## 0.1.0 ## 0.1.0
- Add implementations on iOS. * Add implementations on iOS.
- Combine available and state for Bluetooth. * Combine available and state for Bluetooth.
- Add connectable for Discovery. * Add connectable for Discovery.
- Add maximumWriteLength for GATT. * Add maximumWriteLength for GATT.
## 0.0.2 ## 0.0.2
- Fix connect blocked when bluetooth closed. * Fix connect blocked when bluetooth closed.
- Fix wrong repository url. * Fix wrong repository url.
- Move all example files to main.dart. * Move all example files to main.dart.
## 0.0.1 ## 0.0.1
- Add central APIs. * Add central APIs.
- Add implementations on Android. * Add implementations on Android.
- Add example. * Add example.
- Add test. * Add test.

View File

@ -162,10 +162,10 @@ class _HomeViewState extends State<HomeView> {
return ValueListenableBuilder( return ValueListenableBuilder(
valueListenable: discoveredEventArgs, valueListenable: discoveredEventArgs,
builder: (context, discoveredEventArgs, child) { builder: (context, discoveredEventArgs, child) {
// final items = discoveredEventArgs // final items = discoveredEventArgs;
// .where((eventArgs) => eventArgs.advertisement.name != null) final items = discoveredEventArgs
// .toList(); .where((eventArgs) => eventArgs.advertisement.name != null)
final items = discoveredEventArgs; .toList();
return ListView.separated( return ListView.separated(
itemBuilder: (context, i) { itemBuilder: (context, i) {
final theme = Theme.of(context); final theme = Theme.of(context);
@ -303,8 +303,10 @@ class _PeripheralViewState extends State<PeripheralView> {
late final ValueNotifier<List<GattCharacteristic>> characteristics; late final ValueNotifier<List<GattCharacteristic>> characteristics;
late final ValueNotifier<GattService?> service; late final ValueNotifier<GattService?> service;
late final ValueNotifier<GattCharacteristic?> characteristic; late final ValueNotifier<GattCharacteristic?> characteristic;
late final TextEditingController writeController; late final ValueNotifier<GattCharacteristicWriteType> writeType;
late final ValueNotifier<int> maximumWriteLength;
late final ValueNotifier<List<Log>> logs; late final ValueNotifier<List<Log>> logs;
late final TextEditingController writeController;
late final StreamSubscription stateChangedSubscription; late final StreamSubscription stateChangedSubscription;
late final StreamSubscription valueChangedSubscription; late final StreamSubscription valueChangedSubscription;
@ -317,8 +319,10 @@ class _PeripheralViewState extends State<PeripheralView> {
characteristics = ValueNotifier([]); characteristics = ValueNotifier([]);
service = ValueNotifier(null); service = ValueNotifier(null);
characteristic = ValueNotifier(null); characteristic = ValueNotifier(null);
writeController = TextEditingController(); writeType = ValueNotifier(GattCharacteristicWriteType.withResponse);
maximumWriteLength = ValueNotifier(20);
logs = ValueNotifier([]); logs = ValueNotifier([]);
writeController = TextEditingController();
stateChangedSubscription = centralController.peripheralStateChanged.listen( stateChangedSubscription = centralController.peripheralStateChanged.listen(
(eventArgs) { (eventArgs) {
if (eventArgs.peripheral != this.eventArgs.peripheral) { if (eventArgs.peripheral != this.eventArgs.peripheral) {
@ -519,6 +523,64 @@ class _PeripheralViewState extends State<PeripheralView> {
}, },
), ),
), ),
Row(
children: [
Expanded(
child: Center(
child: ValueListenableBuilder(
valueListenable: writeType,
builder: (context, writeType, child) {
final items =
GattCharacteristicWriteType.values.map((type) {
return DropdownMenuItem(
value: type,
child: Text(
type.name,
style: theme.textTheme.bodyMedium,
),
);
}).toList();
return DropdownButton(
items: items,
onChanged: (type) {
if (type == null) {
return;
}
this.writeType.value = type;
},
value: writeType,
underline: const Offstage(),
);
},
),
),
),
Expanded(
child: ValueListenableBuilder(
valueListenable: state,
builder: (context, state, child) {
return TextButton(
onPressed: state
? () async {
maximumWriteLength.value =
await centralController.getMaximumWriteLength(
eventArgs.peripheral,
type: writeType.value,
);
}
: null,
child: ValueListenableBuilder(
valueListenable: maximumWriteLength,
builder: (context, maximumWriteLength, child) {
return Text('MTU: $maximumWriteLength');
},
),
);
},
),
),
],
),
Container( Container(
margin: const EdgeInsets.symmetric(vertical: 16.0), margin: const EdgeInsets.symmetric(vertical: 16.0),
height: 160.0, height: 160.0,
@ -626,6 +688,10 @@ class _PeripheralViewState extends State<PeripheralView> {
characteristics.dispose(); characteristics.dispose();
service.dispose(); service.dispose();
characteristic.dispose(); characteristic.dispose();
writeType.dispose();
maximumWriteLength.dispose();
logs.dispose();
writeController.dispose();
} }
} }

View File

@ -23,47 +23,47 @@ packages:
path: ".." path: ".."
relative: true relative: true
source: path source: path
version: "2.0.3" version: "2.2.0"
bluetooth_low_energy_android: bluetooth_low_energy_android:
dependency: transitive dependency: transitive
description: description:
name: bluetooth_low_energy_android name: bluetooth_low_energy_android
sha256: e740179e5143c74e2f308c78d395d49054c941b95eb776884d52192b80ccfcee sha256: fde30d9ef058f6859266a8e6d4218136958366de5978f69df2bb37d2b24679eb
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "2.0.3" version: "2.2.0"
bluetooth_low_energy_darwin: bluetooth_low_energy_darwin:
dependency: transitive dependency: transitive
description: description:
name: bluetooth_low_energy_darwin name: bluetooth_low_energy_darwin
sha256: "49e9dc08281fb25f91472252dfbdf7d1d6d94983bf2e746382ad4b2dd8ba05fe" sha256: "876f2d4a288739091296bc22ea574e64451f592154e4201b5999f174120a8b55"
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "2.0.3" version: "2.2.0"
bluetooth_low_energy_linux: bluetooth_low_energy_linux:
dependency: transitive dependency: transitive
description: description:
name: bluetooth_low_energy_linux name: bluetooth_low_energy_linux
sha256: d9576d89471e31b76cb2495dbb72aabaadc3aa093ad607f5aedb15518d5c20cb sha256: dc3062991e0a408941829326f0a3b2e1244a8138dca7f6c4c6beadbc26deecb2
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "2.0.3" version: "2.2.0"
bluetooth_low_energy_platform_interface: bluetooth_low_energy_platform_interface:
dependency: transitive dependency: transitive
description: description:
name: bluetooth_low_energy_platform_interface name: bluetooth_low_energy_platform_interface
sha256: dbc05f604e379ea7570db718a9ae7cf7fa08a20ee088a6f66de17b4acc7d9260 sha256: eecaa2c37ebd536339c292d5566cf4360c9ea3861188342791eb3a0cf65cc64c
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "2.0.3" version: "2.2.0"
bluetooth_low_energy_windows: bluetooth_low_energy_windows:
dependency: transitive dependency: transitive
description: description:
name: bluetooth_low_energy_windows name: bluetooth_low_energy_windows
sha256: c4d11b2769da214b5ea20f50980c00f3c3229450e46f9d075cd64298e5a27057 sha256: a1b5d5d5ad935f15618e6a86334c1ec682c40ffbe6a78173c3f19b67ff0edcfb
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "2.0.3" version: "2.2.0"
bluez: bluez:
dependency: transitive dependency: transitive
description: description:
@ -116,10 +116,10 @@ packages:
dependency: "direct main" dependency: "direct main"
description: description:
name: cupertino_icons name: cupertino_icons
sha256: e35129dc44c9118cee2a5603506d823bab99c68393879edb440e0090d07586be sha256: d57953e10f9f8327ce64a508a355f0b1ec902193f66288e8cb5070e7c47eeb2d
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "1.0.5" version: "1.0.6"
dbus: dbus:
dependency: transitive dependency: transitive
description: description:
@ -166,10 +166,10 @@ packages:
dependency: "direct dev" dependency: "direct dev"
description: description:
name: flutter_lints name: flutter_lints
sha256: "2118df84ef0c3ca93f96123a616ae8540879991b8b57af2f81b76a7ada49b2a4" sha256: a25a15ebbdfc33ab1cd26c63a6ee519df92338a9c10f122adda92938253bef04
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "2.0.2" version: "2.0.3"
flutter_test: flutter_test:
dependency: "direct dev" dependency: "direct dev"
description: flutter description: flutter
@ -253,10 +253,10 @@ packages:
dependency: transitive dependency: transitive
description: description:
name: plugin_platform_interface name: plugin_platform_interface
sha256: "43798d895c929056255600343db8f049921cbec94d31ec87f1dc5c16c01935dd" sha256: da3fdfeccc4d4ff2da8f8c556704c08f912542c5fb3cf2233ed75372384a034d
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "2.1.5" version: "2.1.6"
process: process:
dependency: transitive dependency: transitive
description: description:
@ -370,10 +370,10 @@ packages:
dependency: transitive dependency: transitive
description: description:
name: win32 name: win32
sha256: f2add6fa510d3ae152903412227bda57d0d5a8da61d2c39c1fb022c9429a41c0 sha256: "9e82a402b7f3d518fb9c02d0e9ae45952df31b9bf34d77baf19da2de03fc2aaa"
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "5.0.6" version: "5.0.7"
xml: xml:
dependency: transitive dependency: transitive
description: description:

View File

@ -1,6 +1,6 @@
name: bluetooth_low_energy name: bluetooth_low_energy
description: A Flutter plugin for controlling the bluetooth low energy. description: A Flutter plugin for controlling the bluetooth low energy.
version: 2.0.3 version: 2.2.0
homepage: https://github.com/yanshouwang/bluetooth_low_energy homepage: https://github.com/yanshouwang/bluetooth_low_energy
environment: environment:
@ -10,11 +10,11 @@ environment:
dependencies: dependencies:
flutter: flutter:
sdk: flutter sdk: flutter
bluetooth_low_energy_platform_interface: ^2.0.3 bluetooth_low_energy_platform_interface: ^2.2.0
bluetooth_low_energy_android: ^2.0.3 bluetooth_low_energy_android: ^2.2.0
bluetooth_low_energy_darwin: ^2.0.3 bluetooth_low_energy_darwin: ^2.2.0
bluetooth_low_energy_linux: ^2.0.3 bluetooth_low_energy_linux: ^2.2.0
bluetooth_low_energy_windows: ^2.0.3 bluetooth_low_energy_windows: ^2.2.0
dev_dependencies: dev_dependencies:
flutter_test: flutter_test:

View File

@ -1,20 +1,24 @@
## 2.2.0
* Add `CentralController#getMaximumWriteLength` method.
## 2.0.3 ## 2.0.3
- `Android` Migrate to Android 13. * `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` 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 ## 2.0.2
- Combine iOS and macOS projects. * Combine iOS and macOS projects.
- Optimize project structure. * Optimize project structure.
## 2.0.1 ## 2.0.1
- Fix the issue that GATTs is cleared after peripheral disconnected on iOS and macOS. * 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 create UUID form peripheral's address failed on Linux.
- Fix the issue that instance match failed on Linux. * Fix the issue that instance match failed on Linux.
## 2.0.0 ## 2.0.0
- Rewrite the whole project with federated plugins. * Rewrite the whole project with federated plugins.
- Support macOS and Linux. * Support macOS and Linux.

View File

@ -278,6 +278,7 @@ interface MyCentralControllerHostApi {
fun stopDiscovery() fun stopDiscovery()
fun connect(myPeripheralKey: Long, callback: (Result<Unit>) -> Unit) fun connect(myPeripheralKey: Long, callback: (Result<Unit>) -> Unit)
fun disconnect(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 discoverGATT(myPeripheralKey: Long, callback: (Result<Unit>) -> Unit)
fun getServices(myPeripheralKey: Long): List<MyGattServiceArgs> fun getServices(myPeripheralKey: Long): List<MyGattServiceArgs>
fun getCharacteristics(myServiceKey: Long): List<MyGattCharacteristicArgs> fun getCharacteristics(myServiceKey: Long): List<MyGattCharacteristicArgs>
@ -403,6 +404,26 @@ interface MyCentralControllerHostApi {
channel.setMessageHandler(null) 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 { run {
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.bluetooth_low_energy_android.MyCentralControllerHostApi.discoverGATT", codec) val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.bluetooth_low_energy_android.MyCentralControllerHostApi.discoverGATT", codec)
if (api != null) { 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) { override fun onServicesDiscovered(gatt: BluetoothGatt, status: Int) {
super.onServicesDiscovered(gatt, status) super.onServicesDiscovered(gatt, status)
executor.execute { executor.execute {

View File

@ -59,6 +59,7 @@ class MyCentralController(private val context: Context, binaryMessenger: BinaryM
private var startDiscoveryCallback: ((Result<Unit>) -> Unit)? = null private var startDiscoveryCallback: ((Result<Unit>) -> Unit)? = null
private val connectCallbacks = mutableMapOf<Int, (Result<Unit>) -> Unit>() private val connectCallbacks = mutableMapOf<Int, (Result<Unit>) -> Unit>()
private val disconnectCallbacks = 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 discoverGattCallbacks = mutableMapOf<Int, (Result<Unit>) -> Unit>()
private val readCharacteristicCallbacks = mutableMapOf<Int, (Result<ByteArray>) -> Unit>() private val readCharacteristicCallbacks = mutableMapOf<Int, (Result<ByteArray>) -> Unit>()
private val writeCharacteristicCallbacks = mutableMapOf<Int, (Result<Unit>) -> 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) { override fun discoverGATT(myPeripheralKey: Long, callback: (Result<Unit>) -> Unit) {
try { try {
val deviceKey = myPeripheralKey.toInt() 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) { fun onServicesDiscovered(gatt: BluetoothGatt, status: Int) {
val device = gatt.device val device = gatt.device
val deviceKey = device.hashCode() val deviceKey = device.hashCode()
@ -609,7 +641,8 @@ private val ScanResult.myAdvertisementArgs: MyAdvertisementArgs
} else { } else {
val name = record.deviceName val name = record.deviceName
val manufacturerSpecificData = record.manufacturerSpecificData.toMyArgs() 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 pairs = record.serviceData.entries.map { (uuid, value) ->
val key = uuid.toString() val key = uuid.toString()
return@map Pair(key, value) return@map Pair(key, value)

View File

@ -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 { Future<void> discoverGATT(int arg_myPeripheralKey) async {
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>( final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
'dev.flutter.pigeon.bluetooth_low_energy_android.MyCentralControllerHostApi.discoverGATT', codec, 'dev.flutter.pigeon.bluetooth_low_energy_android.MyCentralControllerHostApi.discoverGATT', codec,

View File

@ -114,6 +114,19 @@ class MyCentralController extends CentralController
await _myApi.disconnect(myPeripheral.hashCode); 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 @override
Future<void> discoverGATT(Peripheral peripheral) async { Future<void> discoverGATT(Peripheral peripheral) async {
await _throwWithoutState(CentralState.poweredOn); await _throwWithoutState(CentralState.poweredOn);
@ -205,14 +218,14 @@ class MyCentralController extends CentralController
final myCharacteristic = characteristic as MyGattCharacteristic; final myCharacteristic = characteristic as MyGattCharacteristic;
final myService = myCharacteristic.myService; final myService = myCharacteristic.myService;
final myPeripheral = myService.myPeripheral; final myPeripheral = myService.myPeripheral;
final typeArgs = type.toMyArgs(); final myTypeArgs = type.toMyArgs();
final typeNumber = typeArgs.index; final myTypeNumber = myTypeArgs.index;
await _myApi.writeCharacteristic( await _myApi.writeCharacteristic(
myPeripheral.hashCode, myPeripheral.hashCode,
myService.hashCode, myService.hashCode,
myCharacteristic.hashCode, myCharacteristic.hashCode,
value, value,
typeNumber, myTypeNumber,
); );
} }

View File

@ -24,6 +24,8 @@ abstract class MyCentralControllerHostApi {
@async @async
void disconnect(int myPeripheralKey); void disconnect(int myPeripheralKey);
@async @async
int getMaximumWriteLength(int myPeripheralKey);
@async
void discoverGATT(int myPeripheralKey); void discoverGATT(int myPeripheralKey);
List<MyGattServiceArgs> getServices(int myPeripheralKey); List<MyGattServiceArgs> getServices(int myPeripheralKey);
List<MyGattCharacteristicArgs> getCharacteristics(int myServiceKey); List<MyGattCharacteristicArgs> getCharacteristics(int myServiceKey);

View File

@ -1,6 +1,6 @@
name: bluetooth_low_energy_android name: bluetooth_low_energy_android
description: Android implementation of the bluetooth_low_energy plugin. 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 homepage: https://github.com/yanshouwang/bluetooth_low_energy
environment: environment:
@ -10,7 +10,7 @@ environment:
dependencies: dependencies:
flutter: flutter:
sdk: flutter sdk: flutter
bluetooth_low_energy_platform_interface: ^2.0.3 bluetooth_low_energy_platform_interface: ^2.2.0
dev_dependencies: dev_dependencies:
flutter_test: flutter_test:

View File

@ -1,9 +1,13 @@
## 2.2.0
* Add `CentralController#getMaximumWriteLength` method.
## 2.0.3 ## 2.0.3
- `Android` Migrate to Android 13. * `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` 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 ## 2.0.2
- Combine iOS and macOS projects. * Combine iOS and macOS projects.
- Optimize project structure. * Optimize project structure.

View File

@ -254,6 +254,7 @@ protocol MyCentralControllerHostApi {
func stopDiscovery() throws func stopDiscovery() throws
func connect(myPeripheralKey: Int64, completion: @escaping (Result<Void, Error>) -> Void) func connect(myPeripheralKey: Int64, completion: @escaping (Result<Void, Error>) -> Void)
func disconnect(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 discoverGATT(myPeripheralKey: Int64, completion: @escaping (Result<Void, Error>) -> Void)
func getServices(myPeripheralKey: Int64) throws -> [MyGattServiceArgs] func getServices(myPeripheralKey: Int64) throws -> [MyGattServiceArgs]
func getCharacteristics(myServiceKey: Int64) throws -> [MyGattCharacteristicArgs] func getCharacteristics(myServiceKey: Int64) throws -> [MyGattCharacteristicArgs]
@ -359,6 +360,22 @@ class MyCentralControllerHostApiSetup {
} else { } else {
disconnectChannel.setMessageHandler(nil) 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) let discoverGATTChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.bluetooth_low_energy_darwin.MyCentralControllerHostApi.discoverGATT", binaryMessenger: binaryMessenger, codec: codec)
if let api = api { if let api = api {
discoverGATTChannel.setMessageHandler { message, reply in 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) { func discoverGATT(myPeripheralKey: Int64, completion: @escaping (Result<Void, Error>) -> Void) {
do { do {
let peripheralKey = Int(myPeripheralKey) 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 { Future<void> discoverGATT(int arg_myPeripheralKey) async {
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>( final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
'dev.flutter.pigeon.bluetooth_low_energy_darwin.MyCentralControllerHostApi.discoverGATT', codec, '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); 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 @override
Future<void> discoverGATT(Peripheral peripheral) async { Future<void> discoverGATT(Peripheral peripheral) async {
await _throwWithoutState(CentralState.poweredOn); await _throwWithoutState(CentralState.poweredOn);
@ -206,14 +222,14 @@ class MyCentralController extends CentralController
final myCharacteristic = characteristic as MyGattCharacteristic; final myCharacteristic = characteristic as MyGattCharacteristic;
final myService = myCharacteristic.myService; final myService = myCharacteristic.myService;
final myPeripheral = myService.myPeripheral; final myPeripheral = myService.myPeripheral;
final typeArgs = type.toMyArgs(); final myTypeArgs = type.toMyArgs();
final typeNumber = typeArgs.index; final myTypeNumber = myTypeArgs.index;
await _myApi.writeCharacteristic( await _myApi.writeCharacteristic(
myPeripheral.hashCode, myPeripheral.hashCode,
myService.hashCode, myService.hashCode,
myCharacteristic.hashCode, myCharacteristic.hashCode,
value, value,
typeNumber, myTypeNumber,
); );
} }

View File

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

View File

@ -1,6 +1,6 @@
name: bluetooth_low_energy_darwin name: bluetooth_low_energy_darwin
description: iOS and macOS implementation of the bluetooth_low_energy plugin. 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 homepage: https://github.com/yanshouwang/bluetooth_low_energy
environment: environment:
@ -10,7 +10,7 @@ environment:
dependencies: dependencies:
flutter: flutter:
sdk: flutter sdk: flutter
bluetooth_low_energy_platform_interface: ^2.0.3 bluetooth_low_energy_platform_interface: ^2.2.0
dev_dependencies: dev_dependencies:
flutter_test: flutter_test:

View File

@ -1,20 +1,24 @@
## 2.2.0
* Add `CentralController#getMaximumWriteLength` method.
## 2.0.3 ## 2.0.3
- `Android` Migrate to Android 13. * `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` 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 ## 2.0.2
- Combine iOS and macOS projects. * Combine iOS and macOS projects.
- Optimize project structure. * Optimize project structure.
## 2.0.1 ## 2.0.1
- Fix the issue that GATTs is cleared after peripheral disconnected on iOS and macOS. * 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 create UUID form peripheral's address failed on Linux.
- Fix the issue that instance match failed on Linux. * Fix the issue that instance match failed on Linux.
## 2.0.0 ## 2.0.0
- Rewrite the whole project with federated plugins. * Rewrite the whole project with federated plugins.
- Support macOS and Linux. * Support macOS and Linux.

View File

@ -163,6 +163,15 @@ class MyCentralController extends CentralController {
await device.disconnect(); await device.disconnect();
} }
@override
Future<int> getMaximumWriteLength(
Peripheral peripheral, {
required GattCharacteristicWriteType type,
}) async {
// TODO: 当前版本 `bluez` 插件不支持获取 MTU返回最小值 20.
return 20;
}
@override @override
Future<void> discoverGATT(Peripheral peripheral) async { Future<void> discoverGATT(Peripheral peripheral) async {
await _throwWithoutState(CentralState.poweredOn); await _throwWithoutState(CentralState.poweredOn);

View File

@ -1,6 +1,6 @@
name: bluetooth_low_energy_linux name: bluetooth_low_energy_linux
description: Linux implementation of the bluetooth_low_energy plugin. description: Linux implementation of the bluetooth_low_energy plugin.
version: 2.0.3 version: 2.2.0
homepage: https://github.com/yanshouwang/bluetooth_low_energy homepage: https://github.com/yanshouwang/bluetooth_low_energy
environment: environment:
@ -10,7 +10,7 @@ environment:
dependencies: dependencies:
flutter: flutter:
sdk: flutter sdk: flutter
bluetooth_low_energy_platform_interface: ^2.0.3 bluetooth_low_energy_platform_interface: ^2.2.0
bluez: ^0.8.1 bluez: ^0.8.1
dev_dependencies: dev_dependencies:

View File

@ -1,20 +1,36 @@
## 2.2.0
* Add `GattCharacteristicWriteType` argument to `CentralController#getMaximumWriteLength` method.
## 2.1.0
* Bump version.
## 2.0.5
* Optimize project structure.
## 2.0.4
* Add `CentralController#getMaximumWriteLength` method.
## 2.0.3 ## 2.0.3
- `Android` Migrate to Android 13. * `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` 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 ## 2.0.2
- Combine iOS and macOS projects. * Combine iOS and macOS projects.
- Optimize project structure. * Optimize project structure.
## 2.0.1 ## 2.0.1
- Fix the issue that GATTs is cleared after peripheral disconnected on iOS and macOS. * 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 create UUID form peripheral's address failed on Linux.
- Fix the issue that instance match failed on Linux. * Fix the issue that instance match failed on Linux.
## 2.0.0 ## 2.0.0
- Rewrite the whole project with federated plugins. * Rewrite the whole project with federated plugins.
- Support macOS and Linux. * Support macOS and Linux.

View File

@ -73,6 +73,12 @@ abstract class CentralController extends PlatformInterface {
/// Disconnects form the peripheral. /// Disconnects form the peripheral.
Future<void> disconnect(Peripheral peripheral); Future<void> disconnect(Peripheral peripheral);
/// Gets the max length in bytes for a single write type of the peripheral.
Future<int> getMaximumWriteLength(
Peripheral peripheral, {
required GattCharacteristicWriteType type,
});
/// Discovers GATT of the peripheral. /// Discovers GATT of the peripheral.
Future<void> discoverGATT(Peripheral peripheral); Future<void> discoverGATT(Peripheral peripheral);

View File

@ -1,6 +1,6 @@
name: bluetooth_low_energy_platform_interface name: bluetooth_low_energy_platform_interface
description: A common platform interface for the bluetooth_low_energy plugin. description: A common platform interface for the bluetooth_low_energy plugin.
version: 2.0.3 version: 2.2.0
homepage: https://github.com/yanshouwang/bluetooth_low_energy homepage: https://github.com/yanshouwang/bluetooth_low_energy
environment: environment:

View File

@ -1,20 +1,24 @@
## 2.2.0
* Add `CentralController#getMaximumWriteLength` method.
## 2.0.3 ## 2.0.3
- `Android` Migrate to Android 13. * `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` 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 ## 2.0.2
- Combine iOS and macOS projects. * Combine iOS and macOS projects.
- Optimize project structure. * Optimize project structure.
## 2.0.1 ## 2.0.1
- Fix the issue that GATTs is cleared after peripheral disconnected on iOS and macOS. * 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 create UUID form peripheral's address failed on Linux.
- Fix the issue that instance match failed on Linux. * Fix the issue that instance match failed on Linux.
## 2.0.0 ## 2.0.0
- Rewrite the whole project with federated plugins. * Rewrite the whole project with federated plugins.
- Support macOS and Linux. * Support macOS and Linux.

View File

@ -59,6 +59,15 @@ class MyCentralController extends CentralController {
throw UnimplementedError(); throw UnimplementedError();
} }
@override
Future<int> getMaximumWriteLength(
Peripheral peripheral, {
required GattCharacteristicWriteType type,
}) {
// TODO: implement getMaximumWriteLength
throw UnimplementedError();
}
@override @override
Future<void> discoverGATT(Peripheral peripheral) { Future<void> discoverGATT(Peripheral peripheral) {
// TODO: implement discoverGATT // TODO: implement discoverGATT

View File

@ -1,6 +1,6 @@
name: bluetooth_low_energy_windows name: bluetooth_low_energy_windows
description: Windows implementation of the bluetooth_low_energy plugin. description: Windows implementation of the bluetooth_low_energy plugin.
version: 2.0.3 version: 2.2.0
homepage: https://github.com/yanshouwang/bluetooth_low_energy homepage: https://github.com/yanshouwang/bluetooth_low_energy
environment: environment:
@ -10,7 +10,7 @@ environment:
dependencies: dependencies:
flutter: flutter:
sdk: flutter sdk: flutter
bluetooth_low_energy_platform_interface: ^2.0.3 bluetooth_low_energy_platform_interface: ^2.2.0
win32: ^5.0.6 win32: ^5.0.6
dev_dependencies: dev_dependencies: