Bluetooth Classic Docs
API Overview
API Overview
Not all functionality is available across both Android and IOS (for Apple MFi related annoying details); for that reason all calls will respond with a Promise
which will throw an implementation error if attempting to call on IOS (which is completely different from previous versions where IOS would just reject the Promise). You'll probably want to either:
- Wrap your functionality in
Platform.IOS
- Duplicate your code with
Component.[android|ios].js
- or handle the the
Error
appropriately
RN Bluetooth Classic
RNBluetoothClassic
is the primary point of access for all Adapter/External Accessory related calls.
import RNBluetoothClassic from 'react-native-bluetooth-classic';
Provides access to the following:
API | Description | Platforms |
---|---|---|
isBluetoothAvailable | Determine whether device has Bluetooth as a feature | |
isBluetoothEnabled | Determine whether device has enabled Bluetooth | |
getBondedDevices | List currently paired/bonded devices | |
getConnectedDevices | List currently connected devices | |
getConnectedDevice | Retrieves a connected BluetoothDevice | |
onBluetoothEnabled | Provide a listener for Bluetooth enabled events | |
onBluetoothDisabled | Provide a listener for Bluetooth disabled events | |
onStateChanged | Provides a listener for Bluetooth state change events | |
onDeviceConnected | Provide a listener for when a device connection is initiated | |
onDeviceDisconnected | Provide a listener for when a device connection has dropped | |
onError | Provide a listener for any generic errors from Adapter or Device | |
startDiscovery | Initiate the discovery process | |
cancelDiscovery | Cancel the discovery process | |
pairDevice | Request to pair with a device | |
unpairDevice | Request to unapir a device | |
accept | Attempt to accept a device connection | |
cancelAccept | Cancels accepting a device connection | |
requestBluetoothEnabled | Request that the system enable Bluetooth | |
setBluetoothAdapterName | Attempt to rename the BluetoothAdapter | |
openBluetoothSettings | Opens Android's Bluetooth Settings programmatically |
IMPORTANT: The term connection refers to an active
socket
! Most of the questions received are related to the fact that the terms connected and connection are being used when bonded is meant.
Bluetooth Device
BluetoothDevice
(s) are available from a number of RNBluetoothClassic
functions. These are the functions available while working with BluetoothDevice
(s):
API | Description | Platforms |
---|---|---|
isConnected | Determine whether the device has an active connection | |
connect | Attempt connection to the device | |
disconnect | Attempt to disconnect from the device | |
available | Whether and how much data is available for read | |
clear | Clear the current read buffer | |
read | Read data from the device (if available) | |
write | Write to the device | |
onDataReceived | Add a listener to handle read events |
onDataReceived and read shouldn't really be used at the same time as you could end up with half filled data or random errors. I suggest sticking to one method
setInterval
oronRead
in order to continuously get data
Edit this page on GitHubA number of the previous functions are also available directly from
RNBluetoothClassic
. But in most cases it's wise to use theBluetoothDevice
returned from one of the prior functions in order to manage directly