Skip to content

Instantly share code, notes, and snippets.

View Naveen-spritle's full-sized avatar

Naveen-spritle

View GitHub Profile
@Naveen-spritle
Naveen-spritle / BLEAdapterStateListerner.js
Created May 27, 2023 03:22
I defined Custom Hook for Bluetooth adapted state because of we can only perform all Bluetooth operations, If Bluetooth adapter is "PoweredOn". So before perform the operation we should check the adapter state. That's why I defined the custom hook. whenever we needed we use it.
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) => {
@Naveen-spritle
Naveen-spritle / ScanBleDevices.js
Last active June 22, 2023 11:35
startDeviceScan() Method to scan the available BLE devices and setTimeout() function to stop the scanning after 2seconds by using stopDeviceScan() Method.
//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 },
@Naveen-spritle
Naveen-spritle / Bluetooth Permissions
Last active June 22, 2023 11:27
Checking the Android version to perform the appropriate permissions for appropriate Android version by using "expo-device" library
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,
@Naveen-spritle
Naveen-spritle / Custom_hook_usage
Last active June 22, 2023 11:23
Perform the Scan BLE device Operation if Bluetooth adapter state is ON and Granted the required Permissions
//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]);
@Naveen-spritle
Naveen-spritle / Connect_and_get_value
Last active June 22, 2023 11:17
Connecting the BLE weighing device by using device ID and get the displaying weight value of weighing device
const [connectedDevice, setConnectedDevice] = useState(null);
const [connectedDeviceServices, setConnectedDeviceServices] = useState(null);
useEffect(() => {
let moniterCharacteristic;
let unSubscribeListener;
if (connectedDevice && connectedDeviceServices) {
// Device disconnect listener