Closed IvanTorres21 closed 1 week ago
Can you provide a sample to reproduce this issue. From the logs above, it seems you passed invalid Peripheral or GATTCharacteristic instance, so the plugin can not find corresponding instance on native side which will throw illegalArgument error.
Here is the issue replicated on a single page.
import 'dart:io';
import 'dart:typed_data';
import 'package:bluetooth_low_energy/bluetooth_low_energy.dart';
import 'package:flutter/material.dart';
class ReplicateIssue extends StatefulWidget {
@override
State<ReplicateIssue> createState() => _ReplicateIssueState();
}
class _ReplicateIssueState extends State<ReplicateIssue> {
final List<DiscoveredEventArgs> _devices = List.empty(growable: true);
final List<DiscoveredEventArgs> _connectedTo = List.empty(growable: true);
List<GATTService> services = List.empty();
final String commonCharacteristic = ""; // This is the common characteristic, both of the devices have this.
final String commonCharacteristic2 = ""; // Same as above, they share this characteristic.
bool isScanning = false;
@override
void initState() {
scanDevices();
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
ElevatedButton(onPressed: sendMessage, child: Text("Send BLE Message")),
SizedBox(
height: MediaQuery.of(context).size.height * .6,
child: ListView.builder(
itemBuilder: (_, index) =>
ElevatedButton(onPressed: () => connectDevice(_devices[index]), child: Text("${_devices[index].advertisement.name}")),
itemCount: _devices.length,
),
)
],
),
);
}
bool isWritting = false;
Future<void> sendMessage() async {
if(isWritting) return;
isWritting = true;
for(var device in _connectedTo) {
await Bluetooth.writeCharacteristic(device.peripheral, getGattCharacteristic(commonCharacteristic2), value: Uint8List.fromList([0x0]), type: GATTCharacteristicWriteType.withResponse);
}
isWritting = false;
}
void scanDevices() async {
if(!Platform.isWindows) {
await Bluetooth.authorize();
}
isScanning = true;
Bluetooth.discovered.listen(AddDevice);
while(isScanning) {
await Future.delayed(const Duration(seconds: 1));
await Bluetooth.startDiscovery();
await Future.delayed(const Duration(seconds: 2));
await Bluetooth.stopDiscovery();
}
}
CentralManager get Bluetooth => CentralManager();
void AddDevice(DiscoveredEventArgs event) {
print(event.advertisement.name);
if (_devices.any((x) => x.advertisement.name == event.advertisement.name)) return;
setState(() {
_devices.add(event);
});
}
Future<void> connectDevice(DiscoveredEventArgs device) async {
isScanning = false;
await Bluetooth.stopDiscovery();
await Bluetooth.connect(device.peripheral);
services = await Bluetooth.discoverGATT(device.peripheral); //Both the devices we are connecting to are the same.
_connectedTo.add(device);
await Bluetooth.writeCharacteristic(device.peripheral, getGattCharacteristic(commonCharacteristic), value: Uint8List.fromList([0x25]), type: GATTCharacteristicWriteType.withResponse);
}
GATTCharacteristic getGattCharacteristic(String whichOne) {
for (var service in services) {
for (var characteristic in service.characteristics) {
if (UUID(characteristic) == whichOne) return characteristic;
}
}
throw new Exception("Characteristic not found in any service");
}
String UUID(GATTCharacteristic gatt) => gatt.uuid.toString().toUpperCase();
}
As I said previously, when executing this in windows it works perfectly fine. But mobile throws the error. I´m using the same bluetooth devices for both attempts, and the bluetooth devices are the exact same.
@IvanTorres21 There are issues in these two method.
Future<void> connectDevice(DiscoveredEventArgs device) async { isScanning = false; await Bluetooth.stopDiscovery(); await Bluetooth.connect(device.peripheral); services = await Bluetooth.discoverGATT(device.peripheral); //Both the devices we are connecting to are the same. _connectedTo.add(device); await Bluetooth.writeCharacteristic(device.peripheral, getGattCharacteristic(commonCharacteristic), value: Uint8List.fromList([0x25]), type: GATTCharacteristicWriteType.withResponse); } GATTCharacteristic getGattCharacteristic(String whichOne) { for (var service in services) { for (var characteristic in service.characteristics) { if (UUID(characteristic) == whichOne) return characteristic; } } throw new Exception("Characteristic not found in any service"); }
services
is set after the first deice connected, and will be replaced after the second device connected. As a result, the first device's service instances are lost.services
without error, but the characteristic is belong to the second's actually. So the plugin will throw this error when you tell it to write to the first device but with the characteristic which belong to the second device.This issus is not exist on Windows because the way I used to find corresponding native instance on Windows is different from the way on other platforms.
Ahhh, I see. Sorry then, I did the windows implementation first and since it worked thought it was a problem with the plugin itself.
Sorry for the issue, I´ll store the services twice then.
Hello,
I was able to connect two different bluetooth devices to the same app and send write requests to them in the windows version. But when executing the same application within the Android/iOS version an error pops out when trying to write to the first bluetooth device I connected to.
Steps to reproduce: 1.- Connect to one device 2.- Connect to a second device 3.- Send a write request to both 4.- The second device recives the requests, while the first one throws this error:
`flutter: PlatformException(illegalArgument, MyError, Stacktrace: ["0 bluetooth_low_energy_darwin 0x0000000103cf3250 $s27bluetooth_low_energy_darwin9wrapError33_D3FF39C2E9589F2A07964E0F3A1AEE9ELLySayypSgGypF + 1708", "1 bluetooth_low_energy_darwin 0x0000000103d029fc $s27bluetooth_low_energy_darwin28MyCentralManagerHostAPISetupC5setUp15binaryMessenger3api20messageChannelSuffixySo013FlutterBinaryM0_p_AA0efgH3API_pSgSStFZyypSg_yAKctcfU14_ys6ResultOyyts5ErrorpGcfU + 324", "2 bluetooth_low_energy_darwin 0x0000000103d1fa1c $s27bluetooth_low_energy_darwin16MyCentralManagerC19writeCharacteristic8uuidArgs08hashCodeK005valueK004typeK010completionySS_s5Int64VSo24FlutterStandardTypedDataCAA0e27GATTCharacteristicWriteTypeK0Oys6ResultOyyts5Error_pGctF + 1104", "3 bluetooth_low_energy_darwin 0x0000000103d2acb4 $s27bluetooth_low_energy_darwin16MyCentralManagerCAA0efG7HostAPIA2aDP19writeCharacteristic8uuidArgs08hashCodeM005valueM004typeM010completionySS_s5Int64VSo24FlutterStandardTypedDataCAA0e27GATTCharacteristicWriteTypeM0Oys6ResultOyyts5Error_pGctFTW + 32", "4 bluetooth_low_energy_darwin 0x0000000103d02888 $s27bluetooth_low_energy_darwin28MyCentralManagerHostAPISetupC5setUp15binaryMessenger3api20messageChannelSuffixySo013FlutterBinaryM0_p_AA0efgH3API_pSgSStFZyypSgyAKctcfU14 + 1760", "5 bluetooth_low_energy_darwin 0x0000000103cfe278 $sypSgAAIegn_Iegng_yXlSgABIeyBy_IeyByy_TR + 236", "6 Flutter 0x00000001072f84c8 __48-[FlutterBasicMessageChannel setMessageHandler:]_block_invoke + 160", "7 Flutter 0x0000000106d65794 ___ZN7flutter25PlatformMessageHandlerIos21HandlePlatformMessageENSt3_fl10unique_ptrINS_15PlatformMessageENS1_14default_deleteIS3_EEEE_block_invoke + 116", "8 libdispatch.dylib 0x00000001c606e7a8 B51E7CDB-ABC9-35AF-B8BB-2DCE23BC4D6E + 407464", "9 libdispatch.dylib 0x00000001c606f780 B51E7CDB-ABC9-35AF-B8BB-2DCE23BC4D6E + 411520", "10 libdispatch.dylib 0x00000001c601bbdc B51E7CDB-ABC9-35AF-B8BB-2DCE23BC4D6E + 68572", "11 libdispatch.dylib 0x00000001c601b82c B51E7CDB-ABC9-35AF-B8BB-2DCE23BC4D6E + 67628", "12 CoreFoundation 0x00000001bf3519ac 55B9BA28-4C5C-3FE7-9C47-4983337D6E83 + 596396", "13 CoreFoundation 0x00000001bf335648 55B9BA28-4C5C-3FE7-9C47-4983337D6E83 + 480840", "14 CoreFoundation 0x00000001bf339d20 CFRunLoopRunSpecific + 584", "15 GraphicsServices 0x00000001f6b49998 GSEventRunModal + 160", "16 UIKitCore 0x00000001c15cc448 27A9C298-B702-3C39-8C06-07196E4CD16B + 3609672", "17 UIKitCore 0x00000001c15cc0c0 UIApplicationMain + 312", "18 Runner 0x0000000102c7895c main + 64", "19 dyld 0x00000001dcae4344 8A4B89B7-D348-375B-97B1-FC8A84E3E5CE + 82756"], null) flutter:
0 MyCentralManagerHostAPI.writeCharacteristic (package:bluetooth_low_energy_darwin/src/my_api.g.dart:861:7)