react-native-ble-plx最新版本修改源码版本已失效,因为最终写数据调用的是bleAdapter.writeCharacteristicForDevice,这个方法接收最终只能接收Base64的值。如果需要修改源码,得去修改MultiPlatformBleAdapter第三方库的源码。鉴于react-native-ble-plx已两年没更新,所以,该fork的库暂时保留旧版本,不再更新。如果需要发送16进制然后转byte数组发数据的话 ,建议使用react-native-ble-manager。
尝试将结束标志[0x0D, 0x0A]跟消息体一起发送,但由于时间有限,swift和java也不熟悉,自己也没有小票打印机测试。 所以将消息体和结束标志分开发送,先发送消息体,如"Hello, World!",再发送结果标志"0D0A",即可成功打印。 如果你的小票打印结束标志不是"0D0A",可以根据comomit记录自行在源码中修改下if判断语句即可,如果有谁能解决一次性发送的问题,麻烦告知下,谢谢!
yarn add https://github.com/zhanguangao/react-native-ble-plx.git
npm install git+https://[email protected]/zhanguangao/react-native-ble-plx.git
This is React Native Bluetooth Low Energy library using RxBluetoothKit and RxAndroidBle under the hood.
It supports:
- observing device's Bluetooth adapter state
- scanning BLE devices
- making connections to peripherals
- discovering services/characteristics
- reading/writing characteristics
- observing characteristic notifications/indications
- reading RSSI
- negotiating MTU
- turning the device's Bluetooth adapter on
What this library does NOT support:
- communicating between phones using BLE (Peripheral support)
- bonding peripherals
React Native | 1.0.3 | 1.1.0 |
---|---|---|
0.60.5 | ✅2 | ✅2 |
0.59.10 | ✅ | ✅ |
0.58.6 | ✅ | ✅ |
0.57.8 | ✅ | ✅ |
0.56.1 | 💥1 | 💥1 |
0.55.4 | 💥1 | 💥1 |
1 fails on Android, although might work after updating gradle
2 may require usage of jetifier
on Android and migration to CocoaPods
on iOS.
1.1.0
- Add support for descriptors.
- Fix XCode 11 compilation error.
Interested in React Native project involving Bluetooth Low Energy? We can help you!
Learn more about Polidea's BLE services here.
Documentation can be found here.
Contact us at Gitter if you have any questions, feedback or want to help!
iOS (expo/Podfile and RN 0.60+, example setup)
- Make sure your Expo project is ejected (formerly: detached). You can read how to do it here and here. (only for expo)
npm install --save react-native-ble-plx
react-native link react-native-ble-plx
- Open Xcode workspace located inside
ios
folder and add empty Swift file if you don't have at least one:- Select File/New/File...
- Choose Swift file and click Next.
- Name it however you want, select your application target and create it.
- Accept to create Objective-C bridging header.
- Update your
ios/Podfile
to contain:pod 'react-native-ble-plx', :path => '../node_modules/react-native-ble-plx' pod 'react-native-ble-plx-swift', :path => '../node_modules/react-native-ble-plx'
- Enter
ios
folder and runpod update
- Minimal supported version of iOS is 8.0
- If you want to support background mode:
- In your application target go to
Capabilities
tab and enableUses Bluetooth LE Accessories
inBackground Modes
section. - Pass
restoreStateIdentifier
andrestoreStateFunction
toBleManager
constructor.
- In your application target go to
- Starting from iOS 13 add
NSBluetoothAlwaysUsageDescription
ininfo.plist
file.
iOS (react-native < 0.60, example setup)
-
npm install --save react-native-ble-plx
-
react-native link react-native-ble-plx
-
Open Xcode project located inside
ios
folder and add empty Swift file if you don't have at least one:- Select File/New/File...
- Choose Swift file and click Next.
- Name it however you want, select your targets and create it.
- Accept to create Objective-C bridging header.
-
Minimal supported version of iOS is 8.0
-
If you want to support background mode:
- In your application target go to
Capabilities
tab and enableUses Bluetooth LE Accessories
inBackground Modes
section. - Pass
restoreStateIdentifier
andrestoreStateFunction
toBleManager
constructor.
- In your application target go to
-
Starting from iOS 13 add
NSBluetoothAlwaysUsageDescription
ininfo.plist
file.
Android (example setup)
npm install --save react-native-ble-plx
react-native link react-native-ble-plx
- In
build.gradle
ofapp
module make sure that min SDK version is at least 18:
android {
...
defaultConfig {
minSdkVersion 18
...
- In
AndroidManifest.xml
, add Bluetooth permissions and update<uses-sdk/>
:
<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"/>
<uses-permission-sdk-23 android:name="android.permission.ACCESS_COARSE_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"/>
<uses-sdk
android:minSdkVersion="18"
...
- If you are using AndroidX, then for the time being you need to convert import statements in the Android library with jetifier. These steps apply for all react-native packages, which are during the transition period:
npm install --save-dev jetifier
- Run
jetify
script afternpm install
. You can do it by adding "postinstall" script to thepackage.json
file:... "postinstall": "npx jetify", ...
Add this to your app/proguard-rules.pro
-dontwarn com.polidea.reactnativeble.**