startScan method
Future<void>
startScan({
- List<
String> serviceUuids = const <String>[], - Duration? timeout,
- bool allowDuplicates = false,
- BluetoothScanMode scanMode = BluetoothScanMode.ble,
override
实现 FlutterBluetoothPluginPlatform.startScan。
Web 会打开设备选择器并返回用户选中的单个 BLE 设备;不是被动扫描。
Implementation
@override
Future<void> startScan({
List<String> serviceUuids = const <String>[],
Duration? timeout,
bool allowDuplicates = false,
BluetoothScanMode scanMode = BluetoothScanMode.ble,
}) async {
if (scanMode == BluetoothScanMode.classic) {
throw UnsupportedError('Web Bluetooth supports BLE devices only.');
}
final bluetooth = _requireBluetooth();
final state = await getAdapterState();
if (state == BluetoothAdapterState.poweredOff) {
throw StateError(
'Bluetooth is not available or powered on in the browser.');
}
_isSelectingDevice = true;
try {
final device = await bluetooth
.requestDevice(_requestDeviceOptions(serviceUuids))
.toDart;
_rememberDevice(device);
_scanResultsController.add(
BluetoothScanResult(
device: _deviceFromWeb(device),
rssi: 0,
localName: device.name,
serviceUuids: serviceUuids,
raw: <String, dynamic>{
'platform': 'web',
'chooserResult': true,
'allowDuplicatesIgnored': allowDuplicates,
'timeoutIgnored': timeout != null,
'scanMode': scanMode.name,
},
),
);
} finally {
_isSelectingDevice = false;
}
}