BT

BT

new BT(runtime, extensionId, peripheralOptions, connectCallback, resetCallback, messageCallback)

A BT peripheral socket object. It handles connecting, over web sockets, to BT peripherals, and reading and writing data to them.

Parameters:
Name Type Default Description
runtime Runtime

the Runtime for sending/receiving GUI update events.

extensionId string

the id of the extension using this socket.

peripheralOptions object

the list of options for peripheral discovery.

connectCallback object

a callback for connection.

resetCallback object null

a callback for resetting extension state.

messageCallback object

a callback for message sending.

Methods

connectPeripheral(id, pin)

Try connecting to the input peripheral id, and then call the connect callback if connection is successful.

Parameters:
Name Type Default Description
id number

the id of the peripheral to connect to

pin string null

an optional pin for pairing

didReceiveCall(method, params) → {object}

Handle a received call from the socket.

Parameters:
Name Type Description
method string

a received method label.

params object

a received list of parameters.

Returns:
  • optional return value.
Type
object

disconnect()

Close the websocket.

handleDisconnectError()

Handle an error resulting from losing connection to a peripheral.

This could be due to:

  • battery depletion
  • going out of bluetooth range
  • being powered down

Disconnect the socket, and if the extension using this socket has a reset callback, call it. Finally, emit an error to the runtime.

isConnected() → {bool}

Returns:

whether the peripheral is connected.

Type
bool

requestPeripheral()

Request connection to the peripheral. If the web socket is not yet open, request when the socket promise resolves.