I have a isolate with 2 function:
@override
void initState() {
super.initState();
startDiscovery();
}
void startDiscoveryIsolate(SendPort sendPort) async {
Timer.periodic(const Duration(seconds: 1), (Timer t) async {
await badgeSDK.startDiscovery(); //methodChannel
CustomBLEDevice? device = await badgeSDK.getBestDevice(); //methodChannel
if (device != null) {
sendPort.send(device);
}
});
}
Future<void> startDiscovery() async {
final receivePort = RawReceivePort();
final sendPort = receivePort.sendPort;
final isolate = await Isolate.spawn(startDiscoveryIsolate, sendPort);
receivePort.handler = (message) {
setState(() {
bestGetDeviceName = message.name;
});
};
}
I need to execute this functions every seconds for scanning bluetooth.
At this row:
final isolate = await Isolate.spawn(startDiscoveryIsolate, sendPort);
I have this error:
Exception has occurred. ArgumentError (Invalid argument(s): Illegal argument in isolate message: (object is a ReceivePort))
Someone can help me?