Skip to content

About

BlueBreeze React Native SDK

Resources

Stars

0 stars

Watchers

1 watching

Forks

Repository files navigation

BlueBreeze React Native

npm version License: MIT Platform

BlueBreeze is a modern Bluetooth LE library for React Native, wrapping the native BlueBreeze iOS and BlueBreeze Android SDKs behind a single JS/TypeScript API. All the platform-specific work -- connection state machines, GATT callback correlation, per-device request queuing -- happens natively; from JS you just observe state and await operations.

  • Reactive state -- adapter power, authorization, discovered devices, connection status, services, and characteristic data are all exposed as BBObservables: read the current .value synchronously, subscribe to .values (the current value, then new ones), or only to .updates. Streams are react-native-value-emitter emitters you subscribe to with .onValue(...).
  • Promise-based operations -- connect, discover services, read, write, and subscribe/unsubscribe are all async calls with a built-in timeout on both platforms; no native callbacks to correlate yourself.
  • Automatic per-device request queuing -- handled natively on both platforms, so you can fire off several operations on the same device without worrying about overlapping BLE requests.
  • Runtime permission handling built in -- authorizationStatus/authorizationRequest/ authorizationOpenSettings cover Android's runtime permissions and iOS's Bluetooth usage authorization behind one API.
  • Bluetooth SIG assigned numbers built in -- known company IDs, service UUIDs, and characteristic UUIDs are resolved automatically by the underlying native SDKs.
  • New Architecture -- implemented as a TurboModule.

Installation

npm install react-native-bluebreeze

iOS additionally requires a pod install:

cd ios && pod install

Requirements

  • React Native with the New Architecture enabled (this library is a TurboModule)
  • Android API 24+ (minSdk)
  • iOS 15.1+
  • Declare the Bluetooth permissions each platform needs:

Android (android/app/src/main/AndroidManifest.xml):

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" android:usesPermissionFlags="neverForLocation" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />

<uses-feature android:name="android.hardware.bluetooth_le" android:required="true" />

iOS (ios/Info.plist):

<key>NSBluetoothAlwaysUsageDescription</key>
<string>This app uses Bluetooth to connect to nearby devices.</string>

Quick start

import BlueBreeze from 'react-native-bluebreeze'

// Request authorization if needed, then start scanning once the adapter is on
if (BlueBreeze.authorizationStatus.value !== 'authorized') {
    BlueBreeze.authorizationRequest()
}

BlueBreeze.state.values.onValue((state) => {
    if (state === 'poweredOn') {
        BlueBreeze.scanStart()
    }
})

// Observe scan results
BlueBreeze.scanResults.onValue((result) => {
    console.log(result.name ?? 'Unknown device', result.rssi)
})

// Connect, discover, and talk to a device
async function connectToFirstDevice() {
    const device = BlueBreeze.devices.value.values().next().value
    if (device == undefined) return

    await device.connect()
    await device.discoverServices()

    for (const service of device.services.value) {
        for (const characteristic of service.characteristics) {
            if (characteristic.properties.includes('read')) {
                const data = await characteristic.read()
                console.log(characteristic.id, data)
            }
        }
    }

    await device.disconnect()
}

Observing state

Every piece of state is a BBObservable, which you can query in three ways:

characteristic.data.value                          // the current value
characteristic.data.values.onValue((data) => {})   // the current value, then new values
characteristic.data.updates.onValue((data) => {})  // only values that arrive from now on

For example, to receive notifications:

const subscription = characteristic.data.updates.onValue((data) => {
    console.log(characteristic.id, data)
})
await characteristic.subscribe()

// Later
subscription.remove()

In React components, useValueEmitter(handler, observable.values) from react-native-value-emitter removes the subscription automatically on unmount.

See example for a full app built on top of BlueBreeze.

Documentation

The public API is documented with TSDoc directly in the source under src -- hover any exported member in your editor to see it, or browse it there directly.

License

BlueBreeze is available under the MIT license. See LICENSE for details.

About

BlueBreeze React Native SDK

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages