Socket
Book a DemoInstallSign in
Socket

react-web-bluetooth

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-web-bluetooth

React hooks wrapper for the Web Bluetooth API

latest
Source
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

React Hooks for Web Bluetooth

This is a package that provides a hooks wrapper around the new Web Bluetooth API

It is an experimental feature implemented under a flag in Chrome. In order to use this feature in Chrome, enable:

chrome://flags/#enable-experimental-web-platform-features

The getDevices method requires also to enable the new permissions backend experimental feature.

chrome://flags/#enable-web-bluetooth-new-permissions-backend

getDevices is used to retrieve already paired Bluetooth devices from the browser, for example after a refresh instead of pairing a device again getDevices can be used to connect to it without pairing.

Example

const { onClick, device } = useRequestDevice({
  acceptAllDevices: true,
});

// ...

return <>
  {!device && <button onClick={onClick}>Connect</button>}
  {device && <span>{device.name}</span>}
</>

API

useGetServer

Params:

device: BluetoothDevice

Returns:

server: BluetoothRemoteGATTServer


useGetCharacteristic

Params:

service: BluetoothRemoteGATTService

bluetoothCharacteristicUUID: string

Returns:

characteristic: BluetoothRemoteGATTCharacteristic


useGetDevices

Returns: devices[]: BluetoothDevice[]


useRequestDevice

Params:

options: https://developer.mozilla.org/en-US/docs/Web/API/Bluetooth/requestDevice#parameters

Returns:

onClick: (event) => void // returns a onClick function that can be used to trigget the browser's pairing window and list the scanned Bluetooth devices.

device: BluetoothDevice // returns a device chosen from the Bluetooth pairing dialogue.


useGetPrimaryService

Params:

device: BluetoothDevice

Returns:

service: BluetoothRemoteGATTService


useReadValue

Params:

characteristic: BluetoothRemoteGATTCharacteristic

Returns::

value: DataView


writeValue

Params:

characteristic: BluetoothRemoteGATTCharacteristic

value: ArrayBuffer

FAQs

Package last updated on 16 Feb 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.