Dart Documentationchrome.bluetoothChromeBluetooth

ChromeBluetooth class

class ChromeBluetooth extends ChromeApi {
 static final JsObject _bluetooth = chrome['bluetooth'];

 ChromeBluetooth._();

 bool get available => _bluetooth != null;

 /**
  * Registers the JavaScript application as an implementation for the given
  * Profile; if a channel or PSM is specified, the profile will be exported in
  * the host's SDP and GATT tables and advertised to other devices.
  */
 Future addProfile(Profile profile) {
   if (_bluetooth == null) _throwNotAvailable();

   var completer = new ChromeCompleter.noArgs();
   _bluetooth.callMethod('addProfile', [jsify(profile), completer.callback]);
   return completer.future;
 }

 /**
  * Unregisters the JavaScript application as an implementation for the given
  * Profile; only the uuid field of the Profile object is used.
  */
 Future removeProfile(Profile profile) {
   if (_bluetooth == null) _throwNotAvailable();

   var completer = new ChromeCompleter.noArgs();
   _bluetooth.callMethod('removeProfile', [jsify(profile), completer.callback]);
   return completer.future;
 }

 /**
  * Get information about the Bluetooth adapter.
  * [callback]: Called with an AdapterState object describing the adapter
  * state.
  */
 Future<AdapterState> getAdapterState() {
   if (_bluetooth == null) _throwNotAvailable();

   var completer = new ChromeCompleter<AdapterState>.oneArg(_createAdapterState);
   _bluetooth.callMethod('getAdapterState', [completer.callback]);
   return completer.future;
 }

 /**
  * Get a bluetooth devices known to the system. Known devices are either
  * currently paired, or have been paired in the past.
  * [options]: Controls which devices are returned and provides
  * [deviceCallback], which is called for each matching device.
  * [callback]: Called when the search is completed. |options.deviceCallback|
  * will not be called after [callback] has been called.
  */
 Future getDevices(GetDevicesOptions options) {
   if (_bluetooth == null) _throwNotAvailable();

   var completer = new ChromeCompleter.noArgs();
   _bluetooth.callMethod('getDevices', [jsify(options), completer.callback]);
   return completer.future;
 }

 /**
  * Returns the set of exported profiles for the device specified in options.
  * This function will not initiate a connection to the remote device.
  */
 Future<List<Profile>> getProfiles(GetProfilesOptions options) {
   if (_bluetooth == null) _throwNotAvailable();

   var completer = new ChromeCompleter<List<Profile>>.oneArg((e) => listify(e, _createProfile));
   _bluetooth.callMethod('getProfiles', [jsify(options), completer.callback]);
   return completer.future;
 }

 /**
  * Get a list of services provided by a device.
  */
 Future<List<ServiceRecord>> getServices(GetServicesOptions options) {
   if (_bluetooth == null) _throwNotAvailable();

   var completer = new ChromeCompleter<List<ServiceRecord>>.oneArg((e) => listify(e, _createServiceRecord));
   _bluetooth.callMethod('getServices', [jsify(options), completer.callback]);
   return completer.future;
 }

 /**
  * Connect to a service on a device.
  * [options]: The options for the connection.
  * [callback]: Called to indicate success or failure.
  */
 Future connect(ConnectOptions options) {
   if (_bluetooth == null) _throwNotAvailable();

   var completer = new ChromeCompleter.noArgs();
   _bluetooth.callMethod('connect', [jsify(options), completer.callback]);
   return completer.future;
 }

 /**
  * Close a Bluetooth connection.
  * [options]: The options for this function.
  * [callback]: Called to indicate success or failure.
  */
 Future disconnect(DisconnectOptions options) {
   if (_bluetooth == null) _throwNotAvailable();

   var completer = new ChromeCompleter.noArgs();
   _bluetooth.callMethod('disconnect', [jsify(options), completer.callback]);
   return completer.future;
 }

 /**
  * Read data from a Bluetooth connection.
  * [options]: The options for this function.
  * [callback]: Called with the data when it is available.
  */
 Future<ArrayBuffer> read(ReadOptions options) {
   if (_bluetooth == null) _throwNotAvailable();

   var completer = new ChromeCompleter<ArrayBuffer>.oneArg(_createArrayBuffer);
   _bluetooth.callMethod('read', [jsify(options), completer.callback]);
   return completer.future;
 }

 /**
  * Write data to a Bluetooth connection.
  * [options]: The options for this function.
  * [callback]: Called with the number of bytes written.
  */
 Future<int> write(WriteOptions options) {
   if (_bluetooth == null) _throwNotAvailable();

   var completer = new ChromeCompleter<int>.oneArg();
   _bluetooth.callMethod('write', [jsify(options), completer.callback]);
   return completer.future;
 }

 /**
  * Get the local Out of Band Pairing data.
  * [callback]: Called with the data.
  */
 Future<OutOfBandPairingData> getLocalOutOfBandPairingData() {
   if (_bluetooth == null) _throwNotAvailable();

   var completer = new ChromeCompleter<OutOfBandPairingData>.oneArg(_createOutOfBandPairingData);
   _bluetooth.callMethod('getLocalOutOfBandPairingData', [completer.callback]);
   return completer.future;
 }

 /**
  * Set the Out of Band Pairing data for a remote device. Any previous Out Of
  * Band Pairing Data for this device is overwritten.
  * [options]: The options for this function.
  * [callback]: Called to indicate success or failure.
  */
 Future setOutOfBandPairingData(SetOutOfBandPairingDataOptions options) {
   if (_bluetooth == null) _throwNotAvailable();

   var completer = new ChromeCompleter.noArgs();
   _bluetooth.callMethod('setOutOfBandPairingData', [jsify(options), completer.callback]);
   return completer.future;
 }

 /**
  * Start discovery. Discovered devices will be returned via the
  * [onDeviceDiscovered] callback. Discovery will fail to start if it is
  * already in progress. Discovery can be resource intensive: stopDiscovery
  * should be called as soon as possible.
  * [options]: The options for this function.
  * [callback]: Called to indicate success or failure.
  */
 Future startDiscovery(StartDiscoveryOptions options) {
   if (_bluetooth == null) _throwNotAvailable();

   var completer = new ChromeCompleter.noArgs();
   _bluetooth.callMethod('startDiscovery', [jsify(options), completer.callback]);
   return completer.future;
 }

 /**
  * Stop discovery.
  * [callback]: Called to indicate success or failure.
  */
 Future stopDiscovery() {
   if (_bluetooth == null) _throwNotAvailable();

   var completer = new ChromeCompleter.noArgs();
   _bluetooth.callMethod('stopDiscovery', [completer.callback]);
   return completer.future;
 }

 Stream<AdapterState> get onAdapterStateChanged => _onAdapterStateChanged.stream;

 final ChromeStreamController<AdapterState> _onAdapterStateChanged =
     new ChromeStreamController<AdapterState>.oneArg(_bluetooth, 'onAdapterStateChanged', _createAdapterState);

 Stream<Socket> get onConnection => _onConnection.stream;

 final ChromeStreamController<Socket> _onConnection =
     new ChromeStreamController<Socket>.oneArg(_bluetooth, 'onConnection', _createSocket);

 void _throwNotAvailable() {
   throw new UnsupportedError("'chrome.bluetooth' is not available");
 }
}

Extends

ChromeApi > ChromeBluetooth

Properties

final bool available #

Returns true if the API is available. The common causes of an API not being avilable are:

  • a permission is missing in the application's manifest.json file
  • the API is defined on a newer version of Chrome then the current runtime
docs inherited from ChromeApi
bool get available => _bluetooth != null;

final Stream<AdapterState> onAdapterStateChanged #

Stream<AdapterState> get onAdapterStateChanged => _onAdapterStateChanged.stream;

final Stream<Socket> onConnection #

Stream<Socket> get onConnection => _onConnection.stream;

Methods

Future addProfile(Profile profile) #

Registers the JavaScript application as an implementation for the given Profile; if a channel or PSM is specified, the profile will be exported in the host's SDP and GATT tables and advertised to other devices.

Future addProfile(Profile profile) {
 if (_bluetooth == null) _throwNotAvailable();

 var completer = new ChromeCompleter.noArgs();
 _bluetooth.callMethod('addProfile', [jsify(profile), completer.callback]);
 return completer.future;
}

Future connect(ConnectOptions options) #

Connect to a service on a device. options: The options for the connection. callback: Called to indicate success or failure.

Future connect(ConnectOptions options) {
 if (_bluetooth == null) _throwNotAvailable();

 var completer = new ChromeCompleter.noArgs();
 _bluetooth.callMethod('connect', [jsify(options), completer.callback]);
 return completer.future;
}

Future disconnect(DisconnectOptions options) #

Close a Bluetooth connection. options: The options for this function. callback: Called to indicate success or failure.

Future disconnect(DisconnectOptions options) {
 if (_bluetooth == null) _throwNotAvailable();

 var completer = new ChromeCompleter.noArgs();
 _bluetooth.callMethod('disconnect', [jsify(options), completer.callback]);
 return completer.future;
}

Future<AdapterState> getAdapterState() #

Get information about the Bluetooth adapter. callback: Called with an AdapterState object describing the adapter state.

Future<AdapterState> getAdapterState() {
 if (_bluetooth == null) _throwNotAvailable();

 var completer = new ChromeCompleter<AdapterState>.oneArg(_createAdapterState);
 _bluetooth.callMethod('getAdapterState', [completer.callback]);
 return completer.future;
}

Future getDevices(GetDevicesOptions options) #

Get a bluetooth devices known to the system. Known devices are either currently paired, or have been paired in the past. options: Controls which devices are returned and provides deviceCallback, which is called for each matching device. callback: Called when the search is completed. |options.deviceCallback| will not be called after callback has been called.

Future getDevices(GetDevicesOptions options) {
 if (_bluetooth == null) _throwNotAvailable();

 var completer = new ChromeCompleter.noArgs();
 _bluetooth.callMethod('getDevices', [jsify(options), completer.callback]);
 return completer.future;
}

Future<OutOfBandPairingData> getLocalOutOfBandPairingData() #

Get the local Out of Band Pairing data. callback: Called with the data.

Future<OutOfBandPairingData> getLocalOutOfBandPairingData() {
 if (_bluetooth == null) _throwNotAvailable();

 var completer = new ChromeCompleter<OutOfBandPairingData>.oneArg(_createOutOfBandPairingData);
 _bluetooth.callMethod('getLocalOutOfBandPairingData', [completer.callback]);
 return completer.future;
}

Future<List<Profile>> getProfiles(GetProfilesOptions options) #

Returns the set of exported profiles for the device specified in options. This function will not initiate a connection to the remote device.

Future<List<Profile>> getProfiles(GetProfilesOptions options) {
 if (_bluetooth == null) _throwNotAvailable();

 var completer = new ChromeCompleter<List<Profile>>.oneArg((e) => listify(e, _createProfile));
 _bluetooth.callMethod('getProfiles', [jsify(options), completer.callback]);
 return completer.future;
}

Future<List<ServiceRecord>> getServices(GetServicesOptions options) #

Get a list of services provided by a device.

Future<List<ServiceRecord>> getServices(GetServicesOptions options) {
 if (_bluetooth == null) _throwNotAvailable();

 var completer = new ChromeCompleter<List<ServiceRecord>>.oneArg((e) => listify(e, _createServiceRecord));
 _bluetooth.callMethod('getServices', [jsify(options), completer.callback]);
 return completer.future;
}

Future<ArrayBuffer> read(ReadOptions options) #

Read data from a Bluetooth connection. options: The options for this function. callback: Called with the data when it is available.

Future<ArrayBuffer> read(ReadOptions options) {
 if (_bluetooth == null) _throwNotAvailable();

 var completer = new ChromeCompleter<ArrayBuffer>.oneArg(_createArrayBuffer);
 _bluetooth.callMethod('read', [jsify(options), completer.callback]);
 return completer.future;
}

Future removeProfile(Profile profile) #

Unregisters the JavaScript application as an implementation for the given Profile; only the uuid field of the Profile object is used.

Future removeProfile(Profile profile) {
 if (_bluetooth == null) _throwNotAvailable();

 var completer = new ChromeCompleter.noArgs();
 _bluetooth.callMethod('removeProfile', [jsify(profile), completer.callback]);
 return completer.future;
}

Future setOutOfBandPairingData(SetOutOfBandPairingDataOptions options) #

Set the Out of Band Pairing data for a remote device. Any previous Out Of Band Pairing Data for this device is overwritten. options: The options for this function. callback: Called to indicate success or failure.

Future setOutOfBandPairingData(SetOutOfBandPairingDataOptions options) {
 if (_bluetooth == null) _throwNotAvailable();

 var completer = new ChromeCompleter.noArgs();
 _bluetooth.callMethod('setOutOfBandPairingData', [jsify(options), completer.callback]);
 return completer.future;
}

Future startDiscovery(StartDiscoveryOptions options) #

Start discovery. Discovered devices will be returned via the onDeviceDiscovered callback. Discovery will fail to start if it is already in progress. Discovery can be resource intensive: stopDiscovery should be called as soon as possible. options: The options for this function. callback: Called to indicate success or failure.

Future startDiscovery(StartDiscoveryOptions options) {
 if (_bluetooth == null) _throwNotAvailable();

 var completer = new ChromeCompleter.noArgs();
 _bluetooth.callMethod('startDiscovery', [jsify(options), completer.callback]);
 return completer.future;
}

Future stopDiscovery() #

Stop discovery. callback: Called to indicate success or failure.

Future stopDiscovery() {
 if (_bluetooth == null) _throwNotAvailable();

 var completer = new ChromeCompleter.noArgs();
 _bluetooth.callMethod('stopDiscovery', [completer.callback]);
 return completer.future;
}

Future<int> write(WriteOptions options) #

Write data to a Bluetooth connection. options: The options for this function. callback: Called with the number of bytes written.

Future<int> write(WriteOptions options) {
 if (_bluetooth == null) _throwNotAvailable();

 var completer = new ChromeCompleter<int>.oneArg();
 _bluetooth.callMethod('write', [jsify(options), completer.callback]);
 return completer.future;
}