Bare Docs

bare-bluetooth

Reference for bare-bluetooth: cross-platform Bluetooth LE for Bare over the Apple, Android and Linux bindings.

Documented against v0.6.5
experimental

bare-bluetooth provides Bluetooth bindings for Bare: BLE central and peripheral roles, GATT services and characteristics, and L2CAP channels, across Apple, Android and Linux. It normalizes the differences between the platforms so that consumer code needs no platform conditionals: state strings, class names and constants are unified. It resolves, per platform, to bare-bluetooth-apple, bare-bluetooth-android or bare-bluetooth-linux.

Upstream marks this module experimental: its API is subject to change and may break at any time.

This page describes 0.6.4, the release npm installs as latest. It raises the minimum versions of the Apple and Android bindings. 0.6.3 added the Linux backend: 0.6.2 and earlier support Apple and Android only.

npm i bare-bluetooth

Usage

The example below shows a peripheral advertising a single writable, notifying characteristic and a central that scans, connects, subscribes, and exchanges data with it. It also uses bare-encoding, so install that too.

Peripheral:

const { TextEncoder, TextDecoder } = require('bare-encoding')
const { Server, Service, Characteristic } = require('bare-bluetooth')

const SERVICE_UUID = '01230000-0000-1000-8000-00805F9B34FB'
const CHAR_UUID = '01230001-0000-1000-8000-00805F9B34FB'

const server = new Server()
let pingChar = null

server.on('stateChange', (state) => {
  if (state !== 'poweredOn') return

  pingChar = new Characteristic(CHAR_UUID, {
    write: true,
    notify: true
  })

  server.addService(new Service(SERVICE_UUID, [pingChar]))
})

server.on('serviceAdd', (uuid) => {
  server.startAdvertising({
    name: 'MyDevice',
    serviceUUIDs: [SERVICE_UUID]
  })
})

server.on('writeRequest', (requests) => {
  const request = requests[0]
  const message = new TextDecoder().decode(request.data)

  server.respondToRequest(request, Server.ATT_SUCCESS, null)
  server.updateValue(pingChar, new TextEncoder().encode('pong: ' + message))
})

Central:

const { TextEncoder, TextDecoder } = require('bare-encoding')
const { Central } = require('bare-bluetooth')

const SERVICE_UUID = '01230000-0000-1000-8000-00805F9B34FB'
const CHAR_UUID = '01230001-0000-1000-8000-00805F9B34FB'

// Platforms report UUIDs in different forms: Apple uppercase, and in their
// short form for UUIDs on the Bluetooth base; Android and Linux long and
// lowercase. Expand both sides before comparing.
function expand(uuid) {
  uuid = uuid.toLowerCase()
  if (uuid.length === 4) return `0000${uuid}-0000-1000-8000-00805f9b34fb`
  if (uuid.length === 8) return `${uuid}-0000-1000-8000-00805f9b34fb`
  return uuid
}

const central = new Central()
let connecting = false

central.on('stateChange', (state) => {
  if (state !== 'poweredOn') return

  central.startScan([SERVICE_UUID])
})

central.on('discover', (peripheral) => {
  if (connecting || peripheral.rssi === null) return

  connecting = true
  central.stopScan()
  central.connect(peripheral)
})

central.on('connect', (peripheral) => {
  peripheral.on('servicesDiscover', (services) => {
    for (const service of services) {
      if (expand(service.uuid) === expand(SERVICE_UUID)) {
        peripheral.discoverCharacteristics(service)
      }
    }
  })

  peripheral.on('characteristicsDiscover', (service, characteristics) => {
    for (const characteristic of characteristics) {
      if (expand(characteristic.uuid) === expand(CHAR_UUID)) {
        peripheral.subscribe(characteristic)
      }
    }
  })

  peripheral.on('notifyState', (characteristic, isNotifying) => {
    if (isNotifying) {
      peripheral.write(characteristic, new TextEncoder().encode('ping'))
    }
  })

  peripheral.on('notify', (characteristic, data) => {
    console.log('received:', new TextDecoder().decode(data))
  })

  peripheral.discoverServices([SERVICE_UUID])
})

The upstream README compares UUIDs with ===, which fails on every platform for these UUIDs; the expand() helper and the discover guard here come from the repository's own example/gattpong.js. That example/ directory also holds scan.js and l2cappong.js, runnable with bare on macOS and Linux.

Platforms

The package resolves to a platform-specific implementation:

There is no fallback, so require('bare-bluetooth') fails on any other platform, such as Windows. 0.6.4 depends on bare-bluetooth-android ^0.11.6, bare-bluetooth-apple ^0.8.0 and bare-bluetooth-linux ^0.9.2.

On Android the module only works inside an app that hosts Bare, since it needs the app's Java VM. The host has to publish the VM under the bare.android.jvm.v1 context key. Bare Kit does so from 2.5.0, so a Bare Kit worklet needs Bare Kit 2.5.0 or later.

On an Android device with no Bluetooth adapter, new Central() and new Server() do not throw. They report 'unsupported' through stateChange, so central.state and server.state read 'unsupported'. From then on central.startScan() throws NOT_POWERED_ON, connect(), disconnect(), addService(), startAdvertising(), respondToRequest() and publishChannel() emit error with that code instead, and the remaining calls do nothing.

Types

BluetoothState

A string describing the current Bluetooth adapter state.

ValuePlatforms
'unknown'Apple
'resetting'Apple
'unsupported'Android, Apple, Linux
'unauthorized'Apple
'poweredOff'Android, Apple, Linux
'poweredOn'Android, Apple, Linux
'turningOn'Android
'turningOff'Android

BluetoothError

Thrown when a call cannot be made at all, with code saying why. This package raises two codes: NOT_POWERED_ON, thrown by startScan (and, on an Android device with no Bluetooth adapter, emitted on error by other calls), and NOT_INITIATED, emitted on error when a GATT operation could not be sent. Only Android raises NOT_INITIATED, since only the Android backend queues GATT operations.

Other failures that happen after a call returns arrive on the error event too. Those come from the platform package, carry their own codes, and are not instances of this class.

API

Central

const central = new Central([options])

Create a new BLE central manager. The central scans for and connects to peripherals. Apple and Android take no options. On Linux, options is passed to the Adapter of bare-bluetooth-linux, for example { path: '/org/bluez/hci1' } to pick an adapter.

Properties

PropertyTypeDescription
stateBluetoothStateCurrent Bluetooth adapter state

Methods

central.startScan([serviceUUIDs[, options]])

Start scanning for peripherals. If serviceUUIDs is provided, only peripherals advertising those services will be discovered.

options = {
  allowDuplicates: false, // Apple only
  scanMode: Central.SCAN_MODE_LOW_LATENCY, // Android only
  callbackType: Central.CALLBACK_TYPE_FIRST_MATCH, // Android only
  transport: 'le' // Linux only
}

Each option goes to the platform that understands it. Both platforms decide how often discover fires, but spell it differently, so set both to get the same behaviour everywhere.

Set allowDuplicates (Apple) to true for a discover on every advertising packet. false asks CoreBluetooth to coalesce them, which it only does as a best effort: a peripheral still turns up several times per scan.

discover reports what the platform reports, with no filtering of its own. Key peripherals by id, keep the latest, and de-duplicate there if you need to.

Set transport (Linux) to 'le' (default), 'auto', or 'bredr'. 'auto' also scans classic, where dual-mode devices such as TVs advertise their name, but slows BLE discovery; a dual-mode device shows up once per channel, and only the BLE one can be connected.

On Linux the name may arrive after the first discover; when it does, discover fires again with name set. BlueZ also replays devices it cached from earlier scans, which have no rssi: a null rssi means this scan has not heard the device.

Set callbackType (Android) to one of Central.CALLBACK_TYPE_ALL_MATCHES, Central.CALLBACK_TYPE_FIRST_MATCH, or Central.CALLBACK_TYPE_MATCH_LOST. ALL_MATCHES is the default and fires on every advertising packet, dozens per second per peripheral. FIRST_MATCH fires once per peripheral, but needs serviceUUIDs and hardware support: without it the scan fails with an error, so keep a fallback. It also stops rssi refreshing.

Set scanMode (Android) to one of Central.SCAN_MODE_OPPORTUNISTIC, Central.SCAN_MODE_LOW_POWER, Central.SCAN_MODE_BALANCED, or Central.SCAN_MODE_LOW_LATENCY.

Throws a BluetoothError with code NOT_POWERED_ON unless central.state is 'poweredOn'.

central.stopScan()

Stop scanning for peripherals. Does nothing unless central.state is 'poweredOn', since the scan is already gone.

central.connect(peripheral)

Connect to a DiscoveredPeripheral.

central.disconnect(peripheral)

Disconnect from a connected Peripheral.

It also accepts the DiscoveredPeripheral passed to central.connect(), which hangs up a connection attempt that has not finished. Before v0.6.5, on Linux, passing a DiscoveredPeripheral threw a TypeError (_disconnect is not a function).

central.destroy()

Destroy the central manager and release all resources.

Events

EventArgumentsDescription
stateChangestate: BluetoothStateBluetooth adapter state changed
discoverperipheral: DiscoveredPeripheralA peripheral was found during scanning
connectperipheral: PeripheralConnection to a peripheral established
disconnectperipheral: Peripheral | nullA peripheral disconnected cleanly
errorerror: ErrorAn error occurred
EventArgumentsPlatform
pairRequestrequest: PairRequestLinux

On Apple and Android, a connection attempt that fails, and a disconnect that carries an error, arrive as error (codes CONNECTION_FAILED and DISCONNECT) rather than as disconnect. On Linux, a connection attempt that fails also arrives as error with code CONNECTION_FAILED, but a dropped link arrives as disconnect, since BlueZ reports only that the device is no longer connected. The module sets no connection timeout of its own: on Apple and Android, give up on an attempt by calling central.disconnect() with the peripheral passed to connect().

Constants

Android only. undefined on other platforms.

ConstantDescription
Central.SCAN_MODE_OPPORTUNISTICScan only while other apps are scanning
Central.SCAN_MODE_LOW_POWERLow power scan mode
Central.SCAN_MODE_BALANCEDBalanced scan mode
Central.SCAN_MODE_LOW_LATENCYLow latency scan mode
Central.CALLBACK_TYPE_ALL_MATCHESEmit discover for every advertising packet (default)
Central.CALLBACK_TYPE_FIRST_MATCHEmit discover once per peripheral
Central.CALLBACK_TYPE_MATCH_LOSTEmit when a peripheral stops advertising

DiscoveredPeripheral

Represents a peripheral found during scanning. Emitted by the discover event on Central. Pass it directly to central.connect(). Its id is a per-host UUID on Apple, which hides the real address, and the Bluetooth address on Android and Linux.

Properties

PropertyTypeDescription
idstringUnique identifier of the peripheral
namestring | nullAdvertised name, or null
rssinumber | nullSignal strength in dBm
serviceData{ [uuid: string]: Uint8Array } | nullService data from advertisement, or null

Peripheral

Represents a connected peripheral. Obtained from the connect event on Central.

Properties

PropertyTypeDescription
idstringUnique identifier of the peripheral
namestring | nullAdvertised name, or null. On Apple and Android, falls back to the device's own name once connected
serviceData{ [uuid: string]: Uint8Array } | nullService data, or null

Methods

peripheral.discoverServices([serviceUUIDs])

Discover services on the peripheral. On Apple and Linux, an optional serviceUUIDs array restricts discovery to those services. Android always discovers all services. Results are emitted via servicesDiscover.

peripheral.discoverCharacteristics(service[, characteristicUUIDs])

Discover characteristics for a Service. On Apple and Linux, an optional characteristicUUIDs array restricts discovery. Android always discovers all characteristics. Results are emitted via characteristicsDiscover.

peripheral.read(characteristic)

Read the value of a Characteristic. The result is emitted via read. While subscribed, Linux also emits a notify with the same value, and Apple emits notify instead of read.

peripheral.write(characteristic, data[, withResponse])

Write data: Uint8Array to a Characteristic. If withResponse is true (the default), the write is confirmed by the peripheral.

peripheral.subscribe(characteristic)

Subscribe to notifications for a Characteristic.

peripheral.unsubscribe(characteristic)

Unsubscribe from notifications for a Characteristic.

peripheral.openL2CAPChannel(psm)

Open an L2CAP channel to the peripheral using the given psm: number. The result is emitted via channelOpen.

peripheral.requestMtu(mtu)

Request a new MTU size (mtu: number). The result is emitted via mtuChanged. No-op on Apple and Linux, which negotiate the MTU themselves.

peripheral.destroy()

Destroy the peripheral instance and release resources.

Events

EventArgumentsDescription
servicesDiscoverservices: Service[]Services discovered
characteristicsDiscoverservice: Service | null, characteristics: Characteristic[]Characteristics discovered for a service
readcharacteristic: Characteristic, data: Uint8ArrayCharacteristic value read
writecharacteristic: CharacteristicCharacteristic write completed
notifycharacteristic: Characteristic, data: Uint8ArrayNotification received
notifyStatecharacteristic: Characteristic, isNotifying: booleanNotification state changed
channelOpenchannel: L2CAPChannelL2CAP channel opened
errorerror: ErrorAn error occurred
EventArgumentsPlatform
disconnect(none)Android, Linux
mtuChangedmtu: numberAndroid

Constants

ConstantValue
Peripheral.PROPERTY_READ0x02
Peripheral.PROPERTY_WRITE_WITHOUT_RESPONSE0x04
Peripheral.PROPERTY_WRITE0x08
Peripheral.PROPERTY_NOTIFY0x10
Peripheral.PROPERTY_INDICATE0x20

Server

const server = new Server([options])

Create a new BLE peripheral manager (server). The server advertises services and handles read/write requests from centrals. As with Central, options only applies on Linux.

On Android the GATT server is opened each time the adapter reaches 'poweredOn' and closed when it leaves it, which drops its services. Add services in your stateChange handler on every transition to 'poweredOn', as the usage example does.

Properties

PropertyTypeDescription
stateBluetoothStateCurrent Bluetooth adapter state

Methods

server.addService(service)

Add a Service to the GATT server. The serviceAdd event is emitted when the service has been registered.

server.startAdvertising([options])

Start advertising the server.

options = {
  name: null,
  serviceUUIDs: null,
  serviceData: null // Apple and Linux only
}

On Android, name only switches on the device's own Bluetooth name in the advertisement; the string itself is not used.

server.stopAdvertising()

Stop advertising.

server.respondToRequest(request, result[, data])

Respond to a ReadRequest or WriteRequest with the given ATT result: number code. Optionally include data: Uint8Array for read responses. Use the Server.ATT_* constants for result.

On Android every read reaches readRequest, even for a characteristic created with a value, so answer each one. On Apple, the system answers reads of a characteristic created with a value itself. On Linux, a result BlueZ has no name for (ATT_INVALID_HANDLE, ATT_INSUFFICIENT_RESOURCES) reaches the central as ATT_UNLIKELY_ERROR, a read nobody listens for is answered with the characteristic's value, and a write nobody listens for is accepted.

server.updateValue(characteristic, data)

Update the value of a Characteristic and notify subscribed centrals. data is a Uint8Array. Returns true if the update was sent successfully. On Apple, false means the transmit queue is full: send again after readyToUpdate.

server.publishChannel([options])

Publish an L2CAP channel. The channelPublish event is emitted with the assigned PSM. The operating system picks the PSM, so a central has to learn it some other way, for example by reading it from a characteristic.

options = {
  encrypted: false
}

encrypted: true requires the devices to be paired. On Linux, options.psm asks for a specific PSM instead of an assigned one.

server.unpublishChannel(psm)

Unpublish a previously published L2CAP channel identified by psm: number.

server.removeAllServices()

Remove every service previously added with server.addService(). Apple and Linux only. undefined on Android.

server.destroy()

Destroy the server and release all resources.

Events

EventArgumentsDescription
stateChangestate: BluetoothStateBluetooth adapter state changed
serviceAdduuid: stringService registered
readRequestrequest: ReadRequestCentral read a characteristic
writeRequestrequests: WriteRequest[]Central wrote to a characteristic
subscribepeer: unknown, characteristicUuid: stringCentral subscribed to notifications
unsubscribepeer: unknown, characteristicUuid: stringCentral unsubscribed from notifications
errorerror: ErrorAn error occurred
channelPublishpsm: numberL2CAP channel published
channelOpenchannel: L2CAPChannelL2CAP channel opened by a central

peer is an opaque handle on Apple, the device address on Android, and null on Linux: BlueZ does not say which central toggled notifications.

EventArgumentsPlatform
connectingdeviceAddress: stringAndroid
connecteddeviceAddress: stringAndroid, Linux
disconnectingdeviceAddress: stringAndroid
disconnecteddeviceAddress: stringAndroid, Linux
notifySentdeviceAddress: string, status: numberAndroid
readyToUpdate(none)Apple
pairRequestrequest: PairRequestLinux

Constants

State constants

ConstantValue
Server.STATE_UNKNOWN0
Server.STATE_RESETTING1
Server.STATE_UNSUPPORTED2
Server.STATE_UNAUTHORIZED3
Server.STATE_POWERED_OFF4
Server.STATE_POWERED_ON5

Connection state constants (Android)

ConstantValue
Server.CONNECTION_STATE_DISCONNECTED0
Server.CONNECTION_STATE_CONNECTING1
Server.CONNECTION_STATE_CONNECTED2
Server.CONNECTION_STATE_DISCONNECTING3

Property constants

ConstantValue
Server.PROPERTY_READ0x02
Server.PROPERTY_WRITE_WITHOUT_RESPONSE0x04
Server.PROPERTY_WRITE0x08
Server.PROPERTY_NOTIFY0x10
Server.PROPERTY_INDICATE0x20

Permission constants

The values differ on Linux, so build bitmasks from the constants rather than from numbers.

ConstantApple, AndroidLinux
Server.PERMISSION_READABLE0x010x01
Server.PERMISSION_WRITEABLE0x020x10
Server.PERMISSION_READ_ENCRYPTED0x040x02
Server.PERMISSION_WRITE_ENCRYPTED0x080x20

ATT result codes

ConstantValue
Server.ATT_SUCCESS0x00
Server.ATT_INVALID_HANDLE0x01
Server.ATT_READ_NOT_PERMITTED0x02
Server.ATT_WRITE_NOT_PERMITTED0x03
Server.ATT_INSUFFICIENT_RESOURCES0x11
Server.ATT_UNLIKELY_ERROR0x0E

ReadRequest

Represents a read request from a central. Emitted by the readRequest event on Server. Pass it directly to server.respondToRequest().

Properties

PropertyTypeDescription
characteristicUuidstringUUID of the characteristic being read
offsetnumberByte offset for the read

WriteRequest

Represents a write request from a central. Emitted as an array by the writeRequest event on Server. Pass it directly to server.respondToRequest().

Properties

PropertyTypeDescription
characteristicUuidstringUUID of the characteristic being written
offsetnumberByte offset for the write
dataUint8ArrayData being written
responseNeededbooleanWhether the central expects a response

PairRequest

Linux only. A peer wants to pair. Emitted by the pairRequest event on Central and Server, and refused when nothing is listening.

Apple and Android hand the decision to the operating system, which prompts the user; BlueZ has no prompt to fall back on, so the app answers instead. Pairing is always "just works": a machine with no screen and no keyboard cannot compare a passkey, so there is nothing else to answer. Use bare-bluetooth-linux directly for BlueZ's other pairing modes.

Properties

PropertyTypeDescription
deviceAddressstringAddress of the peer
namestring | nullName of the peer, if it has one

Methods

request.accept()

Allow the pairing. The first answer wins; a later one does nothing.

request.reject()

Refuse the pairing.

L2CAPChannel

An L2CAP connection-oriented channel. Obtained through the channelOpen event on Server or Peripheral. Extends Duplex from bare-stream and supports standard readable and writable stream operations.

Properties

PropertyTypeDescription
psmnumberProtocol/Service Multiplexer number
peerstring | nullRemote device identifier, or null if unknown

The value of peer is platform-specific. On Apple it is the OS-assigned peer UUID (CBPeer.identifier); the system hides the real Bluetooth address. On Android and Linux it is the remote device's Bluetooth address (for example AA:BB:CC:DD:EE:FF). It matches the identifier reported by device discovery on the same platform, so it can be used to correlate an accepted channel with a known device.

Listen for error on the channel: a write to a peer that has gone destroys the stream rather than throwing, and an unhandled error throws. On Linux the channel is the L2CAPChannel of bare-bluetooth-linux, which also has an mtu property: a single write larger than mtu fails.

Service

const service = new Service(uuid[, characteristics[, options]])

Create a GATT service definition.

ParameterTypeDefaultDescription
uuidstringService UUID
characteristicsCharacteristic[][]Characteristics for this service
options.primarybooleantrueWhether this is a primary service

Properties

PropertyTypeDescription
uuidstringUUID of the service
characteristicsCharacteristic[]Characteristics belonging to the service
primarybooleanWhether this is a primary service

Characteristic

const characteristic = new Characteristic(uuid[, options])

Create a GATT characteristic definition.

ParameterTypeDefaultDescription
uuidstringCharacteristic UUID
options.readbooleanfalseEnable read property
options.writebooleanfalseEnable write property
options.writeWithoutResponsebooleanfalseEnable write-without-response property
options.notifybooleanfalseEnable notify property
options.indicatebooleanfalseEnable indicate property
options.permissionsnumber | nullnullExplicit permission bitmask. If null, inferred from properties
options.valueUint8Array | nullnullStatic value

Properties

PropertyTypeDescription
uuidstringUUID of the characteristic
propertiesnumberBitmask of characteristic properties
permissionsnumber | nullBitmask of permissions, or null if inferred
valueUint8Array | nullStatic value. Setting it on a discovered characteristic throws

Constants

ConstantValue
Characteristic.PROPERTY_READ0x02
Characteristic.PROPERTY_WRITE_WITHOUT_RESPONSE0x04
Characteristic.PROPERTY_WRITE0x08
Characteristic.PROPERTY_NOTIFY0x10
Characteristic.PROPERTY_INDICATE0x20

See also

Last updated on

Was this helpful?

On this page