Skip to content

Instantly share code, notes, and snippets.

@jason19970210
Created October 11, 2022 03:15
Show Gist options
  • Save jason19970210/b947612dfcec51a160a83aa66ac6ce40 to your computer and use it in GitHub Desktop.
Save jason19970210/b947612dfcec51a160a83aa66ac6ce40 to your computer and use it in GitHub Desktop.
for flutter_blue v0.8.0 scan ble peripherals adv information
_scan() async {
if (isDeviceConnected) return;
print('_scan()');
try {
flutterBlue.scan(
timeout: Duration(seconds: 4), // onDone()
withServices: [], // modify the target service UUID
).listen((scanResult) {
print("scanResult : $scanResult");
print(scanResult.device)
print(scanResult.advertisementData)
// do your own filter
if (scanResult.device.name == "TARGET_DEVICE_NAME") {
setState(() {
_connectDeviceInfo.macAddr =
_getMacAddr(scanResult.advertisementData.manufacturerData);
});
BluetoothDevice device = scanResult.device;
if (!devicesList.contains(device)) {
print("_addDeviceTolist : $device");
setState(() {
devicesList.add(device);
});
}
}
}).onDone(() async {
// timeout
// print("onDone");
print("devicesList : $devicesList");
if (devicesList.length == 0) {
showAlert("No device founded !");
} else {
_targetDevice = devicesList.first;
}
});
} catch (e) {
print(e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment