0.6.3 • Published 2 months ago

rook-bluetooth v0.6.3

Weekly downloads
-
License
MIT
Repository
-
Last release
2 months ago

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 />;
};
0.6.3

2 months ago

0.6.2

2 months ago

0.6.1

2 months ago

0.6.0

2 months ago

0.5.5

9 months ago

0.5.4

1 year ago

0.5.3

2 years ago

0.5.2

2 years ago

0.5.0

2 years ago

0.3.1

2 years ago

0.2.2

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago