Skip to content
This repository has been archived by the owner on Jun 5, 2023. It is now read-only.

Commit

Permalink
finish
Browse files Browse the repository at this point in the history
  • Loading branch information
patlux committed May 8, 2018
1 parent 149cc4f commit f2cb31c
Show file tree
Hide file tree
Showing 84 changed files with 10,606 additions and 86 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.pbxproj -text
47 changes: 47 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
example/

# 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

179 changes: 127 additions & 52 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
# react-native-bluetooth-state-manager

## Quick Links
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)

* [Installation](#installation)
* [Linking](#linking)
* [Usage](#usage)
* [API](#api)
* [EVENTS](#events)
The only purpose of this library is to manage the Bluetooth state. Not more, not less.

If 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).

## Why?

##### Why not just using [react-native-ble-plx](https://github.com/Polidea/react-native-ble-plx)?

Because it's to over bloated for my purpose.
In 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.

## Features

* [Manage bluetooth state](#getstate)
* [Open bluetooth settings page of the OS](#opensettings)
* [Declarative API](#declarative-api)
* [ToDo's](#todos)

## Installation

Expand Down Expand Up @@ -47,10 +56,10 @@ 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\`)<
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

Expand Down Expand Up @@ -78,7 +87,7 @@ dependencies {
3. in `android/app/src/main/java/[...]/MainApplication.java`

```diff
+ import de.patwoz.rn.bluetooth_state_manager;
+ import de.patwoz.rn.bluetoothstatemanager.RNBluetoothStateManagerPackage;

public class MainApplication extends Application implements ReactApplication {
// ...
Expand All @@ -88,7 +97,7 @@ public class MainApplication extends Application implements ReactApplication {
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
+ new RNBluetoothStateManager()
+ new RNBluetoothStateManagerPackage()
);
}
};
Expand All @@ -104,61 +113,113 @@ import BluetoothStateManager from 'react-native-bluetooth-state-manager';

## API

| Method | Return Type | OS |
| ----------------------------------------------------------- | ------------------------- | ------------ |
| [getState()](#getstate) | `Promise<BluetoothState>` | Android, iOS |
| [onStateChange(listener, emitCurrentState)](#onstatechange) | `Subscription` | Android, iOS |
| [enable()](#enable) | `Promise<Boolean>` | Android |
| [disable()](#disable) | `Promise<Boolean>` | Android |
| [requestToEnable()](#requesttoenable) | `Promise<Boolean>` | Android |
| [openSettings()](#opensettings) | `undefined` | Android, iOS |
An example you will find in `example/app/ExampleWithApi.js`

| Method | Return Type | OS | Description |
| ----------------------------------------------------------- | ------------------------- | ------------ | ---------------------------------------------------------------- |
| [getState()](#getstate) | `Promise<BluetoothState>` | Android, iOS | Returns the current state of the bluetooth service. |
| [onStateChange(listener, emitCurrentState)](#onstatechange) | `Subscription` | Android, iOS | Listen for bluetooth state changes. |
| [openSettings()](#opensettings) | `Promise<null>` | Android, iOS | Opens the bluetooth settings. Please see below for more details. |
| [requestToEnable()](#requesttoenable) | `Promise<Boolean>` | Android | Show a dialog that allows the user to turn on Bluetooth. |
| [enable()](#enable) | `Promise<null>` | Android | Enables Bluetooth without further user interaction. |
| [disable()](#disable) | `Promise<null>` | Android | Disables Bluetooth without further user interaction. |

**Important**: To use `enable()` and `disable()` on android, you have to add `BLUETOOTH_ADMIN` permission to your `AndroidManifest.xml`:

```diff
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
+ <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
</manifest>
```

---

### getState()

Returns the current state of the bluetooth service.

```js
BluetoothStateManager.getState().then(bluetoothState => {
// do something...
});
}, true /*=emitCurrentState*/);
```

### onStateChange(listener, emitCurrentState)

Listen for bluetooth state changes.

```js
BluetoothStateManager.onStateChange(bluetoothState => {
// do something...
});
}, true /*=emitCurrentState*/);
```

### enable()
### openSettings()

##### Android

Opens the bluetooth settings.

Tested:

* Android 6.0.1 (Huawei P8 Lite ALE-L21)
* Android 7.1.1 (Galaxy J5 2016)
* Android 8.0 (Galaxy S8+ SM-G955f)

##### iOS

Opens the bluetooth settings or the general settings.

The behavior is different in the iOS versions:

* **iOS 9 and below**: Not tested (I don't have a real device with iOS 9)
* **iOS 10**: Opens the bluetooth settings
* Tested on a iPhone 5S with iOS 10.3.3
* **iOS 11+**: Opens the general settings. **NOT** the bluetooth settings.
* Tested on a iPhone 7 with iOS 11.3.0 and iOS 11.3.1

```js
BluetoothStateManager.enable().then(result => {
// do something...
});
BluetoothStateManager.openSettings();
```

### disable()
### requestToEnable()

Show 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).

* This function is **only** on **android** available.

```js
BluetoothStateManager.disable().then(result => {
// do something...
BluetoothStateManager.requestToEnable().then(result => {
// result === true -> user accepted to enable bluetooth
// result === false -> user denied to enable bluetooth
});
```

### requestToEnable()
### enable()

Enables Bluetooth without further user interaction

* This function is **only** on **android** available.
* Needs the `BLUETOOTH_ADMIN` permission.

```js
BluetoothStateManager.requestToEnable().then(result => {
BluetoothStateManager.enable().then(result => {
// do something...
});
```

### openSettings()
### disable()

Disables Bluetooth without further user interaction

* This function is **only** on **android** available.

* Needs the `BLUETOOTH_ADMIN` permission.

```js
BluetoothStateManager.openSettings();
BluetoothStateManager.disable().then(result => {
// do something...
});
```

## EVENTS
Expand All @@ -181,61 +242,75 @@ BluetoothStateManager.addEventListener(
}
);

// or use the alias `onStateChange`. See above.
// recommended: use the `onStateChange` function.
```

## Declarative API

The declarative way uses the new conext api of React 16.3.
The declarative way uses the new context api of React 16.3.

### `<BluetoothState>`

#### props

| Name | Value Type | Default value | Description |
| ------------------ | ----------------- | ------------- | -------------------------------------------------------------------------------------------------------- |
| `emitCurrentState` | `Boolean` | `true` | If true, current state will be emitted. |
| `onChange` | `Function` | `undefined` | Callback which emits the current state (first argument) change and the previous state (second argument). |
| `children` | `Function` or any | `undefined` | |

### `<BluetoothState.PoweredOn>`

The `children` prop of this component will rendered only when bluetooth is turned on.

### `<BluetoothState.PoweredOff>`

The `children` prop of this component will rendered only when bluetooth is turned off.

### `<BluetoothState.Resetting>`

The `children` prop of this component will rendered only when bluetooth state is changing.

* "PoweredOff" -> "PoweredOn"
* "PoweredOn" -> "PoweredOff"

### `<BluetoothState.Unauthorized>`

The `children` prop of this component will rendered only when the app doesn't have the permission to use bluetooth.

### `<BluetoothState.Unsupported>`

The `children` prop of this component will rendered only when the device doesn't support bluetooth.

### `<BluetoothState.Unknown>`

The `children` prop of this component will rendered only when the bluetooth state is unknown.

---

### BluetoothState

An example you will find in `example/app/ExampleWithDeclarativeApi.js`

##### Context

Each component has access to the same context as shown below. Either as `render` or as `children` prop.
Each component has access to the same context as shown below.

```js
{
bluetoothState: String,
openSettings: Function,
requestToEnable: Function,
enable: Function,
disable: Function,
requestToEnable: Function,
openSettings: Function,
}
```

##### Using with `render` prop:

```jsx
<BluetoothState
render={({ bluetoothState, enable, disable, requestToEnable, openSettings }) => {
// show something ...
return <View />;
}}
/>
```

##### Or with `children` as function:
##### `children` prop as function:

```jsx
<BluetoothState>
{({ bluetoothState, enable, disable, requestToEnable, openSettings }) => {
{({ bluetoothState, openSettings, requestToEnable, enable, disable }) => {
// show something ...
return <View />;
}}
Expand Down
23 changes: 23 additions & 0 deletions RNBluetoothStateManager.podspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

Pod::Spec.new do |s|
s.name = "RNBluetoothStateManager"
s.version = "1.0.0"
s.summary = "RNBluetoothStateManager"
s.description = <<-DESC
RNBluetoothStateManager
DESC
s.homepage = "https://github.com/patlux/react-native-bluetooth-state-manager"
s.license = "MIT"
s.author = { "author" => "[email protected]" }
s.platform = :ios, "7.0"
s.source = { :git => "https://github.com/patlux/react-native-bluetooth-state-manager.git", :tag => "master" }
s.source_files = "ios/*.{h,m}"
s.requires_arc = true


s.dependency "React"
#s.dependency "others"

end


Loading

0 comments on commit f2cb31c

Please sign in to comment.