Giter Site home page Giter Site logo

shaunmulligan / react-native-cycling-sensors Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 1.0 891 KB

A React Native package for BLE cycling sensors.

License: MIT License

JavaScript 4.24% Starlark 2.98% Java 26.86% CMake 0.42% C++ 11.27% Ruby 3.62% Objective-C 0.60% Objective-C++ 10.13% Swift 0.17% TypeScript 39.69%
android ble bluetooth cycling heart-rate powermeter react-native typescript

react-native-cycling-sensors's Introduction

react-native-cycling-sensors

A RN package for BLE cycling sensors.

Currently only supports:

  • Heart Rate
  • Cycling Power Measurement
  • Cycling Power Vector
  • Cycling Speed and Cadence

Tested with Garmin Forerunner 945 (HR broadcast), Garmin HRM Pro for Heart Rate and Tacx Neo 2T and Favero Assioma duo for Power and Cadence measurements.

Installation

npm install react-native-cycling-sensors

Usage

import React, { useEffect } from 'react';
import { Button, SafeAreaView, StyleSheet, Text, View } from 'react-native';
import { BleSensors, PowerMeter } from 'react-native-cycling-sensors';

const App = () => {
  const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));

  const handleDiscoverPeripheral = (peripheral: any) => {
    console.log(peripheral);
  };

  const handlePowerData = (data: any) => {
    console.log('Power: ', data);
  };

  const handleScanStop = () => {
    console.log('Scanning Stopped');
  };

  const handleButton = () => {
    console.log('button pressed');
  };

  const handleError = (error: Error) => {
    console.log('Got error: ', error);
  };

  useEffect(() => {
    const startBleSensors = async () => {
      const bleSensor = new BleSensors();
      await bleSensor.requestPermissions();
      await bleSensor.start();
      await bleSensor.startSensorDiscovery();
      bleSensor.subscribeToDiscovery(handleDiscoverPeripheral);
      bleSensor.subscribeToDiscoveryStop(handleScanStop);
      await sleep(10000);
      const sensorList = await bleSensor.getDiscoveredSensors();
      console.log(sensorList);
      if (sensorList[0]?.sensorType?.includes('CyclingPower')) {
        console.log(sensorList[0]);
        const pm = new PowerMeter(sensorList[0].id);
        console.log(pm);
        await pm.connect().catch((err) => handleError(err));
        pm.subscribe(handlePowerData);
        await sleep(2000);
        const list = await bleSensor.getConnectedSensors();
        console.log('Connected list: ', list);
        await sleep(5000);
        let sensorLocation = await pm
          .getSensorLocation()
          .catch((err) => handleError(err));
        console.log('Sensor is on: ', sensorLocation);
        await sleep(5000);
        await pm.disconnect().catch((err) => handleError(err));
      }
    };

    startBleSensors(); // run it, run it

    return () => {
      // this now gets called when the component unmounts
    };
  }, []);

  return (
    <SafeAreaView style={styles.container}>
      <View>
        <Text>Testing...</Text>
        <Button title={'Scan Bluetooth'} onPress={handleButton} />
      </View>
    </SafeAreaView>
  );
};

export default App

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT


Made with create-react-native-library

react-native-cycling-sensors's People

Contributors

shaunmulligan avatar

Stargazers

 avatar  avatar Akira MATSUDA avatar hishizuka avatar

Watchers

 avatar

Forkers

jeanfurquim

react-native-cycling-sensors's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.