New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

rook-bluetooth

Package Overview
Dependencies
Maintainers
2
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rook-bluetooth

RookMotion Bluetooth is a React Hook library that contains the Bluetooth management to get easy the integration with RookMotion services, in this package you could find two hooks in order to do connect with sensors.

  • 0.6.3
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
6
decreased by-71.43%
Maintainers
2
Weekly downloads
 
Created
Source

RookMotion Bluetooth

RookMotion Bluetooth is a React Hook library that contains the Bluetooth management to get easy the integration with RookMotion services, in this package you could find two hooks in order to do connect with sensors.

  • useSensorManager: this hook is in charge of read the battery, bpm and steps
  • useSensorScanner: this hooks is in charge to discover the sensors that are around.

Installation

npm i rook-bluetooth

or

yarn add rook-bluetooth

This package uses the react-native-ble-manager please follow the installation in order to continue.

Next is necessary to set a provider to start the package, set this provider at the highest level of your component tree

import {RookMotionProvider} from "rook-provider";

<RookMotionProvider token="YOUR-TOKEN">
  <Your-Components />
</RookMotionProvider>

NOTE: If you already setted the RookmotionWrapper skip this step

Usage

useSensorManager

useSensorManager: ({ emitter, showAlert, stepsEnable }: useSensorManagerProps) => SensorManager;

import { useSensorManager } from 'rook-bluetooth';

Return

batteryLevel: number;
bpm: number;
connectedPeripheral?: BLPeripheral;
discoveredSensors: Array<BLPeripheral>;
isReady: boolean;
isScanning: boolean;
stepCount: number;
connect: (peripheral: BLPeripheral) => Promise<boolean>;
disconnect: () => void;
startScan: (timeout: number) => void;

Example

import React, {FC, useEffect, useState} from 'react';

import {useNavigation} from '@react-navigation/native';

import {Box, useToast} from 'native-base';
import {NativeEventEmitter, NativeModules} from 'react-native';
import {BLPeripheral, useSensorManager} from 'rook-bluetooth';

type BLEConnectProps = {
  sensor: BLPeripheral | null;
  connected: (result: boolean) => void;
};

export const BLEConnect: FC<BLEConnectProps> = ({sensor, connected}) => {
  const navigation = useNavigation();

  const toast = useToast();

  const [connecting, setConnecting] = useState(false);

  const {isReady, batteryLevel, bpm, stepCount, connect, disconnect} =
    useSensorManager({
      emitter: new NativeEventEmitter(NativeModules.BleManager),
      showAlert: true,
      stepsEnable: true,
    });

  useEffect(() => {
    if (isReady) {
      setTimeout(() => {
        attemptToConnect();
      }, 200);
    }
  }, [isReady, sensor]);

  useEffect(() => {
    if (!navigation.isFocused()) {
      disconnect();
    }
  }, [navigation]);

  useEffect(() => {
    console.log('bpm', bpm);
    console.log('batteryLevel', batteryLevel);
    console.log('stepCount', stepCount);
  }, [bpm, batteryLevel, stepCount]);

  const attemptToConnect = async (): Promise<any> => {
    if (connecting || !sensor) {
      return;
    }

    setConnecting(true);

    try {
      const result = await connect(sensor);

      if (result) {
        toast.show({description: 'Connection successfully'});
        connected(true);
      } else {
        throw new Error();
      }
    } catch (error) {
      connected(false);
      toast.show({description: 'Unable to connect'});
    }
  };

  return <Box />;
};


useSensorScanner

 useSensorScanner: ({ emitter, showAlert, }: useSensorScannerProps) => SensorScanner;

 import { useSensorScanner } from 'rook-bluetooth';

Return

discoveredSensors: Array<BLPeripheral>;
isReady: boolean;
isScanning: boolean;
startScan: (timeout: number) => void;

Example

/* eslint-disable react-hooks/exhaustive-deps */
import React, {FC, useEffect, useState} from 'react';

import {useNavigation} from '@react-navigation/native';

import {Box, useToast} from 'native-base';
import {NativeEventEmitter, NativeModules} from 'react-native';
import {BLPeripheral, useSensorManager} from 'rook-bluetooth';

type BLEConnectProps = {
  sensor: BLPeripheral | null;
  connected: (result: boolean) => void;
};

export const BLEConnect: FC<BLEConnectProps> = ({sensor, connected}) => {
  const navigation = useNavigation();

  const toast = useToast();

  const [connecting, setConnecting] = useState(false);

  const {isReady, batteryLevel, bpm, stepCount, connect, disconnect} =
    useSensorManager({
      emitter: new NativeEventEmitter(NativeModules.BleManager),
      showAlert: true,
      stepsEnable: true,
    });

  useEffect(() => {
    if (isReady) {
      setTimeout(() => {
        attemptToConnect();
      }, 200);
    }
  }, [isReady, sensor]);

  useEffect(() => {
    if (!navigation.isFocused()) {
      disconnect();
    }
  }, [navigation]);

  useEffect(() => {
    console.log('bpm', bpm);
    console.log('batteryLevel', batteryLevel);
    console.log('stepCount', stepCount);
  }, [bpm, batteryLevel, stepCount]);

  const attemptToConnect = async (): Promise<any> => {
    if (connecting || !sensor) {
      return;
    }

    setConnecting(true);

    try {
      const result = await connect(sensor);

      if (result) {
        toast.show({description: 'Connection successfully'});
        connected(true);
      } else {
        throw new Error();
      }
    } catch (error) {
      connected(false);
      toast.show({description: 'Unable to connect'});
    }
  };

  return <Box />;
};

FAQs

Package last updated on 01 Mar 2024

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

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc