Skip to content

Commit

Permalink
chore: docs update
Browse files Browse the repository at this point in the history
  • Loading branch information
dimninik committed Sep 28, 2023
1 parent bb37438 commit 7d23517
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 73 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,11 @@
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).

## 3.0.0

- Updated MultiplatformBleAdapter to version 0.2.0.
- Updated RN bridge config
- Updated CI to RN 0.72.x
- Updated docs
- Updated dependencies
- Changed CI flow
- A example project has been added
43 changes: 43 additions & 0 deletions INTRO.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,49 @@ Note that you may experience undefined behaviour when calling a function on one
and continuing with another instance. A frequently made error is to create a new instance
of the manager for every re-render of a React Native Component.

## Ask for permissions

Check if you requested following permissions

- PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION
- PermissionsAndroid.PERMISSIONS.BLUETOOTH_SCAN (necessary for api 31+ )
- PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT (necessary for api 31+ )

eg.

```js
requestBluetoothPermission = async () => {
if (Platform.OS === 'ios') {
return true
}
if (Platform.OS === 'android' && PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION) {
const apiLevel = parseInt(Platform.Version.toString(), 10)

if (apiLevel < 31) {
const granted = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION)
return granted === PermissionsAndroid.RESULTS.GRANTED
}
if (PermissionsAndroid.PERMISSIONS.BLUETOOTH_SCAN && PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT) {
const result = await PermissionsAndroid.requestMultiple([
PermissionsAndroid.PERMISSIONS.BLUETOOTH_SCAN,
PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT,
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION
])

return (
result['android.permission.BLUETOOTH_CONNECT'] === PermissionsAndroid.RESULTS.GRANTED &&
result['android.permission.BLUETOOTH_SCAN'] === PermissionsAndroid.RESULTS.GRANTED &&
result['android.permission.ACCESS_FINE_LOCATION'] === PermissionsAndroid.RESULTS.GRANTED
)
}
}

this.showErrorToast('Permission have not been granted')

return false
}
```

## Waiting for Powered On state

When iOS application launches BLE stack is not immediately available and we need to check its status.
Expand Down
90 changes: 48 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ It supports:
- [observing characteristic notifications/indications](https://github.com/dotintent/react-native-ble-plx/wiki/Characteristic-Notifying)
- [reading RSSI](https://github.com/dotintent/react-native-ble-plx/wiki/RSSI-Reading)
- [negotiating MTU](https://github.com/dotintent/react-native-ble-plx/wiki/MTU-Negotiation)
- [background mode on iOS](https://github.com/dotintent/react-native-ble-plx/wiki/Background-mode-(iOS))
- [background mode on iOS](<https://github.com/dotintent/react-native-ble-plx/wiki/Background-mode-(iOS)>)
- turning the device's Bluetooth adapter on

It does NOT support:
Expand All @@ -43,22 +43,22 @@ It does NOT support:
This version (2.x) breaks compatibility with old RN versions. Please check [old README](./docs/README_V1.md) (1.x)
for the old instructions or [migration guide](./docs/MIGRATION_V1.md).

| React Native | 2.0.0 |
| ------------- | ------------------------------ |
| 0.63.3 | :white_check_mark: |
| 0.62.2 | :white_check_mark: |
| 0.61.5 | :white_check_mark: |
| 0.60.6 | :white_check_mark: |
| React Native | 2.0.0 |
| ------------ | ------------------ |
| 0.63.3 | :white_check_mark: |
| 0.62.2 | :white_check_mark: |
| 0.61.5 | :white_check_mark: |
| 0.60.6 | :white_check_mark: |

## Recent Changes

**2.0.3**

- Updated MultiplatformBleAdapter to version 0.1.9

[Current version changes](CHANGELOG.md)
[All previous changes](CHANGELOG-pre-03.md)


## Documentation & Support

Interested in React Native project involving Bluetooth Low Energy? [We can help you!](https://withintent.com/?utm_source=github&utm_medium=github&utm_campaign=external_traffic)
Expand Down Expand Up @@ -95,43 +95,49 @@ Contact us at [Gitter](https://gitter.im/RxBLELibraries/react-native-ble) if you
### Android ([example setup](https://github.com/Cierpliwy/SensorTag))

1. `npm install --save react-native-ble-plx`
1. In top level `build.gradle` make sure that min SDK version is at least 18:

```groovy
buildscript {
ext {
...
minSdkVersion = 21
...
```
1. In top level `build.gradle` make sure that min SDK version is at least 23:

```groovy
buildscript {
ext {
...
minSdkVersion = 23
...
```

1. In `build.gradle` make sure to add jitpack repository to known repositories:

```groovy
allprojects {
repositories {
...
maven { url 'https://www.jitpack.io' }
}
}
```
```groovy
allprojects {
repositories {
...
maven { url 'https://www.jitpack.io' }
}
}
```

1. (Optional) In `AndroidManifest.xml`, add Bluetooth permissions and update `<uses-sdk/>`:

```xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
...
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<!-- Please use FINE location instead of COARSE:
https://github.com/dotintent/react-native-ble-plx/issues/730#issuecomment-681946908 -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<!-- Add this line if your application always requires BLE. More info can be found on:
https://developer.android.com/guide/topics/connectivity/bluetooth-le.html#permissions
-->
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
...
```
```xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

...

<!-- Android >= 12 -->
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<!-- Android < 12 -->
<uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30" />
<!-- common -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<!-- Add this line if your application always requires BLE. More info can be found on:
https://developer.android.com/guide/topics/connectivity/bluetooth-le.html#permissions
-->
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>

...
```

## Troubleshooting
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ dependencies {
// For > 0.71, this will be replaced by `com.facebook.react:react-android:$version` by react gradle plugin
//noinspection GradleDynamicVersion
implementation "com.facebook.react:react-native:+"
implementation 'com.github.dotintent:MultiPlatformBleAdapter:48f9f7563c'
implementation 'com.github.dotintent:MultiPlatformBleAdapter:0.2.0'
}

if (isNewArchitectureEnabled()) {
Expand Down
2 changes: 2 additions & 0 deletions example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
<!-- common -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>

<application
android:name=".MainApplication"
android:label="@string/app_name"
Expand Down
1 change: 0 additions & 1 deletion example/ios/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ end

target 'BlePlxExample' do
config = use_native_modules!
pod 'MultiplatformBleAdapter', :git => 'https://github.com/Polidea/MultiPlatformBleAdapter.git', :branch => 'RxBluetooth-update'
# Flags change depending on the env values.
flags = get_default_flags()

Expand Down
20 changes: 6 additions & 14 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ PODS:
- hermes-engine/Pre-built (= 0.72.4)
- hermes-engine/Pre-built (0.72.4)
- libevent (2.1.12)
- MultiplatformBleAdapter (0.1.9)
- MultiplatformBleAdapter (0.2.0)
- OpenSSL-Universal (1.1.1100)
- RCT-Folly (2021.07.22.00):
- boost
Expand Down Expand Up @@ -377,7 +377,7 @@ PODS:
- React-logger (0.72.4):
- glog
- react-native-ble-plx (3.0.0):
- MultiplatformBleAdapter (= 0.1.9)
- MultiplatformBleAdapter (= 0.2.0)
- RCT-Folly (= 2021.07.22.00)
- React-Core
- react-native-safe-area-context (4.7.2):
Expand Down Expand Up @@ -528,7 +528,6 @@ DEPENDENCIES:
- glog (from `../node_modules/react-native/third-party-podspecs/glog.podspec`)
- hermes-engine (from `../node_modules/react-native/sdks/hermes-engine/hermes-engine.podspec`)
- libevent (~> 2.1.12)
- MultiplatformBleAdapter (from `https://github.com/Polidea/MultiPlatformBleAdapter.git`, branch `RxBluetooth-update`)
- OpenSSL-Universal (= 1.1.1100)
- RCT-Folly (from `../node_modules/react-native/third-party-podspecs/RCT-Folly.podspec`)
- RCTRequired (from `../node_modules/react-native/Libraries/RCTRequired`)
Expand Down Expand Up @@ -582,6 +581,7 @@ SPEC REPOS:
- FlipperKit
- fmt
- libevent
- MultiplatformBleAdapter
- OpenSSL-Universal
- SocketRocket
- YogaKit
Expand All @@ -600,9 +600,6 @@ EXTERNAL SOURCES:
hermes-engine:
:podspec: "../node_modules/react-native/sdks/hermes-engine/hermes-engine.podspec"
:tag: hermes-2023-08-07-RNv0.72.4-813b2def12bc9df02654b3e3653ae4a68d0572e0
MultiplatformBleAdapter:
:branch: RxBluetooth-update
:git: https://github.com/Polidea/MultiPlatformBleAdapter.git
RCT-Folly:
:podspec: "../node_modules/react-native/third-party-podspecs/RCT-Folly.podspec"
RCTRequired:
Expand Down Expand Up @@ -676,11 +673,6 @@ EXTERNAL SOURCES:
Yoga:
:path: "../node_modules/react-native/ReactCommon/yoga"

CHECKOUT OPTIONS:
MultiplatformBleAdapter:
:commit: 46dbede0d21229bba7b2c6c19e24926f8dcc11f7
:git: https://github.com/Polidea/MultiPlatformBleAdapter.git

SPEC CHECKSUMS:
boost: 57d2868c099736d80fcd648bf211b4431e51a558
CocoaAsyncSocket: 065fd1e645c7abab64f7a6a2007a48038fdc6a99
Expand All @@ -699,7 +691,7 @@ SPEC CHECKSUMS:
glog: 04b94705f318337d7ead9e6d17c019bd9b1f6b1b
hermes-engine: 81191603c4eaa01f5e4ae5737a9efcf64756c7b2
libevent: 4049cae6c81cdb3654a443be001fb9bdceff7913
MultiplatformBleAdapter: 5a6a897b006764392f9cef785e4360f54fb9477d
MultiplatformBleAdapter: ea8bac405ec200d0ca9de0f89afef6f06fb2abbc
OpenSSL-Universal: ebc357f1e6bc71fa463ccb2fe676756aff50e88c
RCT-Folly: 424b8c9a7a0b9ab2886ffe9c3b041ef628fd4fb1
RCTRequired: c0569ecc035894e4a68baecb30fe6a7ea6e399f9
Expand All @@ -716,7 +708,7 @@ SPEC CHECKSUMS:
React-jsiexecutor: c7f826e40fa9cab5d37cab6130b1af237332b594
React-jsinspector: aaed4cf551c4a1c98092436518c2d267b13a673f
React-logger: da1ebe05ae06eb6db4b162202faeafac4b435e77
react-native-ble-plx: 7f4d4334e071190d091faafc387ddcc3b217c6c5
react-native-ble-plx: 6f7f7d6e57bb109ff5751b8e5fce2654a9d7c033
react-native-safe-area-context: 7aa8e6d9d0f3100a820efb1a98af68aa747f9284
React-NativeModulesApple: edb5ace14f73f4969df6e7b1f3e41bef0012740f
React-perflogger: 496a1a3dc6737f964107cb3ddae7f9e265ddda58
Expand All @@ -740,6 +732,6 @@ SPEC CHECKSUMS:
Yoga: 3efc43e0d48686ce2e8c60f99d4e6bd349aff981
YogaKit: f782866e155069a2cca2517aafea43200b01fd5a

PODFILE CHECKSUM: 0f5087902db593c5807c52c7b1504d858d13b986
PODFILE CHECKSUM: e43e14d964b1de567b73b376b4538cddb7c50ca9

COCOAPODS: 1.12.1
32 changes: 26 additions & 6 deletions example/src/screens/MainStack/DashboardScreen/DashboardScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,40 @@ import { BleDevice } from '../../../components/molecules'
import { DropDown } from './DashboardScreen.styled'

type DashboardScreenProps = NativeStackScreenProps<MainStackParamList, 'DASHBOARD_SCREEN'>
type DeviceExtendedByUpdateTime = Device & { updateTimestamp: number }

const MIN_TIME_BEFORE_UPDATE_IN_MILLISECONDS = 5000

export function DashboardScreen({ navigation }: DashboardScreenProps) {
const [isConnecting, setIsConnecting] = useState(false)
const [foundDevices, setFoundDevices] = useState<Device[]>([])
const [foundDevices, setFoundDevices] = useState<DeviceExtendedByUpdateTime[]>([])

const addFoundDevice = (device: Device) => {
const addFoundDevice = (device: Device) =>
setFoundDevices(prevState => {
const indexToReplace = prevState.findIndex(currentDevice => currentDevice.id === device.id)
if (!isFoundDeviceUpdateNecessary(prevState, device)) {
return prevState
}
// deep clone
const nextState: typeof prevState = JSON.parse(JSON.stringify(prevState))
const extendedDevice: DeviceExtendedByUpdateTime = {
...device,
updateTimestamp: Date.now() + MIN_TIME_BEFORE_UPDATE_IN_MILLISECONDS
} as DeviceExtendedByUpdateTime

const indexToReplace = nextState.findIndex(currentDevice => currentDevice.id === device.id)
if (indexToReplace === -1) {
return prevState.concat(device)
return nextState.concat(extendedDevice)
}
prevState[indexToReplace] = device
return prevState
nextState[indexToReplace] = extendedDevice
return nextState
})

const isFoundDeviceUpdateNecessary = (currentDevices: DeviceExtendedByUpdateTime[], updatedDevice: Device) => {
const currentDevice = currentDevices.find(({ id }) => updatedDevice.id === id)
if (!currentDevice) {
return true
}
return currentDevice.updateTimestamp < Date.now()
}

const onConnectSuccess = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,9 @@ export function DevicenRFTestScreen(_props: DevicenRFTestScreenProps) {
return runTest(
() =>
BLEService.requestMTUForDevice(expectedMTU).then(device => {
if (Platform.OS === 'ios') {
return
}
if (!device) {
throw new Error('requestMTUForDevice error')
}
Expand Down
18 changes: 10 additions & 8 deletions example/src/services/BLEService/BLEService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,32 +380,34 @@ class BLEServiceInstance {
}

requestBluetoothPermission = async () => {
let arePermissionsGranted: boolean = Platform.OS === 'ios'
if (Platform.OS === 'ios') {
return true
}
if (Platform.OS === 'android' && PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION) {
const apiLevel = parseInt(Platform.Version.toString(), 10)

if (apiLevel < 31) {
const granted = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION)
arePermissionsGranted = granted === PermissionsAndroid.RESULTS.GRANTED
} else if (PermissionsAndroid.PERMISSIONS.BLUETOOTH_SCAN && PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT) {
return granted === PermissionsAndroid.RESULTS.GRANTED
}
if (PermissionsAndroid.PERMISSIONS.BLUETOOTH_SCAN && PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT) {
const result = await PermissionsAndroid.requestMultiple([
PermissionsAndroid.PERMISSIONS.BLUETOOTH_SCAN,
PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT,
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION
])

arePermissionsGranted =
return (
result['android.permission.BLUETOOTH_CONNECT'] === PermissionsAndroid.RESULTS.GRANTED &&
result['android.permission.BLUETOOTH_SCAN'] === PermissionsAndroid.RESULTS.GRANTED &&
result['android.permission.ACCESS_FINE_LOCATION'] === PermissionsAndroid.RESULTS.GRANTED
)
}
}

if (!arePermissionsGranted) {
this.showErrorToast('Permission have not been granted')
}
this.showErrorToast('Permission have not been granted')

return arePermissionsGranted
return false
}

showErrorToast = (error: string) => {
Expand Down
Loading

0 comments on commit 7d23517

Please sign in to comment.