Installation
Install Agnostic Web BLE and create your first Bluetooth client.
Install the package:
pnpm add @0xsarwagya/agnostic-web-ble# or: npm install @0xsarwagya/agnostic-web-bleThen create a Bluetooth client with the adapters your application supports, in preference order:
import { createBluetooth } from "@0xsarwagya/agnostic-web-ble";
import { nativeWebBluetoothAdapter } from "@0xsarwagya/agnostic-web-ble/adapters/native";
const bluetooth = createBluetooth({
adapters: [nativeWebBluetoothAdapter()],
});Selection is deterministic: the first adapter whose isAvailable() resolves
true wins. Nothing is selected until the first operation runs, and you can
always inspect the decision:
const adapter = await bluetooth.selectAdapter();
console.log(adapter.id); // "native-web-bluetooth"Checking what this runtime can do
Never branch on browser names. Ask for capabilities instead:
const capabilities = await bluetooth.capabilities();
if (capabilities.requiresUserGesture) {
// requestDevice must be called from a click handler
}Expected result
createBluetooth never throws for an unsupported runtime — the failure
surfaces on first use as a BluetoothError with code UNAVAILABLE, listing
every adapter that was tried.