1.0.5 • Published 4 months ago

react-native-classicbluetooth-sdk v1.0.5

Weekly downloads
-
License
ISC
Repository
github
Last release
4 months ago

React Native Classic Bluetooth SDK

This SDK provides functionalities to connect with classic Bluetooth devices and handle permissions required for Bluetooth and location access.

Installation

To install the SDK, run:

npm install react-native-classicbluetooth-sdk

Permissions

Add the required permissions in AndroidManifest.xml:

<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
<uses-feature android:name="android.hardware.bluetooth" android:required="true"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

SDK Functions

Bluetooth Functions (bluetooth.ts)

MethodDescription
enableBluetoothRequests to enable Bluetooth.
isBluetoothEnabledChecks if Bluetooth is enabled.
startDiscoveryStarts discovering Bluetooth devices.
stopDiscoveryCancels the discovery process.
connectDeviceConnects to a specific device.
disconnectDeviceDisconnects from a device.
pairDevicePairs with a Bluetooth device.
unPairDeviceUnpairs a previously paired device.
getPairedDevicesRetrieves a list of paired devices.
getConnectedDeviceRetrieves a list of connected devices.
getBondedDevicesFetches bonded (trusted) devices.
onBluetoothEnabledListens for Bluetooth being enabled.
onBluetoothDisabledListens for Bluetooth being disabled.
onStateChangedListens for Bluetooth state changes.
onDeviceConnectedListens for device connections.
onDeviceDisconnectedListens for device disconnections.
openBluetoothSettingsOpens Bluetooth settings on the device.
cleanupSubscriptionsCleans up event subscriptions.
readReads data from a connected device.
writeWrites data to a connected device.
availableChecks available bytes for reading.
clearClears the Bluetooth buffer.

Permission Functions (permissions.ts)

MethodDescription
requestAllPermissionsRequests Bluetooth and location permissions.
checkAllPermissionsChecks the status of required permissions.
requestLocationServiceRequests location service to be enabled.

Usage in React Native

Initializing Bluetooth and Permissions

import { 
    enableBluetooth, 
    isBluetoothEnabled, 
    requestAllPermissions,
    checkAllPermissions, 
    startDiscovery, 
    requestLocationService
} from 'react-native-classicbluetooth-sdk';

const App = () =>{ 
    const [permissionStatus,setPermissionStatus] = useState()
    const [bluetoothEnabled,setBluetoothEnabled] = useState()
    
    useEffect(()=>{
        checkPermission()
    },[])

    const checkPermission = async () => {
        const permissionStatus = await checkAllPermissions()
        setPermissionStatus(permissionStatus)

        if(!permissionStatus){
           const isPermissionGranted = await requestAllPermissions()
           setPermissionStatus(isPermissionGranted)
        }
    }

    useEffect(() => {
        if (permissionStatus) {
            checkBluetoothStatus()      
        }
    }, [permissionStatus])

    useEffect(() => {
        if (bluetoothEnabled) {
            getBluetoothDevice()
        } else{
            enableBluetooth()
        }
    }, [bluetoothEnabled])

    const checkBluetoothStatus = async () => {
        const check = await isBluetoothEnabled()
        setBluetoothEnabled(check)
    }

    const getBluetoothDevice = async () => {
        const isLocationEnabled = await requestLocationService()
        if (!isLocationEnabled) {
            return
        }
        const device = await startDiscovery();
    }
}

Connecting and Communicating with Devices

import { 
    read,
    write,
    connectToDevice
} from 'react-native-classicbluetooth-sdk';

const connectToDevice = async (deviceId) => {
  try {
    await connectDevice(deviceId);
    console.log(`Connected to: ${deviceId}`);
  } catch (error) {
    console.error("Failed to connect", error);
  }
};

const sendDataToDevice = async (device, data) => {
  try {
    await write(device, data);
    console.log("Data sent successfully");
  } catch (error) {
    console.error("Failed to send data:", error);
  }
};

const readDataFromDevice = async (device) => {
  try {
    const data = await read(device);
    console.log("Received data:", data);
  } catch (error) {
    console.error("Failed to read data:", error);
  }
};
1.0.5

4 months ago

1.0.4

4 months ago

1.0.3

4 months ago

1.0.2

4 months ago

1.0.1

4 months ago

1.0.0

4 months ago