Integrations
elements
lifecycle
browser
- useAudio
- useBattery
- useBluetooth
- useBreakpoints
- useBroadcastChannel
- useBrowserLocation
- useClipboard
- useCopy
- useCssVar
- useDeviceList
- useDisplayMedia
- useDocumentEvent
- useDocumentTitle
- useDocumentVisibility
- useEventListener
- useEventSource
- useEyeDropper
- useFavicon
- useFileSystemAccess
- useFps
- useFullscreen
- useGamepad
- useGeolocation
- useMeasure
- useMediaControls
- useMediaQuery
- useMediaStream
- useMemory
- useNetwork
- useNotification
- useObjectUrl
- useOnline
- useOtpCredential
- usePermission
- usePictureInPicture
- usePointerLock
- usePostMessage
- useRaf
- useShare
- useSpeechRecognition
- useSpeechSynthesis
- useSticky
- useVibrate
- useVirtualKeyboard
- useWakeLock
- useWebSocket
- useWebWorker
- useWebWorkerCallback
utilities
state
- useBoolean
- useControllableState
- useCookie
- useCookies
- useCounter
- useCycleList
- useDefault
- useDisclosure
- useField
- useForm
- useHash
- useList
- useLocalStorage
- useMap
- useMask
- useMergedRef
- useObject
- useOffsetPagination
- useQueue
- useRafState
- useRefState
- useSessionStorage
- useSet
- useStateHistory
- useStep
- useStorage
- useToggle
- useUrlSearchParam
- useUrlSearchParams
- useValidatedState
- useWizard
user
sensors
- useDeviceMotion
- useDeviceOrientation
- useHotkeys
- useIdle
- useInfiniteScroll
- useIntersectionObserver
- useKeyboard
- useKeyPress
- useKeysPressed
- useMouse
- useMutationObserver
- useOrientation
- usePageLeave
- useParallax
- usePerformanceObserver
- useResizeObserver
- useScroll
- useScrollIntoView
- useScrollTo
- useSwipe
- useTextSelection
- useVisibility
- useWindowEvent
- useWindowFocus
- useWindowScroll
- useWindowSize
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/reactuseUsage
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
| Name | Type | Default | Note |
|---|---|---|---|
| options.acceptAllDevices | boolean | false | The 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
UseBluetoothReturnContributors
ddebabinbbabinGGorilla Dev
Last updated on