@developertown/use-bluetooth-notifications
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

use-bluetooth-notifications

A react hook to easily use and listen to bluetooth notifications from a specified service and characteristic

Usage

There are two options for connecting. You can use the useBluetoothNotifications hook directly or use BluetoothContext and the useBluetooth hook:

useBluetoothNotifications

const [stream, setStream] = useState(); // Track stream state here in onNotification
const { device, status, startStream, stopStream } = useBluetoothNotifications({
  serviceUuid = "health_thermometer",
  characteristicUuid = "temperature_measurement",
  onNotification = (parsedValue: string | number, event: Event): void => {
    setStream(parsedValue);
  },
  onError = (error: Error): void => { ... },
  parser = (value: DataView, offset?: number): number | string => { ... }
});

return (
  <div>
    <button onClick={startStream}>Start</button>
    <button onClick={stopStream}>Stop</button>
  </div>
);

useBluetooth & useBluetoothProvider

const MyComponent: React.FC<{}> = () => {
  const { device, server, service, characteristic, status, startStream, stopStream } = useBluetooth();
  return (
    <div>
      <button onClick={startStream}>Start</button>
      <button onClick={stopStream}>Stop</button>
    </div>
  );
};

const App: React.FC<{}> = () => {
  const onNotification = useCallback((parsedValue: string | number, event: Event): void => { ... }, []);
  const onError = useCallback((error: Error): void => { ... }, []);
  const parser = useCallback((value: DataView, offset?: number): number | string => { ... }, []);

  return (
    <BluetoothProvider
      serviceUuid="health_thermometer"
      characteristicUuid="temperature_measurement"
      parser={myParser}
      onNotification={onNotification}
      onError={onError}
    >
      <MyComponent />
    </BluetoothProvider>
  );
};

Readme

Keywords

none

Package Sidebar

Install

npm i @developertown/use-bluetooth-notifications

Weekly Downloads

1

Version

0.1.0

License

MIT

Unpacked Size

181 kB

Total Files

38

Last publish

Collaborators

  • jasonvasquez
  • abillingsley
  • jaysqubexon