{"library":"react-native-ble-plx","title":"React Native BLE Plx","description":"react-native-ble-plx is a comprehensive React Native library providing a low-level API for interacting with Bluetooth Low Energy (BLE) devices on both iOS and Android platforms. The current stable version is 3.5.1, with recent releases indicating an active development and maintenance cadence addressing bugs and improvements. It supports core BLE functionalities such as observing the Bluetooth adapter state, scanning for devices, connecting to peripherals, discovering services and characteristics, reading/writing characteristic values, observing notifications/indications, reading RSSI, and negotiating MTU. A key differentiator is its inclusion of an Expo config plugin for easier integration into managed Expo workflows (requiring prebuilding). The library explicitly does not support Bluetooth Classic, inter-phone BLE communication (peripheral mode), device bonding, or beacon technologies. It is built to provide robust control over BLE interactions in React Native applications.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install react-native-ble-plx"],"cli":null},"imports":["import { BleManager } from 'react-native-ble-plx';","import { State } from 'react-native-ble-plx';","import type { Device } from 'react-native-ble-plx';","import type { Characteristic } from 'react-native-ble-plx';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { BleManager, State } from 'react-native-ble-plx';\nimport { PermissionsAndroid, Platform } from 'react-native';\n\nconst manager = new BleManager();\n\nasync function requestBluetoothPermissions() {\n  if (Platform.OS === 'ios') {\n    return true; // iOS handles permissions differently, usually in Info.plist\n  }\n\n  if (Platform.OS === 'android') {\n    const apiLevel = Platform.Version;\n    if (apiLevel < 31) { // Android 11 (API 30) and below\n      const granted = await PermissionsAndroid.request(\n        PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,\n        {\n          title: 'Location Permission',\n          message: 'Bluetooth Low Energy requires Location Permission',\n          buttonNeutral: 'Ask Me Later',\n          buttonNegative: 'Cancel',\n          buttonPositive: 'OK',\n        }\n      );\n      return granted === PermissionsAndroid.RESULTS.GRANTED;\n    } else { // Android 12 (API 31) and above\n      const granted = await PermissionsAndroid.requestMultiple([\n        PermissionsAndroid.PERMISSIONS.BLUETOOTH_SCAN,\n        PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT,\n        PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,\n      ]);\n      return (\n        granted['android.permission.BLUETOOTH_SCAN'] === PermissionsAndroid.RESULTS.GRANTED &&\n        granted['android.permission.BLUETOOTH_CONNECT'] === PermissionsAndroid.RESULTS.GRANTED &&\n        granted['android.permission.ACCESS_FINE_LOCATION'] === PermissionsAndroid.RESULTS.GRANTED\n      );\n    }\n  }\n  return false;\n}\n\nexport const scanForDevices = async () => {\n  const hasPermission = await requestBluetoothPermissions();\n  if (!hasPermission) {\n    console.log('Bluetooth permissions not granted.');\n    return;\n  }\n\n  const subscription = manager.onStateChange((state) => {\n    if (state === State.PoweredOn) {\n      console.log('Bluetooth is powered on, starting scan...');\n      manager.startDeviceScan(null, { allowDuplicates: false }, (error, device) => {\n        if (error) {\n          console.error('Scan error:', error);\n          return;\n        }\n        if (device) {\n          console.log('Found device:', device.name || device.id);\n          // Example: Stop scan after finding one device\n          manager.stopDeviceScan();\n          // subscription.remove(); // Unsubscribe from state changes\n        }\n      });\n      subscription.remove(); // Remove state change listener after successful scan initiation\n    } else {\n      console.log('Bluetooth state:', state);\n    }\n  }, true);\n};\n\n// To stop scanning, typically called after a timeout or finding a device\n// manager.stopDeviceScan();\n\n// Don't forget to destroy the manager when your component unmounts or app closes\n// manager.destroy();","lang":"typescript","description":"Initializes the BleManager, requests necessary Bluetooth permissions for Android, monitors Bluetooth adapter state, and performs a device scan, logging found devices. It also demonstrates how to handle state changes and stop scanning.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}