Android Bluetooth Low Energy (BLE) bindings for Bare, providing both central and peripheral roles. Built on the Android Bluetooth API.
npm i bare-bluetooth-android
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
})
})Create a new BLE central manager for scanning and connecting to peripherals.
The current Bluetooth adapter state. One of 'off', 'turningOn', 'on', or 'turningOff'.
Start scanning for peripherals advertising the given serviceUUIDs. Pass null to scan for all peripherals.
Options include:
opts = {
scanMode: null
}Set scanMode to one of Central.SCAN_MODE_OPPORTUNISTIC, Central.SCAN_MODE_LOW_POWER, Central.SCAN_MODE_BALANCED, or Central.SCAN_MODE_LOW_LATENCY.
Stop scanning for peripherals.
Connect to a discovered peripheral.
Disconnect from a connected peripheral.
Destroy the central manager, disconnecting all connected peripherals.
Emitted when the Bluetooth adapter state changes. The listener receives the new state string.
Emits when a peripheral is discovered during scanning. The listener receives a Peripheral instance. Its scanResult is a ScanResult exposing scanResult.device (a Device with address and name), scanResult.rssi, and scanResult.scanRecord (a ScanRecord with serviceData, or null when the advertisement has no scan record). The peripheral also exposes id, name, rssi, and serviceData derived from the scan result.
Emitted when a connection to a peripheral is established. The listener receives a Peripheral instance.
Emitted when a peripheral disconnects. The listener receives the peripheral and an optional error.
Emitted when a connection attempt fails. The listener receives the peripheral id and an error.
Emitted when scanning fails. The listener receives the errorCode.
Scan mode constants for use with central.startScan().
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.
The ScanResult from the most recent advertisement for this peripheral.
The unique identifier of the peripheral, equal to scanResult.device.address.
The advertised name of the peripheral, or null if unavailable. Equal to scanResult.device.name.
The signal strength of the most recent advertisement, equal to scanResult.rssi.
The advertised service data, or null when the advertisement carried no scan record or no service data.
Discover services offered by the peripheral. Results are emitted via the 'servicesDiscover' event.
Discover characteristics for the given service. Results are emitted via the 'characteristicsDiscover' event.
Read the value of characteristic. The result is emitted via the 'read' event.
Write data to characteristic. If withResponse is true (the default), a write confirmation is requested.
Subscribe to notifications for characteristic.
Unsubscribe from notifications for characteristic.
Open an L2CAP channel to the peripheral using the given psm. The result is emitted via the 'channelOpen' event.
Request a new MTU size. The result is emitted via the 'mtuChanged' event.
Destroy the peripheral instance.
Emitted when services are discovered. The listener receives an array of Service instances and an optional error.
Emitted when characteristics are discovered. The listener receives the service, an array of Characteristic instances, and an optional error.
Emitted when a characteristic read completes. The listener receives the characteristic, data, and an optional error.
Emitted when a characteristic write completes. The listener receives the characteristic and an optional error.
Emitted when a characteristic notification is received. The listener receives the characteristic, data, and an optional error.
Emitted when the notification state changes. The listener receives the characteristic, isNotifying, and an optional error.
Emitted when an L2CAP channel is opened. The listener receives an L2CAPChannel instance or null, and an optional error.
Emitted when the MTU is changed. The listener receives the new mtu and an optional error.
Characteristic property constants.
The Device the advertisement came from, mirroring Android's ScanResult.getDevice().
The received signal strength in dBm, mirroring ScanResult.getRssi().
The ScanRecord parsed from the advertisement, or null when the advertisement carried no scan record. Mirrors ScanResult.getScanRecord().
An object with UUID string keys and Uint8Array values, or null when the advertisement carried no service data. Mirrors ScanRecord.getServiceData().
The hardware address of the device, mirroring BluetoothDevice.getAddress().
The advertised device name, or null if unavailable. Mirrors BluetoothDevice.getName().
Create a new BLE peripheral server for advertising services and handling client requests.
The current Bluetooth adapter state. One of 'off', 'turningOn', 'on', or 'turningOff'.
Add a service to the GATT server. The 'serviceAdd' event is emitted when the service has been registered.
Start advertising the server.
Options include:
opts = {
name: null,
serviceUUIDs: null
}Stop advertising.
Respond to a read or write request with a result code and optional data. Use the Server.ATT_* constants for the result.
Update the value of characteristic with data and notify subscribed clients. Returns true if the notification was sent successfully.
Publish an L2CAP channel. The 'channelPublish' event is emitted with the assigned PSM.
Options include:
opts = {
encrypted: false
}Unpublish an L2CAP channel with the given psm.
Destroy the server.
Emitted when the Bluetooth adapter state changes. The listener receives the new state string.
Emitted when a service is added. The listener receives the uuid and an optional error.
Emitted when a client reads a characteristic. The listener receives a request object with handle, requestId, characteristicUuid, and offset properties.
Emitted when a client writes to a characteristic. The listener receives an array of request objects, each with handle, requestId, characteristicUuid, data, offset, and responseNeeded properties.
Emitted when a client subscribes to notifications. The listener receives deviceAddress and characteristicUuid.
Emitted when a client unsubscribes from notifications. The listener receives deviceAddress and characteristicUuid.
Emitted when advertising fails. The listener receives errorCode and error.
Emitted when an L2CAP channel is published. The listener receives the psm and an optional error.
Emitted when an L2CAP channel is opened by a client. The listener receives an L2CAPChannel instance or null, and an optional error.
Emitted when a notification is delivered. The listener receives deviceAddress and status.
Characteristic property constants.
Characteristic permission constants.
ATT result codes for use with server.respondToRequest().
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.
The Protocol/Service Multiplexer number of the channel.
The address of the remote peer, or null.
Create a new GATT service definition.
Options include:
opts = {
primary: true
}The UUID of the service.
The array of characteristics belonging to the service.
Whether the service is a primary service.
Create a new GATT characteristic definition.
Options include:
opts = {
read: false,
write: false,
writeWithoutResponse: false,
notify: false,
indicate: false,
permissions: null,
value: null
}Set read, write, writeWithoutResponse, notify, and indicate to configure the characteristic properties. If permissions is null, permissions are inferred from the properties.
The UUID of the characteristic.
The property flags of the characteristic.
The permission flags of the characteristic, or null if inferred.
The current value of the characteristic, or null.
Characteristic property constants.
Apache-2.0