Bluetooth Headset Detection for React Native
$ npm install react-native-bluetooth-headset-detect --save
$ react-native link react-native-bluetooth-headset-detect
import { useBluetoothHeadsetDetection } from 'react-native-bluetooth-headset-detect';
const MyComponent = () => {
const device = useBluetoothHeadsetDetection();
return (
<Text>Connected headset: {device}</Text>
);
};
import {
getHeadset,
addListener,
removeListener,
} from 'react-native-bluetooth-headset-detect';
console.log('Connected device:', getHeadset());
addListener((device) => {
console.log('Connected device:', device);
});
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");
}
});
https://github.com/alexkorep/react-native-bluetooth-headset-detect-demo