0.0.2 • Published 4 years ago

react-native-bluetooth-headset-detect-ex v0.0.2

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

react-native-bluetooth-headset-detect

Bluetooth Headset Detection for React Native

Getting started

$ npm install react-native-bluetooth-headset-detect --save

If you use react-native < 0.60

$ react-native link react-native-bluetooth-headset-detect

Usage

With React hooks:

import { useBluetoothHeadsetDetection } from 'react-native-bluetooth-headset-detect';

const MyComponent = () => {
  const device = useBluetoothHeadsetDetection();
  return (
    <Text>Connected headset: {device}</Text>
  );
};

Without React hooks:

import {
  getHeadset,
  addListener,
  removeListener,
} from 'react-native-bluetooth-headset-detect';

console.log('Connected device:', getHeadset());
addListener((device) => {
  console.log('Connected device:', device);
});

Legacy (not recommended):

import { NativeEventEmitter, NativeModules } from "react-native";

const BluetoothHeadsetDetectModule = NativeModules.RNBluetoothHeadsetDetect;
const bluetoothHeadsetDetectEmitter = new NativeEventEmitter(
  BluetoothHeadsetDetectModule
);
bluetoothHeadsetDetectEmitter.addListener("onChange", ({ devices }) => {
  if (devices.length) {
    console.log("Connected device:", devices[0]);
  } else {
    console.log("No devices connected");
  }
});

Demo project

https://github.com/alexkorep/react-native-bluetooth-headset-detect-demo