fn

useBluetooth

v0.0.14testeddemo

Reactive 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

ParameterTypeDescription
options?UseBluetoothOptionsOptions

Returns

UseBluetoothReturnisSupported, isConnected, device, requestDevice, connect, disconnect, server, and error
PropertyTypeDescription
isSupportedReadonly<Ref<boolean>>Whether the Web Bluetooth API is available
isConnectedReadonly<ShallowRef<boolean>>Whether a GATT server connection is currently established
deviceShallowRef<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() => voidDisconnect from the current device's GATT server
serverShallowRef<BluetoothRemoteGATTServer | undefined>The connected GATT server, or undefined while disconnected
errorShallowRef<unknown | null>The last error thrown by requestDevice or a connection attempt