Я использую плагин Flutter для Bluetooth под названием flutter_bluetooth_serial, и хотя он отлично работает, когда приложение создано для Android, оно не работает и для iOS.
Я получаю следующую ошибку:
Произошло исключение. MissingPluginException (MissingPluginException (Реализация для метода getState на канале flutter_bluetooth_serial / methods не найдена))
в этой строке:
bluetoothSerial.state.then((state) => setState(() {
А вот полный код:
class _MainProgrammingScreenState extends State<MainProgrammingScreen> {
FlutterBluetoothSerial bluetoothSerial = FlutterBluetoothSerial.instance;
BluetoothConnection deviceConnection;
bool bluetoothOn = false;
bool get deviceConnected =>
(deviceConnection != null && deviceConnection.isConnected);
Future<bool> enableBluetooth() async {
var btEnabled = await bluetoothSerial.requestEnable();
setState(() {});
return btEnabled;
}
Future<bool> connectToDevice() async {
if (!(await enableBluetooth())) {
return false;
}
//get bounded devices
var boundedDevices = await bluetoothSerial.getBondedDevices();
if (await bluetoothSerial.isDiscovering) {
await bluetoothSerial.cancelDiscovery();
}
//bt scan
for (int scanAttempt = 0; scanAttempt < 3; scanAttempt++) {
await for (var discoveryResult in bluetoothSerial.startDiscovery()) {
BluetoothDevice device = discoveryResult.device;
//find device
if (device.name != null && device.name.contains("DEVICE")) {
BluetoothDevice device = device;
//if device not just paired
if (!boundedDevices.contains(device)) {
//pair to device
try {
var deviceBounded =
await bluetoothSerial.bondDeviceAtAddress(device.address);
if (!deviceBounded) {
return false;
}
} catch (error) {
print("bound device error: $error");
return false;
}
}
//try connect to device
for (var deviceConnAttempt = 0;
deviceConnAttempt < 3;
deviceConnAttempt++) {
try {
deviceConnection =
await BluetoothConnection.toAddress(device.address);
if (deviceConnection.isConnected) {
deviceConnection.input.listen(btReceaveData);
setState(() {});
return true;
}
} catch (error) {
print("device connection failed: $error");
return false;
}
}
}
}
}
return false;
}
int intChar(str) => str.codeUnitAt(0);
BtObj btObj = new BtObj();
void btReceaveData(Uint8List buffer) {
for (var byte in buffer) {
if (byte == intChar('<')) {
btObj.clear();
} else if (byte == intChar('>')) {
//decode obj
btDecoder(btObj);
} else {
btObj.pushByte(byte);
}
}
}
Future<void> sendPack(Uint8List data, BtObjType dataType) async {
try {
BtObj tempObj = BtObj.fromList(data, dataType);
deviceConnection.output.add(tempObj.list);
//await deviceConnection.output.done;
} catch (error) {
print("error sending data to device: $error");
}
}
void initState() {
super.initState();
bluetoothSerial.state.then((state) => setState(() {
bluetoothOn = state == BluetoothState.STATE_ON;
}));
bluetoothSerial.onStateChanged().listen((state) => setState(() {
bluetoothOn = state == BluetoothState.STATE_ON;
}));
}
@override
Widget build(BuildContext context) {
return ...
Есть идеи, что происходит на iOS?
Большое спасибо.