requestDevice static method
Request the use of a device from the user. Calling this will show the user a dialog in which they will be able to select a single device to pair with
If you need to pair with more devices then you will need to request this for each device individually.
Make sure to check getAvailability, and isBluetoothAPISupported or else you may get an Error.
-
May throw UserCancelledDialogError if the user canceled the dialog
-
May throw DeviceNotFoundError if no device could be found for the RequestOptions.
-
May throw TypeError if the RequestOptions are malformed.
-
May throw MissingUserGestureError if the method is not called from a user gesture.
See:
Implementation
static Future<WebBluetoothDevice> requestDevice(
final RequestOptions options) async {
final promise = _nativeBluetooth.requestDevice(options);
try {
final result = await _JSUtil.promiseToFuture(promise);
final device = WebBluetoothDevice.fromJSObject(result);
return device;
} catch (e) {
final error = e.toString();
if (error.startsWith("NotFoundError")) {
// No devices found or cancelled by the user.
if (error.toLowerCase().contains("user cancel")) {
// TODO: check if this is also the message on other browsers!
throw UserCancelledDialogError(
error.replaceFirst("NotFoundError", "").replaceFirst(": ", ""));
}
throw DeviceNotFoundError(
error.replaceFirst("NotFoundError", "").replaceFirst(": ", ""));
} else if (error.startsWith("SecurityError") &&
error.toLowerCase().contains("gesture")) {
throw MissingUserGestureError("requestDevice");
}
if (e is Error) {
rethrow;
}
throw BrowserError(error);
}
}