566

useBluetooth

Hook for getting information about bluetooth

browserlowtest coverage

API not supported, make sure to check for compatibility with different browsers when using this API

import { useBluetooth } from '@siberiacancode/reactuse';
import { BluetoothConnectedIcon, BluetoothIcon } from 'lucide-react';
import { useState } from 'react';

import { cn } from '@/utils/lib';

const Demo = () => {
  const bluetooth = useBluetooth({ acceptAllDevices: true });
  const [connecting, setConnecting] = useState(false);

  const onConnect = async () => {
    setConnecting(true);

    try {
      await bluetooth.requestDevice();
    } catch {
      // The device picker can be closed without selecting a device.
    } finally {
      setConnecting(false);
    }
  };

  if (!bluetooth.supported)
    return (
      <p>
        API not supported, make sure to check for compatibility with different browsers when using
        this{' '}
        <a
          href='https://developer.mozilla.org/en-US/docs/Web/API/Navigator/bluetooth'
          rel='noreferrer'
          target='_blank'
        >
          API
        </a>
      </p>
    );

  const Icon = bluetooth.connected ? BluetoothConnectedIcon : BluetoothIcon;

  return (
    <section className='flex min-w-sm flex-col items-center gap-4 p-6 text-center'>
      <div
        className={cn(
          'bg-muted text-muted-foreground flex size-16 items-center justify-center rounded-full',
          bluetooth.connected && 'bg-blue-500/10 text-blue-500'
        )}
      >
        <Icon className='size-7' />
      </div>

      <div aria-live='polite' className='flex flex-col gap-1'>
        <p className='font-semibold'>
          {bluetooth.connected ? 'Bluetooth connected' : 'Connect Bluetooth'}
        </p>
        <p className='text-muted-foreground text-sm'>
          {bluetooth.connected
            ? (bluetooth.device?.name ?? 'Your device is ready to use')
            : 'Pair with a nearby device'}
        </p>
      </div>

      {!bluetooth.connected && (
        <button disabled={connecting} type='button' onClick={onConnect}>
          {connecting ? 'Connecting...' : 'Connect'}
        </button>
      )}
    </section>
  );
};

export default Demo;
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

pnpm add @siberiacancode/reactuse

Usage

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

Type Declarations

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[];
}

API

Parameters

NameTypeDefaultNote
options.acceptAllDevicesbooleanfalseThe options to request all Bluetooth devices
options.filtersArray<BluetoothLEScanFilter>-Array of filters to apply when scanning Bluetooth devices
options.optionalServicesArray<BluetoothServiceUUID>-Array of optional services that the application can use

Returns

UseBluetoothReturn

Contributors

ddebabinbbabinGGorilla Dev

Last updated on