From 149cc4fd206ca808329531186ed2889b45307951 Mon Sep 17 00:00:00 2001 From: Patrick Wozniak Date: Fri, 4 May 2018 13:45:04 +0200 Subject: [PATCH] initial commit --- .eslintrc | 20 + .gitignore | 47 + .npmrc | 1 + .prettierrc | 8 + .yarnrc | 1 + README.md | 286 ++++ package.json | 31 + src/BluetoothStateManager.js | 24 + src/RNBluetoothStateManager.js | 12 + src/TypeDefinitions.js | 29 + src/index.js | 2 + yarn.lock | 2721 ++++++++++++++++++++++++++++++++ 12 files changed, 3182 insertions(+) create mode 100644 .eslintrc create mode 100644 .gitignore create mode 100644 .npmrc create mode 100644 .prettierrc create mode 100644 .yarnrc create mode 100644 README.md create mode 100644 package.json create mode 100644 src/BluetoothStateManager.js create mode 100644 src/RNBluetoothStateManager.js create mode 100644 src/TypeDefinitions.js create mode 100644 src/index.js create mode 100644 yarn.lock diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..1622926 --- /dev/null +++ b/.eslintrc @@ -0,0 +1,20 @@ +{ + "env": { + "browser": true, + "commonjs": true, + "es6": true, + "node": true, + "jest": true + }, + "parser": "babel-eslint", + "parserOptions": { + "ecmaFeatures": { + "jsx": true, + "experimentalObjectRestSpread": true + }, + "sourceType": "module" + }, + "globals": { + "__DEV__": false + } +} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c4cc96f --- /dev/null +++ b/.gitignore @@ -0,0 +1,47 @@ +example/ios/Pods/ + +# OSX +# +.DS_Store + +# node.js +# +node_modules/ +npm-debug.log +yarn-error.log + + +# Xcode +# +build/ +*.pbxuser +!default.pbxuser +*.mode1v3 +!default.mode1v3 +*.mode2v3 +!default.mode2v3 +*.perspectivev3 +!default.perspectivev3 +xcuserdata +*.xccheckout +*.moved-aside +DerivedData +*.hmap +*.ipa +*.xcuserstate +project.xcworkspace + + +# Android/IntelliJ +# +build/ +.idea +.gradle +local.properties +*.iml + +# BUCK +buck-out/ +\.buckd/ +*.keystore + diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000..3d56772 --- /dev/null +++ b/.npmrc @@ -0,0 +1 @@ +save-exact true diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..365e948 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,8 @@ +semi: true +tabWidth: 2 +useTabs: false +printWidth: 100 +singleQuote: true +trailingComma: "es5" +bracketSpacing: true +requirePragma: false diff --git a/.yarnrc b/.yarnrc new file mode 100644 index 0000000..3d56772 --- /dev/null +++ b/.yarnrc @@ -0,0 +1 @@ +save-exact true diff --git a/README.md b/README.md new file mode 100644 index 0000000..907bf03 --- /dev/null +++ b/README.md @@ -0,0 +1,286 @@ +# react-native-bluetooth-state-manager + +## Quick Links + +* [Installation](#installation) +* [Linking](#linking) +* [Usage](#usage) +* [API](#api) +* [EVENTS](#events) +* [Declarative API](#declarative-api) +* [ToDo's](#todos) + +## Installation + +Using [Yarn](https://yarnpkg.com/): (recommended) + +```shell +yarn add patlux/react-native-bluetooth-state-manager +``` + +Using [npm](https://www.npmjs.com/): + +```shell +npm install patlux/react-native-bluetooth-state-manager --save +``` + +## Linking + +### Automatic + +Run `react-native link react-native-bluetooth-state-manager` + +### Manual + +#### iOS + +##### With cocoapods + +Append the following lines to your `ios/Podfile`: + +```diff +target '' do + ... ++ pod 'RNBluetoothStateManager', :path => '../node_modules/react-native-bluetooth-state-manager' +end +``` + +##### Without cocoapods + +1. In XCode, in the project navigator, right click \`Libraries\` ➜ \`Add Files to [your project's name]\` +2. Go to \`node_modules\` ➜ \`react-native-bluetooth-state-manager\` and add \`RNBluetoothStateManager.xcodeproj\` +3. In XCode, in the project navigator, select your project. Add \`libRNBluetoothStateManager.a\` to your project's \`Build Phases\` ➜ \`Link Binary With Libraries\` +4. Run your project (\`Cmd+R\`)< + +#### Android + +##### Manually + +1. in `android/settings.gradle`: + +```diff +... +include ':app' ++ include ':react-native-bluetooth-state-manager' ++ project(':react-native-bluetooth-state-manager').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-bluetooth-state-manager/android') +``` + +2. in `android/app/build.gradle`: + +```diff +dependencies { ++ compile project(':react-native-bluetooth-state-manager') + ... + compile "com.facebook.react:react-native:+" // From node_modules +} +``` + +3. in `android/app/src/main/java/[...]/MainApplication.java` + +```diff ++ import de.patwoz.rn.bluetooth_state_manager; + +public class MainApplication extends Application implements ReactApplication { + // ... + + private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) { + @Override + protected List getPackages() { + return Arrays.asList( + new MainReactPackage(), ++ new RNBluetoothStateManager() + ); + } + }; + +} +``` + +## Usage + +```javascript +import BluetoothStateManager from 'react-native-bluetooth-state-manager'; +``` + +## API + +| Method | Return Type | OS | +| ----------------------------------------------------------- | ------------------------- | ------------ | +| [getState()](#getstate) | `Promise` | Android, iOS | +| [onStateChange(listener, emitCurrentState)](#onstatechange) | `Subscription` | Android, iOS | +| [enable()](#enable) | `Promise` | Android | +| [disable()](#disable) | `Promise` | Android | +| [requestToEnable()](#requesttoenable) | `Promise` | Android | +| [openSettings()](#opensettings) | `undefined` | Android, iOS | + +--- + +### getState() + +```js +BluetoothStateManager.getState().then(bluetoothState => { + // do something... +}); +``` + +### onStateChange(listener, emitCurrentState) + +```js +BluetoothStateManager.onStateChange(bluetoothState => { + // do something... +}); +``` + +### enable() + +```js +BluetoothStateManager.enable().then(result => { + // do something... +}); +``` + +### disable() + +```js +BluetoothStateManager.disable().then(result => { + // do something... +}); +``` + +### requestToEnable() + +```js +BluetoothStateManager.requestToEnable().then(result => { + // do something... +}); +``` + +### openSettings() + +```js +BluetoothStateManager.openSettings(); +``` + +## EVENTS + +| Name | Description | +| ------------------------------------------------------------- | --------------------------------------------- | +| [EVENT_BLUETOOTH_STATE_CHANGE](#event_bluetooth_state_change) | Callback for when the bluetooth state changed | + +--- + +### EVENT_BLUETOOTH_STATE_CHANGE + +Callback for when the bluetooth state changed + +```js +BluetoothStateManager.addEventListener( + BluetoothStateManager.EVENT_BLUETOOTH_STATE_CHANGE, + bluetoothState => { + // do something... + } +); + +// or use the alias `onStateChange`. See above. +``` + +## Declarative API + +The declarative way uses the new conext api of React 16.3. + +### `` + +### `` + +### `` + +### `` + +### `` + +### `` + +### `` + +--- + +### BluetoothState + +##### Context + +Each component has access to the same context as shown below. Either as `render` or as `children` prop. + +```js +{ + bluetoothState: String, + enable: Function, + disable: Function, + requestToEnable: Function, + openSettings: Function, +} +``` + +##### Using with `render` prop: + +```jsx + { + // show something ... + return ; + }} +/> +``` + +##### Or with `children` as function: + +```jsx + + {({ bluetoothState, enable, disable, requestToEnable, openSettings }) => { + // show something ... + return ; + }} + +``` + +### BluetoothState.\ + +#### Example + +```jsx + + + This will rendered only when bluetooth is turned on. + + + {({ requestToEnable, openSettings }) => ( + + This will rendered only when bluetooth is turned off. +