{"id":26696583,"url":"https://github.com/patlux/react-native-bluetooth-state-manager","last_synced_at":"2025-05-16T15:09:03.374Z","repository":{"id":37484259,"uuid":"132113537","full_name":"patlux/react-native-bluetooth-state-manager","owner":"patlux","description":"Manage the bluetooth state of your device on android \u0026 iOS","archived":false,"fork":false,"pushed_at":"2025-03-27T21:14:26.000Z","size":2005,"stargazers_count":66,"open_issues_count":8,"forks_count":38,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-05-15T17:55:43.625Z","etag":null,"topics":["android","app","bluetooth","context","ios","manager","native","react","react-native","state"],"latest_commit_sha":null,"homepage":"https://patwoz.dev/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/patlux.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-05-04T08:42:17.000Z","updated_at":"2025-03-16T18:08:23.000Z","dependencies_parsed_at":"2025-04-09T22:20:13.766Z","dependency_job_id":null,"html_url":"https://github.com/patlux/react-native-bluetooth-state-manager","commit_stats":{"total_commits":66,"total_committers":10,"mean_commits":6.6,"dds":"0.31818181818181823","last_synced_commit":"58ffbfeca061c12bbbcb13284de48ff8f48e37b6"},"previous_names":[],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patlux%2Freact-native-bluetooth-state-manager","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patlux%2Freact-native-bluetooth-state-manager/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patlux%2Freact-native-bluetooth-state-manager/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patlux%2Freact-native-bluetooth-state-manager/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/patlux","download_url":"https://codeload.github.com/patlux/react-native-bluetooth-state-manager/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254553958,"owners_count":22090417,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","app","bluetooth","context","ios","manager","native","react","react-native","state"],"created_at":"2025-03-26T20:26:56.439Z","updated_at":"2025-05-16T15:08:58.359Z","avatar_url":"https://github.com/patlux.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# react-native-bluetooth-state-manager\n\n[![npm version](https://badge.fury.io/js/react-native-bluetooth-state-manager.svg)](https://badge.fury.io/js/react-native-bluetooth-state-manager) [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)\n\nThe only purpose of this library is to manage the Bluetooth state. Not more, not less.\n\nIf you need further functionality like connecting and communicating to a device, please look at [react-native-ble-plx](https://github.com/Polidea/react-native-ble-plx).\n\n## Features\n\n- [Manage bluetooth state](#getstate)\n- [Open bluetooth settings page of the OS](#opensettings)\n- [Declarative API](#declarative-api)\n\n## Installation\n\nUsing [Yarn](https://yarnpkg.com/): (recommended)\n\n```shell\nyarn add react-native-bluetooth-state-manager\n```\n\nUsing [npm](https://www.npmjs.com/):\n\n```shell\nnpm install react-native-bluetooth-state-manager --save\n```\n\n## Linking\n\n### React-Native \u003e= 0.60\n\nBeginning from 0.60 you don't need to link it anymore. For more see [here](https://github.com/react-native-community/cli/blob/master/docs/autolinking.md).\n\n**iOS**\n\nFor iOS you just need to install the pods.\n\n```sh\ncd ios/\npod install\ncd ..\n```\n\n### React-Native \u003c 0.60\n\n\u003cdetails\u003e\n\u003csummary\u003eClick here for instructions how to link it for react-native \u003c 0.60\u003c/summary\u003e\n\n### Automatic\n\nRun `react-native link react-native-bluetooth-state-manager`\n\n### Manual\n\n#### iOS\n\n##### With cocoapods\n\nAppend the following lines to your `ios/Podfile`:\n\n```diff\ntarget '\u003cyour-project\u003e' do\n  ...\n+ pod 'RNBluetoothStateManager', :path =\u003e '../node_modules/react-native-bluetooth-state-manager'\nend\n```\n\n##### Without cocoapods\n\n1.  In XCode, in the project navigator, right click `Libraries` ➜ `Add Files to [your project's name]`\n2.  Go to `node_modules` ➜ `react-native-bluetooth-state-manager` and add `RNBluetoothStateManager.xcodeproj`\n3.  In XCode, in the project navigator, select your project. Add `libRNBluetoothStateManager.a` to your project's `Build Phases` ➜ `Link Binary With Libraries`\n4.  Run your project (`Cmd+R`)\u003c\n\n#### Android\n\n##### Manually\n\n1.  in `android/settings.gradle`:\n\n```diff\n...\ninclude ':app'\n+ include ':react-native-bluetooth-state-manager'\n+ project(':react-native-bluetooth-state-manager').projectDir = new File(rootProject.projectDir,   '../node_modules/react-native-bluetooth-state-manager/android')\n```\n\n2.  in `android/app/build.gradle`:\n\n```diff\ndependencies {\n+  compile project(':react-native-bluetooth-state-manager')\n  ...\n  compile \"com.facebook.react:react-native:+\"  // From node_modules\n}\n```\n\n3.  in `android/app/src/main/java/[...]/MainApplication.java`\n\n```diff\n+ import de.patwoz.rn.bluetoothstatemanager.RNBluetoothStateManagerPackage;\n\npublic class MainApplication extends Application implements ReactApplication {\n  // ...\n\n  private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {\n    @Override\n    protected List\u003cReactPackage\u003e getPackages() {\n      return Arrays.\u003cReactPackage\u003easList(\n          new MainReactPackage(),\n+         new RNBluetoothStateManagerPackage()\n      );\n    }\n  };\n\n}\n```\n\n\u003c/details\u003e\n\n## Usage\n\n```javascript\nimport BluetoothStateManager from 'react-native-bluetooth-state-manager';\n```\n\n**iOS**\n\nYou must provide a short description why you need access to bluetooth in your app. Otherwise your app will crash when requesting for bluetooth:\n\n```\nThis app has crashed because it attempted to access privacy-sensitive data without a usage description.  The app's Info.plist must contain an NSBluetoothAlwaysUsageDescription key with a string value explaining to the user how the app uses this data.\n```\n\nSee: https://developer.apple.com/documentation/bundleresources/information_property_list/nsbluetoothalwaysusagedescription\n\n## API\n\nAn example you will find in `example/app/ExampleWithApi.js`\n\n| Method                                                      | Return Type        | OS           | Description                                                      |\n| ----------------------------------------------------------- | ------------------ | ------------ | ---------------------------------------------------------------- |\n| [getState()](#getstate)                                     | `Promise\u003cString\u003e`  | Android, iOS | Returns the current state of the bluetooth service.              |\n| [onStateChange(listener, emitCurrentState)](#onstatechange) | `Subscription`     | Android, iOS | Listen for bluetooth state changes.                              |\n| [openSettings()](#opensettings)                             | `Promise\u003cnull\u003e`    | Android, iOS | Opens the bluetooth settings. Please see below for more details. |\n| [requestToEnable()](#requesttoenable)                       | `Promise\u003cBoolean\u003e` | Android      | Show a dialog that allows the user to turn on Bluetooth.         |\n| [enable()](#enable)                                         | `Promise\u003cnull\u003e`    | Android      | Enables Bluetooth without further user interaction.              |\n| [disable()](#disable)                                       | `Promise\u003cnull\u003e`    | Android      | Disables Bluetooth without further user interaction.             |\n\n**Important**: To use `enable()` and `disable()` on android, you have to add `BLUETOOTH_ADMIN` permission to your `AndroidManifest.xml`:\n\n```diff\n  \u003cmanifest xmlns:android=\"http://schemas.android.com/apk/res/android\"\u003e\n+    \u003cuses-permission android:name=\"android.permission.BLUETOOTH_ADMIN\"/\u003e\n  \u003c/manifest\u003e\n```\n\n---\n\n### getState()\n\nReturns the current state of the bluetooth service.\n\n```js\nBluetoothStateManager.getState().then((bluetoothState) =\u003e {\n  switch (bluetoothState) {\n    case 'Unknown':\n    case 'Resetting':\n    case 'Unsupported':\n    case 'Unauthorized':\n    case 'PoweredOff':\n    case 'PoweredOn':\n    default:\n      break;\n  }\n});\n```\n\n### onStateChange(listener, emitCurrentState)\n\nListen for bluetooth state changes.\n\n```js\nBluetoothStateManager.onStateChange((bluetoothState) =\u003e {\n  // do something...\n}, true /*=emitCurrentState*/);\n```\n\n### openSettings()\n\n##### Android\n\nOpens the bluetooth settings.\n\nTested:\n\n- Android 6.0.1 (Huawei P8 Lite ALE-L21)\n- Android 7.1.1 (Galaxy J5 2016)\n- Android 8.0 (Galaxy S8+ SM-G955f)\n\n##### iOS\n\nOpens the settings page of the app. Please see [here](https://developer.apple.com/documentation/uikit/uiapplicationopensettingsurlstring).\n\n```js\nBluetoothStateManager.openSettings();\n```\n\n### requestToEnable()\n\nShow a dialog that allows the user to turn on Bluetooth. More here: [Android documentation](https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#ACTION_REQUEST_ENABLE).\n\n- This function is **only** on **android** available.\n\n```js\nBluetoothStateManager.requestToEnable().then((result) =\u003e {\n  // result === true -\u003e user accepted to enable bluetooth\n  // result === false -\u003e user denied to enable bluetooth\n});\n```\n\n### enable()\n\nEnables Bluetooth without further user interaction\n\n- This function is **only** on **android** available.\n- Needs the `BLUETOOTH_ADMIN` permission.\n\n```js\nBluetoothStateManager.enable().then((result) =\u003e {\n  // do something...\n});\n```\n\n### disable()\n\nDisables Bluetooth without further user interaction\n\n- This function is **only** on **android** available.\n\n- Needs the `BLUETOOTH_ADMIN` permission.\n\n```js\nBluetoothStateManager.disable().then((result) =\u003e {\n  // do something...\n});\n```\n\n## EVENTS\n\n| Name                                                          | Description                                   |\n| ------------------------------------------------------------- | --------------------------------------------- |\n| [EVENT_BLUETOOTH_STATE_CHANGE](#event_bluetooth_state_change) | Callback for when the bluetooth state changed |\n\n---\n\n### EVENT_BLUETOOTH_STATE_CHANGE\n\nCallback for when the bluetooth state changed\n\n```js\nBluetoothStateManager.addEventListener(\n  BluetoothStateManager.EVENT_BLUETOOTH_STATE_CHANGE,\n  (bluetoothState) =\u003e {\n    // do something...\n  }\n);\n\n// recommended: use the `onStateChange` function.\n```\n\n## Declarative API\n\nThe declarative way uses the new context api of React 16.3.\n\n```javascript\nimport { BluetoothState } from 'react-native-bluetooth-state-manager';\n```\n\n### `\u003cBluetoothState\u003e`\n\n#### props\n\n| Name               | Value Type        | Default value | Description                                                                                              |\n| ------------------ | ----------------- | ------------- | -------------------------------------------------------------------------------------------------------- |\n| `emitCurrentState` | `Boolean`         | `true`        | If true, current state will be emitted.                                                                  |\n| `onChange`         | `Function`        | `undefined`   | Callback which emits the current state (first argument) change and the previous state (second argument). |\n| `children`         | `Function` or any | `undefined`   |                                                                                                          |\n\n### `\u003cBluetoothState.PoweredOn\u003e`\n\nThe `children` prop of this component will rendered only when bluetooth is turned on.\n\n### `\u003cBluetoothState.PoweredOff\u003e`\n\nThe `children` prop of this component will rendered only when bluetooth is turned off.\n\n### `\u003cBluetoothState.Resetting\u003e`\n\nThe `children` prop of this component will rendered only when bluetooth state is changing.\n\n- \"PoweredOff\" -\u003e \"PoweredOn\"\n- \"PoweredOn\" -\u003e \"PoweredOff\"\n\n### `\u003cBluetoothState.Unauthorized\u003e`\n\nThe `children` prop of this component will rendered only when the app doesn't have the permission to use bluetooth.\n\n### `\u003cBluetoothState.Unsupported\u003e`\n\nThe `children` prop of this component will rendered only when the device doesn't support bluetooth.\n\n### `\u003cBluetoothState.Unknown\u003e`\n\nThe `children` prop of this component will rendered only when the bluetooth state is unknown.\n\n---\n\n### BluetoothState\n\nAn example you will find in `example/app/ExampleWithDeclarativeApi.js`\n\n##### Context\n\nEach component has access to the same context as shown below.\n\n```js\n{\n  bluetoothState: String,\n  openSettings: Function,\n  requestToEnable: Function,\n  enable: Function,\n  disable: Function,\n}\n```\n\n##### `children` prop as function:\n\n```jsx\n\u003cBluetoothState\u003e\n  {({ bluetoothState, openSettings, requestToEnable, enable, disable }) =\u003e {\n    // show something ...\n    return \u003cView /\u003e;\n  }}\n\u003c/BluetoothState\u003e\n```\n\n### BluetoothState.\\\u003cBluetoothStateType\\\u003e\n\n#### Example\n\n```jsx\nimport { BluetoothState } from 'react-native-bluetooth-state-manager';\n\n\u003cBluetoothState\u003e\n  \u003cBluetoothState.PoweredOn\u003e\n    \u003cText\u003eThis will rendered only when bluetooth is turned on.\u003c/Text\u003e\n  \u003c/BluetoothState.PoweredOn\u003e\n  \u003cBluetoothState.PoweredOff\u003e\n    {({ requestToEnable, openSettings }) =\u003e (\n      \u003cView\u003e\n        \u003cText\u003eThis will rendered only when bluetooth is turned off.\u003c/Text\u003e\n        \u003cButton\n          title=\"This will rendered only when bluetooth is turned off.\"\n          onPress={Platform.OS === 'android' ? requestToEnable : openSettings}\n        /\u003e\n      \u003c/View\u003e\n    )}\n  \u003c/BluetoothState.PoweredOff\u003e\n  \u003cBluetoothState.Resetting\u003e\n    \u003cActivityIndicator /\u003e\n  \u003c/BluetoothState.Resetting\u003e\n  \u003cBluetoothState.Unauthorized\u003e\n    \u003cText\u003eThis will rendered only when bluetooth permission is not granted.\u003c/Text\u003e\n  \u003c/BluetoothState.Unauthorized\u003e\n  \u003cBluetoothState.Unsupported\u003e\n    \u003cText\u003eThis will rendered only when bluetooth is not supported.\u003c/Text\u003e\n  \u003c/BluetoothState.Unsupported\u003e\n  \u003cBluetoothState.Unknown\u003e\n    \u003cText\u003eYou have a really strange phone.\u003c/Text\u003e\n  \u003c/BluetoothState.Unknown\u003e\n\u003c/BluetoothState\u003e;\n```\n\n## ToDo's\n\n- [ ] Add tests\n\n## Why?\n\n##### Why not just using [react-native-ble-plx](https://github.com/Polidea/react-native-ble-plx)?\n\nBecause it's to over bloated for my purpose.\nIn several of my projects I'm working on, I had to integrate several third-party SDK which communicates with different bluetooth devices (on the native side). So the only functionality I needed there (on the javascript side), was to check if the bluetooth is enabled to start the third-party SDK.\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpatlux%2Freact-native-bluetooth-state-manager","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpatlux%2Freact-native-bluetooth-state-manager","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpatlux%2Freact-native-bluetooth-state-manager/lists"}