react-native-advance-bluetooth-audio-manager
v0.1.5
Published
advance bluetooth and audio
Downloads
10
Readme
react-native-advance-bluetooth-audio-manager
A React Native library for advanced Bluetooth audio management, allowing you to scan for Bluetooth devices, connect to them, and route audio to connected devices.
Features
- Scan for nearby Bluetooth devices.
- Connect to Bluetooth devices.
- Route audio output to connected Bluetooth devices.
- Full control of Bluetooth audio playback.
Installation
To install the library, use npm or yarn:
npm install react-native-advance-bluetooth-audio-manager
or
yarn add react-native-advance-bluetooth-audio-manager
Linking
If you are not using auto-linking (React Native 0.60 and above), link the package manually:
react-native link react-native-advance-bluetooth-audio-manager
Note: For Expo users, this package requires a custom development build because Expo Go does not support native code. You can create a custom build using EAS
:
eas build --profile development --platform android
Permissions
Ensure that your AndroidManifest.xml
file contains the necessary permissions for Bluetooth usage:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Usage
Import the package and utilize its Bluetooth audio management features:
import BluetoothAudioManager from 'react-native-advance-bluetooth-audio-manager';
// Initialize Bluetooth Manager
await BluetoothAudioManager.initialize();
// Scan for Bluetooth devices
const devices = await BluetoothAudioManager.startScanning();
// Connect to a specific device
await BluetoothAudioManager.connectToDevice(devices[0]);
// Route audio to the connected device
await BluetoothAudioManager.routeAudioToDevice(devices[0].id);
// Destroy the Bluetooth Manager when done
await BluetoothAudioManager.destroy();
Example
import React, { useState, useEffect } from 'react';
import { View, Button, Text, Alert } from 'react-native';
import BluetoothAudioManager from 'react-native-advance-bluetooth-audio-manager';
const BluetoothAudioExample = () => {
const [devices, setDevices] = useState([]);
useEffect(() => {
// Initialize Bluetooth on mount
const initializeBluetooth = async () => {
try {
await BluetoothAudioManager.initialize();
} catch (error) {
Alert.alert('Error', 'Failed to initialize Bluetooth manager.');
}
};
initializeBluetooth();
return () => {
BluetoothAudioManager.destroy(); // Clean up on unmount
};
}, []);
const scanDevices = async () => {
try {
const foundDevices = await BluetoothAudioManager.startScanning();
setDevices(foundDevices);
} catch (error) {
Alert.alert('Error', 'Failed to scan for devices.');
}
};
const connectToDevice = async device => {
try {
await BluetoothAudioManager.connectToDevice(device);
Alert.alert('Connected', `Connected to ${device.name}`);
} catch (error) {
Alert.alert('Error', `Failed to connect to ${device.name}`);
}
};
return (
<View>
<Button title="Scan for Devices" onPress={scanDevices} />
{devices.map(device => (
<Text key={device.id} onPress={() => connectToDevice(device)}>
{device.name}
</Text>
))}
</View>
);
};
export default BluetoothAudioExample;
Troubleshooting
Common Issues
Cannot read property 'initialize' of undefined: Ensure the native module is properly linked. If using Expo, remember that Expo Go doesn't support custom native code. You will need to build a custom development build using EAS.
No devices found: Make sure your Bluetooth device is in pairing mode and that the necessary permissions (like
BLUETOOTH_CONNECT
andBLUETOOTH_SCAN
) are granted.
Contributing
We welcome contributions! Please read the contributing guide to learn how to contribute to this project.
License
MIT License
Created with create-react-native-library