1.0.2 • Published 6 years ago
react-native-bluetooth-android v1.0.2
react-native-bluetooth-android
ReactNative 蓝牙库,适用于安卓平台
快速开始
$ npm install react-native-bluetooth-android --save
使用 yarn
$ yarn add react-native-bluetooth-android
自动链接
$ react-native link react-native-bluetooth-android
手动安装
Android
- 打开
android/app/src/main/java/[...]/MainActivity.java
import com.reactlibrary.RNBluetoothPackage;to the imports at the top of the file- Add
new RNBluetoothPackage()to the list returned by thegetPackages()method
- Append the following lines to
android/settings.gradle:include ':react-native-react-native-bluetooth' project(':react-native-react-native-bluetooth').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-react-native-bluetooth/android') - Insert the following lines inside the dependencies block in
android/app/build.gradle:compile project(':react-native-react-native-bluetooth')
使用
import * as RNBluetooth from 'react-native-bluetooth-android';API 列表
openBluetoothAdaptercloseBluetoothAdapterstartDevicesDiscoverystopDevicesDiscoverylistDevicescreateConnectioncloseConnectionwriteBufferonDataReceivedonDeviceFoundonConnectionLost
openBluetoothAdapter
使用async, await
try {
const res = await openBluetoothAdapter()
console.log(res)
} catch (e) {
console.error(e)
}使用promise
openBluetoothAdapter().then(result => {
console.log(res)
}).catch(e => {
console.error(e)
})