Bluetooth Classic Docs
Getting Started with Android
Getting Started with Android
Installation
As previously described the react-native-bluetooth-classic
is installed through NPM:
$ npm install react-native-bluetooth-classic --save
Once installed auto linking will take over and all that is required is starting the application as normal. Be sure to check out the previous link if you need to customize or disable the autolinking of this (or any) module.
Auto linking performs the task of dynamically adding the default package:
new RNBluetoothClassicPackage()
to your application packages.
API Overview
The primary classes that you may need to work with are:
RNBluetoothClassicPackage
RNBluetoothClassicPackage is responsible for providing the DeviceConnection
configuration to the RNBluetoothClassicModule
. You're able to provide your own implementation to React Native MainApplication
or react-native.config.js
file, more information can be found in the guides.
RNBluetoothClassicPackage.Builder
RNBluetoothClassicPackage.Builder provides a fluent method for creating the RNBluetoothClassicPackage
. The builder is used to provide customized ConnectionAcceptor
(s), ConnectionConnector
(s) and DeviceConnection
(s) to the RNBluetoothClassicModule
RNBluetoothClassicModule
RNBluetoothClassicModule manages all communication between Android and React Native.
ConnectionAcceptor
ConnectionAcceptor provides a standard interface for accepting connections with the BluetoothAdapter
. A single implementation RfcommAcceptorThreadImpl is provided by the default package.
ConnectionConnector
ConnectionConnector provides a standard interface for performing connection to individual devices. A single implementation RfcommConnectorThreadImpl is provided by the default package.
DeviceConnection
DeviceConnection is responsible for all the communication between Android and the device. This is where most of the customization will be made:
- Providing different event handling, middleware
- Handling different message types and formats
- Etc.
There are two current implementations available:
Customization
Customization of the native module is done through implementation of your own RNBluetoothClassicPackage
. For more information take a look at the package documentation or some of the guides available.
Android Permissions
Android permissions need to be requested within your application (much like Camera). To do this check out the example application code, and look for the PermissionsAndroid within react-native
:
import { PermissionsAndroid } from 'react-native';
/** * See https://reactnative.dev/docs/permissionsandroid for more information * on why this is required (dangerous permissions). */const requestAccessFineLocationPermission = async () => { const granted = await PermissionsAndroid.request( PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION, { title: 'Access fine location required for discovery', message: 'In order to perform discovery, you must enable/allow ' + 'fine location access.', buttonNeutral: 'Ask Me Later', buttonNegative: 'Cancel', buttonPositive: 'OK', } ); return granted === PermissionsAndroid.RESULTS.GRANTED;};