# react-native-ble-manager
[![npm version](https://img.shields.io/npm/v/react-native-ble-manager.svg?style=flat)](https://www.npmjs.com/package/react-native-ble-manager)
[![npm downloads](https://img.shields.io/npm/dm/react-native-ble-manager.svg?style=flat)](https://www.npmjs.com/package/react-native-ble-manager)
[![GitHub issues](https://img.shields.io/github/issues/innoveit/react-native-ble-manager.svg?style=flat)](https://github.com/innoveit/react-native-ble-manager/issues)
This is a porting of https://github.com/don/cordova-plugin-ble-central project to React Native.
## Requirements
RN 0.60+
RN 0.40-0.59 supported until 6.7.X
RN 0.30-0.39 supported until 2.4.3
## Supported Platforms
- iOS 8+
- Android (API 19+)
## Install
```shell
npm i --save react-native-ble-manager
```
The library support the react native autolink feature.
##### Android - Update Manifest
```xml
// file: android/app/src/main/AndroidManifest.xml
<!-- Add xmlns:tools -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="YOUR_PACKAGE_NAME">
<uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" android:maxSdkVersion="28"/>
<uses-permission-sdk-23 android:name="android.permission.ACCESS_FINE_LOCATION" tools:targetApi="Q"/>
<!-- Only when targeting Android 12 or higher -->
<!-- Please make sure you read the following documentation to have a
better understanding of the new permissions.
https://developer.android.com/guide/topics/connectivity/bluetooth/permissions#assert-never-for-location
-->
<!-- If your app doesn't use Bluetooth scan results to derive physical location information,
you can strongly assert that your app
doesn't derive physical location. -->
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT"
android:usesPermissionFlags="neverForLocation"
tools:targetApi="s" />
<!-- Needed only if your app looks for Bluetooth devices. -->
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<!-- Needed only if your app makes the device discoverable to Bluetooth devices. -->
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
...
```
If you need communication while the app is not in the foreground you need the "ACCESS_BACKGROUND_LOCATION" permission.
##### iOS - Update Info.plist
In iOS >= 13 you need to add the `NSBluetoothAlwaysUsageDescription` string key.
## Note
- Remember to use the `start` method before anything.
- If you have problem with old devices try avoid to connect/read/write to a peripheral during scan.
- Android API >= 23 require the ACCESS_COARSE_LOCATION permission to scan for peripherals. React Native >= 0.33 natively support PermissionsAndroid like in the example.
- Android API >= 29 require the ACCESS_FINE_LOCATION permission to scan for peripherals.
React-Native 0.63.X started targeting Android API 29.
- Before write, read or start notification you need to call `retrieveServices` method
- Because location and bluetooth permissions are runtime permissions, you **must** request these permissions at runtime along with declaring them in your manifest.
## Example
The easiest way to test is simple make your AppRegistry point to our example component, like this:
```javascript
// in your index.ios.js or index.android.js
import React, { Component } from "react";
import { AppRegistry } from "react-native";
import App from "react-native-ble-manager/example/App"; //<-- simply point to the example js!
AppRegistry.registerComponent("MyAwesomeApp", () => App);
```
Or, you can still look into the whole [example](https://github.com/innoveit/react-native-ble-manager/tree/master/example) folder for a standalone project.
## Methods
### start(options)
Init the module.
Returns a `Promise` object.
Don't call this multiple times.
**Arguments**
- `options` - `JSON`
The parameter is optional the configuration keys are:
- `showAlert` - `Boolean` - [iOS only] Show or hide the alert if the bluetooth is turned off during initialization
- `restoreIdentifierKey` - `String` - [iOS only] Unique key to use for CoreBluetooth state restoration
- `queueIdentifierKey` - `String` - [iOS only] Unique key to use for a queue identifier on which CoreBluetooth events will be dispatched
- `forceLegacy` - `Boolean` - [Android only] Force to use the LegacyScanManager
**Examples**
```js
BleManager.start({ showAlert: false }).then(() => {
// Success code
console.log("Module initialized");
});
```
### scan(serviceUUIDs, seconds, allowDuplicates, scanningOptions)
Scan for availables peripherals.
Returns a `Promise` object.
**Arguments**
- `serviceUUIDs` - `Array of String` - the UUIDs of the services to looking for. On Android the filter works only for 5.0 or newer.
- `seconds` - `Integer` - the amount of seconds to scan.
- `allowDuplicates` - `Boolean` - [iOS only] allow duplicates in device scanning
- `scanningOptions` - `JSON` - [Android only] after Android 5.0, user can control specific ble scan behaviors:
- `numberOfMatches` - `Number` - [Android only] corresponding to [`setNumOfMatches`](<https://developer.android.com/reference/android/bluetooth/le/ScanSettings.Builder.html#setNumOfMatches(int)>)
- `matchMode` - `Number` - [Android only] corresponding to [`setMatchMode`](<https://developer.android.com/reference/android/bluetooth/le/ScanSettings.Builder.html#setMatchMode(int)>)
- `scanMode` - `Number` - [Android only] corresponding to [`setScanMode`](<https://developer.android.com/reference/android/bluetooth/le/ScanSettings.Builder.html#setScanMode(int)>)
- `reportDelay` - `Number` - [Android only] corresponding to [`setReportDelay`](<https://developer.android.com/reference/android/bluetooth/le/ScanSettings.Builder.html#setReportDelay(long)>)
- `phy` - `Number` - [Android only] corresponding to [`setPhy`](https://developer.android.com/reference/android/bluetooth/le/ScanSettings.Builder#setPhy(int))
- `legacy` - `Boolean` - [Android only] corresponding to [`setLegacy`](https://developer.android.com/reference/android/bluetooth/le/ScanSettings.Builder#setLegacy(boolean))
**Examples**
```js
BleManager.scan([], 5, true).then(() => {
// Success code
console.log("Scan started");
});
```
### stopScan()
Stop the scanning.
Returns a `Promise` object.
**Examples**
```js
BleManager.stopScan().then(() => {
// Success code
console.log("Scan stopped");
});
```
### connect(peripheralId)
Attempts to connect to a peripheral. In many case if you can't connect you have to scan for the peripheral before.
Returns a `Promise` object.
> In iOS, attempts to connect to a peripheral do not time out (please see [Apple's doc](https://developer.apple.com/documentation/corebluetooth/cbcentralmanager/1518766-connect)), so you might need to set a timer explicitly if you don't want this behavior.
**Arguments**
- `peripheralId` - `String` - the id/mac address of the peripheral to connect.
**Examples**
```js
BleManager.connect("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")
.then(() => {
// Success code
console.log("Connected");
})
.catch((error) => {
// Failure code
console.log(error);
});
```
### disconnect(peripheralId, force)
Disconnect from a peripheral.
Returns a `Promise` object.
**Arguments**
- `peripheralId` - `String` - the id/mac address of the peripheral to disconnect.
- `force` - `boolean` - [Android only] defaults to true, if true force closes gatt
connection and send the BleManagerDisconnectPeripheral
event immediately to Javascript, else disconnects the
connection and waits for [`disconnected state`](https://developer.android.com/reference/android/b
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
react-native-ble-manager-master.zip (99个子文件)
react-native-ble-manager-master
ios
NSData+Conversion.h 203B
BLECommandContext.m 71B
BleManager.xcodeproj
project.xcworkspace
contents.xcworkspacedata 201B
xcshareddata
IDEWorkspaceChecks.plist 238B
project.pbxproj 12KB
BleManager.m 40KB
BLECommandContext.h 232B
CBPeripheral+Extensions.h 454B
BleManager.h 1KB
NSData+Conversion.m 1KB
CBPeripheral+Extensions.m 11KB
example
ios
example
main.m 200B
LaunchScreen.storyboard 6KB
AppDelegate.h 196B
AppDelegate.m 2KB
Images.xcassets
AppIcon.appiconset
Contents.json 849B
Contents.json 63B
Info.plist 2KB
exampleTests
exampleTests.m 2KB
Info.plist 733B
example.xcodeproj
project.xcworkspace
contents.xcworkspacedata 135B
xcshareddata
IDEWorkspaceChecks.plist 238B
project.pbxproj 39KB
xcshareddata
xcschemes
example-tvOS.xcscheme 3KB
example.xcscheme 3KB
Podfile 1KB
Podfile.lock 15KB
example-tvOSTests
Info.plist 733B
example-tvOS
Info.plist 1KB
example.xcworkspace
contents.xcworkspacedata 225B
xcshareddata
IDEWorkspaceChecks.plist 238B
.babelrc 148B
index.js 183B
.watchmanconfig 2B
android
gradle.properties 1KB
app
proguard-rules.pro 435B
debug.keystore 2KB
build_defs.bzl 602B
src
main
res
mipmap-xxxhdpi
ic_launcher.png 9KB
ic_launcher_round.png 15KB
mipmap-hdpi
ic_launcher.png 3KB
ic_launcher_round.png 5KB
values
strings.xml 70B
styles.xml 246B
mipmap-xxhdpi
ic_launcher.png 6KB
ic_launcher_round.png 10KB
mipmap-mdpi
ic_launcher.png 2KB
ic_launcher_round.png 3KB
mipmap-xhdpi
ic_launcher.png 4KB
ic_launcher_round.png 7KB
java
com
example
MainActivity.java 341B
MainApplication.java 3KB
AndroidManifest.xml 1KB
debug
java
com
example
ReactNativeFlipper.java 3KB
AndroidManifest.xml 368B
BUCK 1KB
build.gradle 8KB
settings.gradle 191B
build.gradle 1002B
.flowconfig 2KB
package.json 705B
__tests__
App-test.js 266B
app.json 51B
package-lock.json 1.04MB
.eslintrc.js 74B
.gitignore 813B
App.js 9KB
.buckconfig 114B
.prettierrc.js 120B
metro.config.js 770B
.gitattributes 16B
android
gradle.properties 52B
src
main
java
it
innove
LollipopScanManager.java 6KB
UUIDHelper.java 850B
Helper.java 5KB
NotifyBufferContainer.java 913B
BundleJSONConverter.java 6KB
Peripheral.java 32KB
BLECommand.java 1015B
BleManagerPackage.java 924B
BleManager.java 29KB
LollipopPeripheral.java 3KB
ScanManager.java 1KB
LegacyScanManager.java 3KB
AndroidManifest.xml 438B
settings.gradle 47B
build.gradle 1KB
package.json 686B
.github
FUNDING.yml 73B
ISSUE_TEMPLATE
bug_report.md 953B
LICENSE 11KB
package-lock.json 200B
.npmignore 216B
index.d.ts 4KB
BleManager.js 9KB
.gitignore 361B
README.md 25KB
yarn.lock 86B
react-native-ble-manager.podspec 584B
共 99 条
- 1
hiddu
- 粉丝: 0
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0