tyanbiao / react-native-bluetooth-android

A react native bluetooth module for android platform

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

react-native-bluetooth-android

ReactNative 蓝牙库,适用于安卓平台

快速开始

使用npm

$ npm install react-native-bluetooth-android --save

使用 yarn

$ yarn add react-native-bluetooth-android

自动链接

$ react-native link react-native-bluetooth-android

手动安装

Android

  1. 打开 android/app/src/main/java/[...]/MainApplication.java
  • 引入 import com.tyanbiao.bluetooth.RNBluetoothPackage;
  • list() 方法中添加 packages.add(new RNBluetoothPackage());
  1. android/settings.gradle文件中加入:
    include ':react-native-bluetooth-android'
    project(':react-native-bluetooth-android').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-bluetooth-android/android')
    
  2. android/app/build.gradle的 dependencies 中插入:
    implementation project(':react-native-bluetooth-android')
    

示例

  1. git clone https://github.com/tyanbiao/react-native-bluetooth-android.git
  2. cd react-native-bluetooth-android/example
  3. npm i
  4. react-native run-android

使用

全部导入

import * as RNBluetooth from 'react-native-bluetooth-android'

单个导入

import { openBluetoothAdapter } from 'react-native-bluetooth-android'

初始化

const res = await openBluetoothAdapter()

获取已绑定设备

const devices = await listDevices()

返回设备列表,类型: Array.<Object>, devices结构:

{
	name: string
	address: string
}

搜索设备

startDevicesDiscovery()

停止搜索

stopDevicesDiscovery()

监听发现设备事件

onDeviceFound((device) => {
	console.log(device.name)
	console.log(device.address)
})

创建连接

const res = createConnection(address)

断开连接

closeConnection()

监听连接断开事件

onConnectionLost(() => {
	console.log('连接已断开')
})

发送数据

  • 发送二进制数据
const bufferView = new Uint8Array(10)
writeBuffer(bufferView.buffer)
  • 发送ascii字符串
const buffer = utils.stringToBuffer('hello world')
writeBuffer(buffer)
  • 发送hex字符串
const buffer = utils.hexToBuffer('550d0aeeff')
writeBuffer(buffer)

监听数据接收事件

const handler = (buffer) => {
	const bufferView = new Uint8Array(buffer)
	// do something
}
onDataReceived(handler)

接收到的数据类型为ArrayBuffer,可以根据实际需求做处理,utils对象内置了ArrayBuffer转换为ascii和hex编码的方法

import { utils } from 'react-native-bluetooth-android'

let hexStr = utils.bufferToHex(arrayBuffer)
let asciiStr = utils.bufferToString(arrayBuffer)

API 列表

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)
})

closeBluetoothAdapter

startDevicesDiscovery

stopDevicesDiscovery

listDevices

createConnection

closeConnection

writeBuffer

onDataReceived

onDeviceFound

onConnectionLost

utils

  • bufferToString
  • stringToBuffer
  • bufferToHex
  • hexToBuffer

About

A react native bluetooth module for android platform


Languages

Language:Java 80.6%Language:TypeScript 17.1%Language:JavaScript 1.2%Language:Starlark 1.0%