fn
useBluetooth
v0.0.14testeddemoReactive Web Bluetooth API. Prompts for a device and tracks its GATT server connection.
Examples
ts
const { isSupported, isConnected, device, requestDevice, server, error } = useBluetooth({
acceptAllDevices: true,
});
requestDevice();ts
// Filter by advertised service and request access to the battery service
const { device, requestDevice, server } = useBluetooth({
filters: [{ services: ['heart_rate'] }],
optionalServices: ['battery_service'],
});Demo
Loading demo…
Signature
ts
export function useBluetooth(options: UseBluetoothOptions ={ ... }Parameters
| Parameter | Type | Description |
|---|---|---|
options? | UseBluetoothOptions | Options |
Returns
UseBluetoothReturnisSupported, isConnected, device, requestDevice, connect, disconnect, server, and error| Property | Type | Description |
|---|---|---|
isSupported | Readonly<Ref<boolean>> | Whether the Web Bluetooth API is available |
isConnected | Readonly<ShallowRef<boolean>> | Whether a GATT server connection is currently established |
device | ShallowRef<BluetoothDevice | undefined> | The selected Bluetooth device, or undefined before one is chosen |
requestDevice | () => Promise<void> | Prompt the user to pick a device, then connect to its GATT server |
connect | () => Promise<void> | Re-establish the GATT connection to the current device |
disconnect | () => void | Disconnect from the current device's GATT server |
server | ShallowRef<BluetoothRemoteGATTServer | undefined> | The connected GATT server, or undefined while disconnected |
error | ShallowRef<unknown | null> | The last error thrown by requestDevice or a connection attempt |