Skip to content

useBluetooth

Hook for getting information about bluetooth

browser
low
test coverage
Last changed: 8 months ago

TIP

This hook uses navigator.bluetooth browser api to provide enhanced functionality. Make sure to check for compatibility with different browsers when using this api

Installation

Library
CLI
Manual
typescript
import { useBluetooth } from '@siberiacancode/reactuse';

Usage

typescript
const { supported, connected, device, requestDevice, server } = useBluetooth(options);

Demo

Api

Parameters

NameTypeDefaultNote
options.acceptAllDevices?booleanfalseThe options to request all Bluetooth devices
options.filters?Array<BluetoothLEScanFilter>-Array of filters to apply when scanning Bluetooth devices
options.optionalServices?Array<BluetoothServiceUUID>-Array of optional services that the application can use

Returns

UseBluetoothReturn

Type declaration

typescript
export interface UseBluetoothReturn {
  /** Indicates if bluetooth device is currently connected */
  connected: boolean;
  /** Describe connected bluetooth device */
  device?: BluetoothDevice;
  /** The GATT server for connected bluetooth device */
  server?: BluetoothRemoteGATTServer;
  /** Whether the bluetooth is supported*/
  supported: boolean;
  /** Function to request bluetooth device from the user */
  requestDevice: () => Promise<void>;
}

export interface UseBluetoothOptions {
  /** The options to request all bluetooth devices */
  acceptAllDevices?: boolean;
  /** Array of filters to apply when scanning bluetooth devices */
  filters?: BluetoothLEScanFilter[];
  /** Array of optional services that the application can use */
  optionalServices?: BluetoothServiceUUID[];
}

Source

SourceDemo

Contributors

D
debabin
debabin
B
babin
babin
G
Gorilla Dev
Gorilla Dev

Released under the MIT License.