Class Central

The manager for creating client connections.

Class summary


Constructors

Methods

Returns a list of device identifiers that have been bonded. The elements of the list can be used as arguments to connect.
NOTE: Not implemented on MacOS.

close -> none

connect identifier/any --secure/bool=false -> RemoteDevice
Connects to the remote device with the given identifier.
Connections cannot be established while a scan is ongoing.
If secure is true, the connections is secured and the remote peer is bonded.

operator == other/any -> bool
Whether this object is equal to the other.
By default, identical is used for equality.
Inheritance
Classes overwrite this operator to get an equality specific to their needs. Equality operators often compare the type and field contents. For example:

class Pin:
  number/int

  constructor .number:

  operator == other:
    if other is not Pin: return false
    return number == other.number
A class doesn't have to follow the above format, but it must keep the operator in sync with any hash-code method. That is, if a class has a hash-code member, then the equality and hash-code must agree. If two instances are equal (a == b), then their hash codes must also be equal (a.hash-code == b.hash-code).

scan --interval/int=0 --window/int=0 --duration/Duration=null --limited-only/bool=false --active/bool=false [block] -> none
Scans for nearby devices. This method blocks while the scan is ongoing.
Only one scan can run at a time.
If active is true, then we request a scan response from discovered devices. Users may need to merge the advertisement data from the scan response with the advertisement data from the discovery event. Use RemoteScannedDevice.is-scan-response to distinguish between the two.
Connections cannot be established while a scan is ongoing.
Stops the scan after the given duration.
The interval is the time between the start of two consecutive scan windows. It is given in units of 0.625 ms. If it is 0, the default value of the system is used.
The window is the time the scanner is active during a scan window. It is given in units of 0.625 ms. If it is 0, the default value of the system is used. The window must be less than or equal to the interval.
If limited-only is true, only limited discoverable devices are reported. Devices declare during advertising whether they are limited (advertising for a limited duration) or general (continuously broadcasting). This flag filters out general devices.
Merging advertisements
When active is true, then there are two calls to the block for each device. The first call is for the discovery event, and the second call is for the scan response event. It is up to the user to merge the advertisement data from the two calls.
A simple way to merge advertisement data is to concatenate the DataBlock entries of the advertisements.

discovered-blocks := {:}
central.scan --duration=(Duration --s=2) --active: | device/RemoteScannedDevice |
  blocks := discovered-blocks.get device.identifier --init=: {}
  blocks.add-all device.data.data-blocks
// Construct a map from identifier to the discovered advertisements.
discovered-advertisements := discovered-blocks.map: | _ blocks |
  Advertisement blocks.to-list --no-check-size

Stringifies this object.
Inheritance
Objects that need a human-friendly string representation should overwrite this method. The default string is based on the internal class-ID.