final List<String> _edgeDev = [ // "лузы"
"a4:c1:38:67:7d:4c",
];
final List<String> _beaconDev = [ // "шарики"
"a4:c1:38:32:f0:d0"
];
final List<String> _rssi = ['', '', '', '']; // для прорисовки результата
dynamic _startScan() async {
FlutterBlue.instance.scan().listen((e) {
if (isEdgeDev(e.device)) {
_processScanResult(e);
}
});
}
bool isEdgeDev(BluetoothDevice d) {
return _edgeDev.contains(d.id.id.toLowerCase());
}
void _onNewData(BluetoothCharacteristic _advC, List<int> l) {
// print('=data====== $l');
if (l.length > 11 && l[8].toUnsigned(8) == 0x16) {
var rssi = l[0].toSigned(8);
var mac = l.sublist(1, 7).reversed;
String s = '';
for (var b in mac) {
s += b.toRadixString(16) + ":";
}
s = s.substring(0, s.length - 1);
var indx = _beaconDev.indexOf(s);
print('=data====== $s $rssi');
if (indx >= 0 && indx <= _rssi.length) {
setState(
() => _rssi[indx] = rssi.toString(),
);
}
}
}
void _processScanResult(ScanResult e) {
e.device.state.listen(
(state) async {
if (state == BluetoothDeviceState.connected) {
print('===== device connected');
e.device.mtu.listen((value) async {
print('=mtu====== $value');
e.device.discoverServices();
});
e.device.services.listen((services) {
var _advS = services.firstWhere(
(e) => e.uuid == Guid('00001f10-0000-1000-8000-00805f9b34fb'),
);
var _advC = _advS.characteristics.firstWhere(
(e) => e.uuid == Guid('00001f11-0000-1000-8000-00805f9b34fb'),
);
_advC.setNotifyValue(true).then((value) {
_advC.value.listen((l) {
_onNewData(_advC, l);
});
});
});
await e.device.requestMtu(63);
} else if (state == BluetoothDeviceState.disconnected) {
print('===== device disconnected');
}
},
);
e.device.connect();
}