You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When I create a connection for multiple devices (A, B, C), only two devices (A and B) can connect and exchange data with each other. The third device (C) can only send data to A.
/// Service for managing BLE (Central and Peripheral) operations@lazySingletonclassBleService {
final _bleCentral =FlutterBlePeripheralCentral();
final _blePeripheral =FlutterBlePeripheralCentral();
StreamSubscription<dynamic>? _scanSubscription;
StreamSubscription<dynamic>? _advertiseSubscription;
finalSet<String> _receivedDataList = {};
final _receivedDataListController =StreamController<Set<String>>.broadcast();
final _receivedDataController =StreamController<String>.broadcast();
Stream<Set<String>> get receivedDataListStream =>
_receivedDataListController.stream;
Stream<String> get receivedDataStream => _receivedDataController.stream;
/// Start scanning for BLE devicesFuture<void> startScanning() async {
try {
await_checkPermissions();
_scanSubscription = _bleCentral.scanAndConnect().listen(
(event) {
debugPrint('Scan Event: $event');
final eventData =jsonDecode(event);
if (eventData.containsKey('onCharacteristicWriteRequest')) {
final receivedString =
eventData['onCharacteristicWriteRequest'] asString?;
if (receivedString !=null) {
_receivedDataList.add(receivedString);
_receivedDataListController.add(_receivedDataList);
}
}
},
);
} catch (e) {
debugPrint('Error scanning: $e');
throwException('Error scanning: $e');
}
}
/// Stop scanning for BLE devicesFuture<void> stopScanning() async {
_receivedDataList.clear();
await _scanSubscription?.cancel();
if (_scanSubscription !=null) {
await _bleCentral.bleDisconnect();
}
_scanSubscription =null;
}
/// Start advertising BLE dataFuture<void> startAdvertising({String? dataAdvertising}) async {
try {
await_checkPermissions();
_advertiseSubscription = _blePeripheral
.startBlePeripheralService('userPath', dataAdvertising ??'')
.listen((event) {
debugPrint('Advertise Event: $event');
finalMap<String, dynamic> eventData =jsonDecode(event);
if (eventData.containsKey('onCharacteristicWriteRequest')) {
finalString? receivedString =
eventData['onCharacteristicWriteRequest'];
if (receivedString !=null) {
_receivedDataController.add(receivedString);
_receivedDataList.add(receivedString);
_receivedDataListController.add(_receivedDataList);
}
}
});
} catch (e) {
debugPrint('Error advertising: $e');
throwException('Error advertising: $e');
}
}
/// Stop advertising BLE dataFuture<void> stopAdvertising() async {
_receivedDataList.clear();
await _advertiseSubscription?.cancel();
if (_advertiseSubscription !=null) {
await _blePeripheral.stopBlePeripheralService();
}
_advertiseSubscription =null;
}
/// Send data to a connected BLE deviceFuture<void> sendData(String data) async {
try {
await _blePeripheral.bleWriteCharacteristic(data);
} catch (e) {
debugPrint('Error sending data: $e');
}
}
/// Read data from a BLE deviceFuture<String?> bleReadCharacteristic() async {
return _bleCentral.bleReadCharacteristic();
}
/// Clean up resourcesFuture<void> dispose() async {
_receivedDataList.clear();
await _receivedDataListController.close();
await _receivedDataController.close();
awaitstopScanning();
awaitstopAdvertising();
}
/// Check and request necessary permissionsFuture<void> _checkPermissions() async {
if (Platform.isAndroid) {
final permissions =await [
Permission.bluetoothScan,
Permission.bluetoothAdvertise,
Permission.bluetoothConnect,
Permission.location,
].request().then((value) => value.values.toList());
if (permissions.any((status) => status.isDenied)) {
throwException('BLE permissions are denied');
}
}
}
}
The text was updated successfully, but these errors were encountered:
When I create a connection for multiple devices (A, B, C), only two devices (A and B) can connect and exchange data with each other. The third device (C) can only send data to A.
The text was updated successfully, but these errors were encountered: