Created
October 11, 2022 03:15
-
-
Save jason19970210/b947612dfcec51a160a83aa66ac6ce40 to your computer and use it in GitHub Desktop.
for flutter_blue v0.8.0 scan ble peripherals adv information
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_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