LogoPear Docs
ReferencesBareModules

bare-bluetooth-android

Android Bluetooth bindings for Bare

stable

bare-bluetooth-android — Android Bluetooth bindings for Bare. It is a native addon.

npm i bare-bluetooth-android

Usage

const { Central } = require('bare-bluetooth-android')

const central = new Central()

central.on('stateChange', (state) => {
  if (state === 'on') {
    central.startScan(['180D']) // Heart Rate service UUID
  }
})

central.on('discover', (peripheral) => {
  console.log('Found:', peripheral.name, peripheral.id)
  central.stopScan()
  central.connect(peripheral)
})

central.on('connect', (peripheral) => {
  peripheral.discoverServices()

  peripheral.on('servicesDiscover', (services) => {
    // Discover characteristics for each service
  })
})

API

BluetoothError

new BluetoothError(msg: string, fn?: Function, code?: string)

Source

Parameters

ParameterTypeDefaultDescription
msgstringA human-readable error message.
fn?FunctionThe function to omit from the captured stack trace (default BluetoothError).
code?stringThe error code; defaults to fn.name (e.g. SCAN_FAILED).

BluetoothError.ADVERTISE_FAILED(msg: string): BluetoothError

Source

Parameters

ParameterTypeDefaultDescription
msgstring

BluetoothError.CHANNEL_FAILED(msg: string): BluetoothError

Source

Parameters

ParameterTypeDefaultDescription
msgstring

BluetoothError.CHANNEL_PUBLISH_FAILED(msg: string): BluetoothError

Source

Parameters

ParameterTypeDefaultDescription
msgstring

BluetoothError.CONNECTION_FAILED(msg: string, id: string): BluetoothError

Source

Parameters

ParameterTypeDefaultDescription
msgstringA human-readable error message.
idstringThe id of the peripheral the connection attempt targeted.

BluetoothError.DISCONNECT(msg: string, id: string): BluetoothError

Source

Parameters

ParameterTypeDefaultDescription
msgstringA human-readable error message.
idstringThe id of the peripheral that disconnected.

BluetoothError.DISCOVER_FAILED(msg: string): BluetoothError

Source

Parameters

ParameterTypeDefaultDescription
msgstring

BluetoothError.MTU_CHANGE_FAILED(msg: string): BluetoothError

Source

Parameters

ParameterTypeDefaultDescription
msgstring

BluetoothError.NOTIFY_FAILED(msg: string): BluetoothError

Source

Parameters

ParameterTypeDefaultDescription
msgstring

BluetoothError.NOTIFY_STATE_FAILED(msg: string): BluetoothError

Source

Parameters

ParameterTypeDefaultDescription
msgstring

BluetoothError.READ_FAILED(msg: string): BluetoothError

Source

Parameters

ParameterTypeDefaultDescription
msgstring

BluetoothError.SCAN_FAILED(msg: string): BluetoothError

Source

Parameters

ParameterTypeDefaultDescription
msgstring

BluetoothError.SERVICE_ADD_FAILED(msg: string): BluetoothError

Source

Parameters

ParameterTypeDefaultDescription
msgstring

BluetoothError.WRITE_FAILED(msg: string): BluetoothError

Source

Parameters

ParameterTypeDefaultDescription
msgstring

code: string

Source

BluetoothError.id: string

Source

name: 'BluetoothError'

Source

L2CAPChannel

new L2CAPChannel(channelHandle: ArrayBuffer)

Source

A duplex stream representing an L2CAP connection-oriented channel. Extends Duplex from <https://github.com/holepunchto/bare-stream>. Typically obtained via the 'channelOpen' event rather than constructed directly.

Parameters

ParameterTypeDefaultDescription
channelHandleArrayBufferThe native channel handle backing the stream; supplied internally when a channel opens, not usually passed directly.

peer: string | null

Source

The address of the remote peer, or null.

psm: number

Source

The Protocol/Service Multiplexer number of the channel.

Service

new Service(uuid: string, characteristics?: Characteristic[], opts?: ServiceOptions)

Source

Create a new GATT service definition.

Parameters

ParameterTypeDefaultDescription
uuidstringThe service's UUID.
characteristics?Characteristic[]The characteristics belonging to the service.
opts?ServiceOptionsOptions; set primary: true to mark this a primary service.

characteristics: Characteristic[]

Source

The array of characteristics belonging to the service.

primary: boolean

Source

Whether the service is a primary service.

Service.uuid: string

Source

The UUID of the service.

Characteristic

new Characteristic(uuid: string, opts?: CharacteristicOptions)

Source

Create a new GATT characteristic definition.

Parameters

ParameterTypeDefaultDescription
uuidstringThe characteristic's UUID.
opts?CharacteristicOptionsOptions selecting the characteristic properties, permissions, and initial value.

Characteristic.PROPERTY_INDICATE: number

Source

Characteristic property constants.

Characteristic.PROPERTY_NOTIFY: number

Source

Characteristic.PROPERTY_READ: number

Source

Characteristic.PROPERTY_WRITE: number

Source

Characteristic.PROPERTY_WRITE_WITHOUT_RESPONSE: number

Source

permissions: number | null

Source

The permission flags of the characteristic, or null if inferred.

properties: number

Source

The property flags of the characteristic.

Characteristic.uuid: string

Source

The UUID of the characteristic.

value: Uint8Array | null

Source

The current value of the characteristic, or null.

Server

new Server()

Source

Create a new BLE peripheral server for advertising services and handling client requests.

addService(service: Service): void

Source

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

Parameters

ParameterTypeDefaultDescription
serviceServiceThe Service to register with the GATT server.

Server.destroy(): void

Source

Destroy the server, stopping advertising and unpublishing all L2CAP channels.

publishChannel(opts?: ChannelOptions): void

Source

Publish an L2CAP channel. The 'channelPublish' event is emitted with the assigned PSM.

Parameters

ParameterTypeDefaultDescription
opts?ChannelOptionsOptions for the L2CAP channel to publish.

respondToRequest(request: ReadRequest, result: number, data?: Uint8Array): void

Source

Respond to a read or write request with a result code and optional data. Use the Server.ATT_* constants for the result.

Parameters

ParameterTypeDefaultDescription
requestReadRequestThe read or write request to respond to.
resultnumberThe ATT result code; use the Server.ATT_* constants.
data?Uint8ArrayThe value to return for a read request; omit for write responses.

Server.ATT_INSUFFICIENT_RESOURCES: number

Source

Server.ATT_INVALID_HANDLE: number

Source

Server.ATT_READ_NOT_PERMITTED: number

Source

Server.ATT_SUCCESS: number

Source

Server.ATT_UNLIKELY_ERROR: number

Source

ATT result codes for use with server.respondToRequest().

Server.ATT_WRITE_NOT_PERMITTED: number

Source

Server.CONNECTION_STATE_CONNECTED: number

Source

Server.CONNECTION_STATE_CONNECTING: number

Source

Server.CONNECTION_STATE_DISCONNECTED: number

Source

Server.CONNECTION_STATE_DISCONNECTING: number

Source

Server.PERMISSION_READ_ENCRYPTED: number

Source

Server.PERMISSION_READABLE: number

Source

Server.PERMISSION_WRITE_ENCRYPTED: number

Source

Characteristic permission constants.

Server.PERMISSION_WRITEABLE: number

Source

Server.PROPERTY_INDICATE: number

Source

Characteristic property constants.

Server.PROPERTY_NOTIFY: number

Source

Server.PROPERTY_READ: number

Source

Server.PROPERTY_WRITE: number

Source

Server.PROPERTY_WRITE_WITHOUT_RESPONSE: number

Source

Server.STATE_OFF: number

Source

Server.STATE_ON: number

Source

Server.STATE_TURNING_OFF: number

Source

Server.STATE_TURNING_ON: number

Source

startAdvertising(opts?: AdvertisingOptions): void

Source

Start advertising the server.

Parameters

ParameterTypeDefaultDescription
opts?AdvertisingOptionsAdvertising options such as the local name and the serviceUUIDs to advertise.

Server.state: BluetoothState

Source

The current Bluetooth adapter state. One of 'off', 'turningOn', 'on', or 'turningOff'.

stopAdvertising(): void

Source

Stop advertising.

unpublishChannel(psm: number): void

Source

Unpublish an L2CAP channel with the given psm.

Parameters

ParameterTypeDefaultDescription
psmnumberThe PSM of the channel to unpublish, as assigned when it was published.

updateValue(characteristic: Characteristic, data: Uint8Array): boolean

Source

Update the value of characteristic with data and notify subscribed clients.

Parameters

ParameterTypeDefaultDescription
characteristicCharacteristicThe characteristic whose value changed.
dataUint8ArrayThe new value to send to subscribed clients.

Returns boolean — Whether the notification was sent to subscribed clients successfully.

Central

new Central()

Source

Create a new BLE central manager for scanning and connecting to peripherals.

Central.SCAN_MODE_BALANCED: number

Source

Central.SCAN_MODE_LOW_LATENCY: number

Source

Scan mode constants for use with central.startScan().

Central.SCAN_MODE_LOW_POWER: number

Source

Central.SCAN_MODE_OPPORTUNISTIC: number

Source

Central.STATE_OFF: number

Source

Central.STATE_ON: number

Source

Central.STATE_TURNING_OFF: number

Source

Central.STATE_TURNING_ON: number

Source

connect(peripheral: Peripheral): void

Source

Connect to a discovered peripheral.

Parameters

ParameterTypeDefaultDescription
peripheralPeripheralA discovered peripheral to connect to.

Central.destroy(): void

Source

Destroy the central manager, stopping any active scan and disconnecting all connected peripherals.

disconnect(peripheral: Peripheral): void

Source

Disconnect from a connected peripheral.

Parameters

ParameterTypeDefaultDescription
peripheralPeripheralThe connected peripheral to disconnect from.

startScan(serviceUUIDs?: string[], opts?: { scanMode?: number }): void

Source

Start scanning for peripherals advertising the given serviceUUIDs. Pass null to scan for all peripherals.

Parameters

ParameterTypeDefaultDescription
serviceUUIDs?string[]The service UUIDs to filter advertisements by; pass null to scan for all peripherals.
opts?{ scanMode?: number }Options; scanMode selects the Android scan mode (one of the Central.SCAN_MODE_* constants).

Central.state: BluetoothState

Source

The current Bluetooth adapter state. One of 'off', 'turningOn', 'on', or 'turningOff'.

stopScan(): void

Source

Stop scanning for peripherals.

Peripheral

new Peripheral(opts: PeripheralOptions)

Source

Create a new peripheral instance from a ScanResult. Typically obtained via the 'discover' event on Central rather than constructed directly. Its identity and advertised metadata are derived from the scan result.

Parameters

ParameterTypeDefaultDescription
optsPeripheralOptionsOptions carrying the ScanResult this peripheral is derived from.

Peripheral.destroy(): void

Source

Destroy the peripheral, releasing its underlying resources.

discoverCharacteristics(service: Service): void

Source

Discover characteristics for the given service. Results are emitted via the 'characteristicsDiscover' event.

Parameters

ParameterTypeDefaultDescription
serviceServiceThe service to discover characteristics on.

discoverServices(): void

Source

Discover services offered by the peripheral. Results are emitted via the 'servicesDiscover' event.

Peripheral.id: string

Source

The unique identifier of the peripheral, equal to scanResult.device.address.

name: string | null

Source

The advertised name of the peripheral, or null if unavailable. Equal to scanResult.device.name.

openL2CAPChannel(psm: number): void

Source

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

Parameters

ParameterTypeDefaultDescription
psmnumberThe PSM (Protocol/Service Multiplexer) of the channel to open.

Peripheral.PROPERTY_INDICATE: number

Source

Characteristic property constants.

Peripheral.PROPERTY_NOTIFY: number

Source

Peripheral.PROPERTY_READ: number

Source

Peripheral.PROPERTY_WRITE: number

Source

Peripheral.PROPERTY_WRITE_WITHOUT_RESPONSE: number

Source

read(characteristic: Characteristic): void

Source

Read the value of characteristic. The result is emitted via the 'read' event.

Parameters

ParameterTypeDefaultDescription
characteristicCharacteristicThe characteristic to read.

requestMtu(mtu: number): void

Source

Request a new MTU size. The result is emitted via the 'mtuChanged' event.

Parameters

ParameterTypeDefaultDescription
mtunumberThe desired ATT MTU size, in bytes.

rssi: number

Source

The signal strength of the most recent advertisement, equal to scanResult.rssi.

scanResult: ScanResult

Source

The ScanResult from the most recent advertisement for this peripheral.

serviceData: ServiceData | null

Source

The advertised service data, or null when the advertisement carried no scan record or no service data.

subscribe(characteristic: Characteristic): void

Source

Subscribe to notifications for characteristic.

Parameters

ParameterTypeDefaultDescription
characteristicCharacteristicThe characteristic to start receiving notifications for.

unsubscribe(characteristic: Characteristic): void

Source

Unsubscribe from notifications for characteristic.

Parameters

ParameterTypeDefaultDescription
characteristicCharacteristicThe characteristic to stop receiving notifications for.

write(characteristic: Characteristic, data: Uint8Array, withResponse?: boolean): void

Source

Write data to characteristic. If withResponse is true (the default), a write confirmation is requested.

Parameters

ParameterTypeDefaultDescription
characteristicCharacteristicThe characteristic to write to.
dataUint8ArrayThe bytes to write.
withResponse?booleanWhether a write confirmation is requested (default true).

Types

ServiceOptions

interface ServiceOptions {
  primary?: boolean
}
Source

CharacteristicOptions

interface CharacteristicOptions {
  read?: boolean
  write?: boolean
  writeWithoutResponse?: boolean
  notify?: boolean
  indicate?: boolean
  permissions?: number
  value?: Uint8Array | null
}
Source

BluetoothState

type BluetoothState = 'off' | 'turningOn' | 'on' | 'turningOff'
Source

AdvertisingOptions

interface AdvertisingOptions {
  name?: string
  serviceUUIDs?: string[]
}
Source

ChannelOptions

interface ChannelOptions {
  encrypted?: boolean
}
Source

ReadRequest

interface ReadRequest {
  handle: unknown
  requestId: number
  characteristicUuid: string
  offset: number
}
Source

WriteRequest

interface WriteRequest {
  handle: unknown
  requestId: number
  characteristicUuid: string
  data: Uint8Array
  offset: number
  responseNeeded: boolean
}
Source

PeripheralOptions

interface PeripheralOptions {
  scanResult: ScanResult
}
Source

ServiceData

type ServiceData = {
  [uuid: string]: Uint8Array
}
Source

Classes

ScanResult

Source
class ScanResult {
  device: Device
  rssi: number
  scanRecord: ScanRecord | null
}

ScanRecord

Source
class ScanRecord {
  serviceData: ServiceData | null
}

Device

Source
class Device {
  address: string
  name: string | null
}

See also

On this page

Usage
API
BluetoothError
new BluetoothError(msg: string, fn?: Function, code?: string)
BluetoothError.ADVERTISE_FAILED(msg: string): BluetoothError
BluetoothError.CHANNEL_FAILED(msg: string): BluetoothError
BluetoothError.CHANNEL_PUBLISH_FAILED(msg: string): BluetoothError
BluetoothError.CONNECTION_FAILED(msg: string, id: string): BluetoothError
BluetoothError.DISCONNECT(msg: string, id: string): BluetoothError
BluetoothError.DISCOVER_FAILED(msg: string): BluetoothError
BluetoothError.MTU_CHANGE_FAILED(msg: string): BluetoothError
BluetoothError.NOTIFY_FAILED(msg: string): BluetoothError
BluetoothError.NOTIFY_STATE_FAILED(msg: string): BluetoothError
BluetoothError.READ_FAILED(msg: string): BluetoothError
BluetoothError.SCAN_FAILED(msg: string): BluetoothError
BluetoothError.SERVICE_ADD_FAILED(msg: string): BluetoothError
BluetoothError.WRITE_FAILED(msg: string): BluetoothError
code: string
BluetoothError.id: string
name: 'BluetoothError'
L2CAPChannel
new L2CAPChannel(channelHandle: ArrayBuffer)
peer: string | null
psm: number
Service
new Service(uuid: string, characteristics?: Characteristic[], opts?: ServiceOptions)
characteristics: Characteristic[]
primary: boolean
Service.uuid: string
Characteristic
new Characteristic(uuid: string, opts?: CharacteristicOptions)
Characteristic.PROPERTY_INDICATE: number
Characteristic.PROPERTY_NOTIFY: number
Characteristic.PROPERTY_READ: number
Characteristic.PROPERTY_WRITE: number
Characteristic.PROPERTY_WRITE_WITHOUT_RESPONSE: number
permissions: number | null
properties: number
Characteristic.uuid: string
value: Uint8Array | null
Server
new Server()
addService(service: Service): void
Server.destroy(): void
publishChannel(opts?: ChannelOptions): void
respondToRequest(request: ReadRequest, result: number, data?: Uint8Array): void
Server.ATT_INSUFFICIENT_RESOURCES: number
Server.ATT_INVALID_HANDLE: number
Server.ATT_READ_NOT_PERMITTED: number
Server.ATT_SUCCESS: number
Server.ATT_UNLIKELY_ERROR: number
Server.ATT_WRITE_NOT_PERMITTED: number
Server.CONNECTION_STATE_CONNECTED: number
Server.CONNECTION_STATE_CONNECTING: number
Server.CONNECTION_STATE_DISCONNECTED: number
Server.CONNECTION_STATE_DISCONNECTING: number
Server.PERMISSION_READ_ENCRYPTED: number
Server.PERMISSION_READABLE: number
Server.PERMISSION_WRITE_ENCRYPTED: number
Server.PERMISSION_WRITEABLE: number
Server.PROPERTY_INDICATE: number
Server.PROPERTY_NOTIFY: number
Server.PROPERTY_READ: number
Server.PROPERTY_WRITE: number
Server.PROPERTY_WRITE_WITHOUT_RESPONSE: number
Server.STATE_OFF: number
Server.STATE_ON: number
Server.STATE_TURNING_OFF: number
Server.STATE_TURNING_ON: number
startAdvertising(opts?: AdvertisingOptions): void
Server.state: BluetoothState
stopAdvertising(): void
unpublishChannel(psm: number): void
updateValue(characteristic: Characteristic, data: Uint8Array): boolean
Central
new Central()
Central.SCAN_MODE_BALANCED: number
Central.SCAN_MODE_LOW_LATENCY: number
Central.SCAN_MODE_LOW_POWER: number
Central.SCAN_MODE_OPPORTUNISTIC: number
Central.STATE_OFF: number
Central.STATE_ON: number
Central.STATE_TURNING_OFF: number
Central.STATE_TURNING_ON: number
connect(peripheral: Peripheral): void
Central.destroy(): void
disconnect(peripheral: Peripheral): void
startScan(serviceUUIDs?: string[], opts?: { scanMode?: number }): void
Central.state: BluetoothState
stopScan(): void
Peripheral
new Peripheral(opts: PeripheralOptions)
Peripheral.destroy(): void
discoverCharacteristics(service: Service): void
discoverServices(): void
Peripheral.id: string
name: string | null
openL2CAPChannel(psm: number): void
Peripheral.PROPERTY_INDICATE: number
Peripheral.PROPERTY_NOTIFY: number
Peripheral.PROPERTY_READ: number
Peripheral.PROPERTY_WRITE: number
Peripheral.PROPERTY_WRITE_WITHOUT_RESPONSE: number
read(characteristic: Characteristic): void
requestMtu(mtu: number): void
rssi: number
scanResult: ScanResult
serviceData: ServiceData | null
subscribe(characteristic: Characteristic): void
unsubscribe(characteristic: Characteristic): void
write(characteristic: Characteristic, data: Uint8Array, withResponse?: boolean): void
Types
ServiceOptions
CharacteristicOptions
BluetoothState
AdvertisingOptions
ChannelOptions
ReadRequest
WriteRequest
PeripheralOptions
ServiceData
Classes
ScanResult
ScanRecord
Device
See also