Flattern Sie eine Verbindung mit dem BLE-Gerät, auch nachdem Verbindungsmethoden aufgerufen wurden
Posted: 23 Jan 2025, 04:25
Ich habe versucht, meine Anwendung mithilfe der Bibliothek flutter_blue_plus mit Bluetooth-Geräten (ESP32) zu verbinden. In ESP32 gibt es also eine Firmware, die ein Bluetooth-Signal ankündigt, und meine Anwendung sollte eine Verbindung zu diesem Gerät herstellen, wenn ich die Schaltfläche „VERBINDEN“ drücke.
Hier ist der Code für meine Verbindungsschaltfläche:
Nachdem ich also auf die Schaltfläche „VERBINDEN“ geklickt habe, wird nicht versucht, die Dienste zu finden. Nach der Debug-Verarbeitung wurde mir mitgeteilt, dass meine Apps überhaupt nicht verbunden sind. Hier ist das Dlog für mein Debug-Ergebnis.
Ich glaube, es liegt ein Problem mit meiner Schaltfläche „VERBINDEN“ vor. Aber ich bin mir nicht wirklich sicher. Hier ist der vollständige Code. Brauche dafür wirklich Hilfe. Danke.
Hier ist der Code für meine Verbindungsschaltfläche:
Code: Select all
onPressed: () {
flutterBlue.startScan(timeout: Duration(seconds: 4));
var subscription = flutterBlue.scanResults.listen((results) async {
for (ScanResult r in results) {
print('${r.device.name} found! rssi: ${r.rssi}');
if (_id_controller.text == '${r.device.name}' && _id_controller.text != '') {
flutterBlue.stopScan();
r.device.connect();
print('${_id_controller.text} same with ${r.device.name}');
print(flutterBlue.connectedDevices);
List services = await r.device.discoverServices();
services.forEach((service) async {
var characteristics = service.characteristics;
for(BluetoothCharacteristic c in characteristics) {
List value = await c.read();
print(value);
await c.write([73, 77, 65, 77]);
}
});
}
}
});
flutterBlue.stopScan();
},
Code: Select all
I/flutter (28832): found! rssi: -49
I/flutter (28832): GNG000001 found! rssi: -92
W/provisioningap(28832): Accessing hidden method Lsun/misc/Unsafe;->putInt(Ljava/lang/Object;JI)V (greylist, linking, allowed)
D/BluetoothAdapter(28832): isLeEnabled(): ON
D/BluetoothLeScanner(28832): onScannerRegistered() - status=0 scannerId=8 mScannerId=0
I/flutter (28832): GNG000001 same with GNG000001
D/BluetoothAdapter(28832): isLeEnabled(): ON
I/flutter (28832): Instance of 'Future'
W/provisioningap(28832): Accessing hidden method Lsun/misc/Unsafe;->putObject(Ljava/lang/Object;JLjava/lang/Object;)V (greylist, linking, allowed)
E/BluetoothGatt(28832):
E/BluetoothGatt(28832): android.os.DeadObjectException
E/BluetoothGatt(28832): at android.os.BinderProxy.transactNative(Native Method)
E/BluetoothGatt(28832): at android.os.BinderProxy.transact(BinderProxy.java:543)
E/BluetoothGatt(28832): at android.bluetooth.IBluetoothGatt$Stub$Proxy.clientConnect(IBluetoothGatt.java:1847)
E/BluetoothGatt(28832): at android.bluetooth.BluetoothGatt.connect(BluetoothGatt.java:915)
E/BluetoothGatt(28832): at com.boskokg.flutter_blue_plus.FlutterBluePlusPlugin.lambda$onMethodCall$3$com-boskokg-flutter_blue_plus-FlutterBluePlusPlugin(FlutterBluePlusPlugin.java:342)
E/BluetoothGatt(28832): at com.boskokg.flutter_blue_plus.FlutterBluePlusPlugin$$ExternalSyntheticLambda5.op(Unknown Source:6)
E/BluetoothGatt(28832): at com.boskokg.flutter_blue_plus.FlutterBluePlusPlugin.ensurePermissionBeforeAction(FlutterBluePlusPlugin.java:727)
E/BluetoothGatt(28832): at com.boskokg.flutter_blue_plus.FlutterBluePlusPlugin.onMethodCall(FlutterBluePlusPlugin.java:315)
E/BluetoothGatt(28832): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:262)
E/BluetoothGatt(28832): at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:295)
E/BluetoothGatt(28832): at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:319)
E/BluetoothGatt(28832): at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12)
E/BluetoothGatt(28832): at android.os.Handler.handleCallback(Handler.java:883)
E/BluetoothGatt(28832): at android.os.Handler.dispatchMessage(Handler.java:100)
E/BluetoothGatt(28832): at android.os.Looper.loop(Looper.java:264)
E/BluetoothGatt(28832): at android.app.ActivityThread.main(ActivityThread.java:7581)
E/BluetoothGatt(28832): at java.lang.reflect.Method.invoke(Native Method)
E/BluetoothGatt(28832): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
E/BluetoothGatt(28832): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:980)
D/FlutterBluePlugin(28832): mDevices size: 1
D/BluetoothAdapter(28832): isLeEnabled(): ON
D/BluetoothLeScanner(28832): could not find callback wrapper
E/flutter (28832): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(reconnect_error, error when reconnecting to device, null, null)
E/flutter (28832): #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:653:7)
E/flutter (28832): #1 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:296:18)
E/flutter (28832):
E/flutter (28832): #2 BluetoothDevice.connect (package:flutter_blue_plus/src/bluetooth_device.dart:39:5)
E/flutter (28832):
E/flutter (28832):
E/flutter (28832): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Exception: Cannot discoverServices while device is not connected. State == BluetoothDeviceState.disconnected
E/flutter (28832):