wzc5840 / react-native-bluetooth-headset-detect

Bluetooth headset detection for React Native

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

About

Bluetooth headset detection for React Native


Languages

Language:Java 58.3%Language:Objective-C 20.7%Language:JavaScript 12.4%Language:Ruby 8.6%