This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const [connectedDevice, setConnectedDevice] = useState(null); | |
const [connectedDeviceServices, setConnectedDeviceServices] = useState(null); | |
useEffect(() => { | |
let moniterCharacteristic; | |
let unSubscribeListener; | |
if (connectedDevice && connectedDeviceServices) { | |
// Device disconnect listener |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//CUSTOM HOOK | |
const isBluetoothOn = useBluetoothListener(); // We made above the code snippet, Whenever needed import and use it. | |
useEffect(() => { | |
// Perform the Scan BLE device Operation if Bluetooth adapter state is ON and Granted the required Permissions | |
isBluetoothOn && accessPermission && scanAvailableBleDevice(); | |
}, [isBluetoothOn, accessPermission]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const [accessPermission, setAccessPermission] = useState(false); | |
const [askAgain, setAskAgain] = useState({}); | |
//PERMISSION FOR ACCESS BLUETOOTH | |
useEffect(() => { | |
if (Number(Device.osVersion) >= 12) { // Device is import from "expo-device" library for check the device OS version | |
const result = PermissionsAndroid.requestMultiple([ | |
PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT, | |
PermissionsAndroid.PERMISSIONS.BLUETOOTH_SCAN, | |
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//SCANNING BLUETOOTH DEVICES | |
function scanAvailableBleDevice() { | |
let devicesObj = {}; | |
// _BleManager is the BleManager instance, We already import and initialize the instance in _BleManager variable | |
// If you need any clarification please check the "Usage" section, we already discussed above in this blog. | |
_BleManager.startDeviceScan( | |
[null], | |
{ allowDuplicates: false }, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useState, useEffect } from "react"; | |
import { BleManager } from "react-native-ble-plx"; | |
const _BleManager = new BleManager(); | |
const useBluetoothListener = () => { | |
const [isBluetoothOn, setIsBluetoothOn] = useState(false); | |
useEffect(() => { | |
const bleSubscription = _BleManager.onStateChange((state) => { |