I need to upgrade to flutter blue plus but I'm using this: import…
- User
I need to upgrade to flutter blue plus but I'm using this:
import 'package:path_provider/path_provider.dart'; import 'package:flutter/material.dart'; import 'package:just_audio/just_audio.dart'; import 'package:permission_handler/permission_handler.dart'; import 'package:flutter_blue_plus/gen/flutterblue.pb.dart' as ProtoBluetoothDevice;
and flutterblue.pb doesn't exist in blue plus but needs it to connect here:
return Navigator.of(context).canPop();}
void connectToDevice(BuildContext context, String macAddress) async {
print("Hallo"); isConnecting = true; var protoBt = ProtoBluetoothDevice.BluetoothDevice(remoteId: macAddress); var d = BluetoothDevice.fromProto(protoBt); targetDevice = d; targetDevice!.connect().then((_) { handleBluetoothDevice().then((_) { isConnecting = false; Navigator.push( context, MaterialPageRoute( builder: (context) => BluetoothScreen(takePictureScreenState: this)), ); }).catchError((error) { if (canPop(context)) { Navigator.pop(context); - Assistant
- User
void connectToDevice(BuildContext context, String macAddress) async {
print("Hallo"); isConnecting = true; targetDevice = BluetoothDevice(remoteId: DeviceIdentifier(macAddress)); targetDevice!.connect().then((_) { handleBluetoothDevice().then((_) { isConnecting = false; Navigator.push( context, MaterialPageRoute( builder: (context) => BluetoothScreen(takePictureScreenState: this)), ); }).catchError((error) { if (canPop(context)) { Navigator.pop(context); } }); }).catchError((error) { print("Something happened"); print(error); if (canPop(context)) { Navigator.pop(context); } }); connectionStateSubscription = targetDevice!.state.listen((BluetoothDeviceState state) { if (state == BluetoothDeviceState.disconnected && canPop(context)) { Navigator.pop(context); } });}
A value of type 'StreamSubscription' can't be assigned to a variable of type 'StreamSubscription?'. Try changing the type of the variable, or casting the right-hand type to 'StreamSubscription?'.dartinvalid_assignment
- Assistant
The error message indicates that there's a type mismatch between what's being returned from
targetDevice.state.listenand the variableconnectionStateSubscription. This typically means thattargetDevice.statereturns a stream with the wrong type of data for your variable.This can happen if the
BluetoothDeviceclass influtter_blue_plusreturns a stream of a different enum type for the device's state. To fix this, let's verify the type of streamtargetDevice.stateprovides.For
flutter_blue_plus, the correct type for listening to the device state isStream<BluetoothDeviceState>. The variableconnectionStateSubscriptionhas to be of type to capture the subscription to this stream. - User
the bluetooth device shows it's connected but nothing happens on the app it only prints hallo in the debug console:
void connectToDevice(BuildContext context, String macAddress) async { if(isConnecting) return; print("Hallo"); isConnecting = true; targetDevice = BluetoothDevice(remoteId: DeviceIdentifier(macAddress));
targetDevice!.connect().then((_) { handleBluetoothDevice().then((_) { isConnecting = false; Navigator.push( context, MaterialPageRoute( builder: (context) => BluetoothScreen(takePictureScreenState: this)), ); }).catchError((error) { if (canPop(context)) { Navigator.pop(context); } }); }).catchError((error) { print("Something happened"); print(error); if (canPop(context)) { Navigator.pop(context); } });connectionStateSubscription = targetDevice!.connectionState.listen((BluetoothConnectionState state) { if (state == BluetoothConnectionState.disconnected && canPop(context)) { Navigator.pop(context); } }); }
- Assistant
- User
I/flutter (27227): Connection error: FlutterBluePlusException | setNotifyValue | android-code: 3 | GATT_WRITE_NOT_PERMITTED
- Assistant
- User
So the thing is I already know what charastetic to connect to but I cannot write to the bluetooth device, it only sends doesn't accept and sending to it only wanna listen to the charastetic:
Future handleBluetoothDevice() async { if (targetDevice == null) return;
List<BluetoothService> services = await targetDevice!.discoverServices(); print(services); services.forEach((service) { print(service); service.characteristics.forEach((characteristic) { print(characteristic); if (characteristic.uuid.toString() == "06d1e5e7-79ad-4a71-8faa-373789f7d93c") { targetCharacteristic = characteristic; characteristic.setNotifyValue(true); characteristic.value.listen((value) { handleReceivedData(value); }); } }); });}
- Assistant
- User
If I know the service adress can't I just use that + the charastetic to connect to it directly without needing to scan?
- Assistant
- User
is it possible to do without the List services = await device.discoverServices();
it requiers to write tot the ble but that's not possible
- Assistant
- User
but it was working when using fromproto
- Assistant
- User
how can you make so it doesn't write to the bluetooth unit?_
Future<List> discoverServices({bool subscribeToServicesChanged = true, int timeout = 15}) async { // check connected if (isConnected == false) { throw FlutterBluePlusException( ErrorPlatform.fbp, "discoverServices", FbpErrorCode.deviceIsDisconnected.index, "device is not connected"); }
// Only allow a single ble operation to be underway at a time _Mutex mtx = _MutexFactory.getMutexForKey("global"); await mtx.take(); List<BluetoothService> result = []; try { var responseStream = FlutterBluePlus._methodStream.stream .where((m) => m.method == "OnDiscoveredServices") .map((m) => m.arguments) .map((args) => BmDiscoverServicesResult.fromMap(args)) .where((p) => p.remoteId == remoteId.str); // Start listening now, before invokeMethod, to ensure we don't miss the response Future<BmDiscoverServicesResult> futureResponse = responseStream.first; // invoke await FlutterBluePlus._invokeMethod('discoverServices', remoteId.str); // wait for response BmDiscoverServicesResult response = await futureResponse .fbpEnsureAdapterIsOn("discoverServices") .fbpEnsureDeviceIsConnected(this, "discoverServices") .fbpTimeout(timeout, "discoverServices"); // failed? if (!response.success) { throw FlutterBluePlusException(_nativeError, "discoverServices", response.errorCode, response.errorString); } result = response.services.map((p) => BluetoothService.fromProto(p)).toList(); } finally { mtx.give(); } // in order to match iOS behavior on all platforms, // we always listen to the Services Changed characteristic if it exists. if (subscribeToServicesChanged) { if (Platform.isIOS == false && Platform.isMacOS == false) { BluetoothCharacteristic? c = _servicesChangedCharacteristic; if (c != null && (c.properties.notify || c.properties.indicate) && c.isNotifying == false) { await c.setNotifyValue(true); } } } return result; - Assistant
The
discoverServicesmethod implementation you show is from the FlutterBluePlus library, and it is designed to perform service discovery on a connected Bluetooth device. The service discovery process itself does not require writing to the BLE peripheral except for enabling notifications on the Service Changed characteristic, which is a standard procedure that isn't usually a problem.From your code snippet, we can see an optional parameter. This parameter controls whether to automatically subscribe to notifications from the "Services Changed" characteristic (if it exists) after services have been discovered. This characteristic is used to let the central device know when a peripheral's available services have changed.
- User
it doesn't seem to send the command to the post request and play the incomming audio when I have built the app, but it works in debug mode:
- Assistant