【uniapp蓝牙】基于native.js链接ble和非ble蓝牙
uniapp不是仅支持低功耗蓝牙(基础蓝牙通讯不支持),有些可能需要基础蓝牙。我现在同步我的手机蓝牙列表低功耗,基础蓝牙都支持
/**
* @author wzj
* 通用蓝牙模块封装
* 搜索 ble 和非 ble 蓝牙设备
*/
class Ble {
constructor() {
// false 蓝牙关闭, true 蓝牙打开
this.bluetooth = false
this.systemPlatform = uni.$uv.os()
this.context = plus.android.importClass('android.content.Context')
this.locationManager = plus.android.importClass('android.location.LocationManager')
this.main = plus.android.runtimeMainActivity()
this.mainSvr = this.main.getSystemService(this.context.LOCATION_SERVICE)
this.Intent = plus.android.importClass('android.content.Intent')
this.Settings = plus.android.importClass('android.provider.Settings')
this.IntentFilter = plus.android.importClass('android.content.IntentFilter')
this.BluetoothAdapter = plus.android.importClass('android.bluetooth.BluetoothAdapter')
this.BluetoothDevice = plus.android.importClass('android.bluetooth.BluetoothDevice')
this.UUID = plus.android.importClass('java.util.UUID')
// 未配对蓝牙设备列表
this.unpairedList = []
// 已配对蓝牙设备列表
this.pairedList = []
}
// 监听蓝牙状态改变
listenerConnection() {
plus.bluetooth.onBLEConnectionStateChange(function (e) {
console.log('connection state changed: ' + JSON.stringify(e))
})
}
// 提示框
showToast(title, options = {}) {
uni.showToast({
title,
...options
})
}
// 初始化蓝牙模块
openBluetoothAdapter() {
this.getBluetoothState()
if (!this.bluetooth) {
this.showToast('请先打开蓝牙!', {
icon: 'error',
duration: 2000
})
return false
}
if (this.systemPlatform !== 'android') {
this.showToast('蓝牙功能只支持Android系统!', {
icon: 'error',
duration: 2000
})
return false
}
// 定位检测
this.checkLocation()
}
// gps是否开启
isProviderEnabled() {
return this.mainSvr.isProviderEnabled(this.locationManager.GPS_PROVIDER)
}
// 建立连接
createBLEConnection(mac_address) {
const that = this
var BAdapter = this.BluetoothAdapter.getDefaultAdapter()
let device = BAdapter.getRemoteDevice(mac_address)
plus.android.importClass(device)
let bdevice = new this.BluetoothDevice()
// 判断是否配对
const getBondState = device.getBondState() === bdevice.BOND_NONE
if (!getBondState) {
console.log('已配对蓝牙设备')
return true
}
// 参数如果跟取得的mac地址一样就配对
const addressSame = device.getAddress() === mac_address
if (!addressSame) return false
// 配对命令
const createBond = device.createBond()
if (!createBond) return false
let cha = setInterval(() => {
if (device.getBondState() === bdevice.BOND_BONDED) {
clearInterval(cha)
// 删除未配对蓝牙,添加到已配对
that.unpairedList.map((item, i) => {
if (item.address === mac_address) {
that.pairedList.push(item)
that.unpairedList.splice(i, 1)
}
})
}
}, 1000)
}
// 执行蓝牙打印
blueToothPrint(mac_address, dataToSend) {
if (!mac_address) {
this.showToast('请选择蓝牙打印机', {
icon: 'error',
})
return false
}
let uuid = this.UUID.fromString('00001101-0000-1000-8000-00805f9b34fb')
let BAdapter = this.BluetoothAdapter.getDefaultAdapter()
let device = BAdapter.getRemoteDevice(mac_address)
plus.android.importClass(device)
let bluetoothSocket = device.createInsecureRfcommSocketToServiceRecord(uuid)
plus.android.importClass(bluetoothSocket)
if (bluetoothSocket.isConnected()) {
let outputStream = bluetoothSocket.getOutputStream()
plus.android.importClass(outputStream)
let bytes = plus.android.invoke(dataToSend, 'getBytes', 'gbk')
console.log('发送数据完成', bytes)
outputStream.write(bytes)
outputStream.flush()
// 这里关键
device = null
// 必须关闭蓝牙连接否则意外断开的话打印错误
bluetoothSocket.close()
} else {
console.log('检测到设备未连接,尝试连接....')
bluetoothSocket.connect()
}
}
// 定位检测
checkLocation() {
const GPS_PROVIDER = this.isProviderEnabled()
if (GPS_PROVIDER) {
this.searchDevices()
return true
}
uni.showModal({
title: "提示",
content: "请打开定位服务功能",
showCancel: false, // 不显示取消按钮
success() {
if (GPS_PROVIDER) {
this.showToast('GPS功能已开启!', {
icon: 'success',
})
return true
}
const intent = new this.Intent(this.Settings.ACTION_LOCATION_SOURCE_SETTINGS)
// 打开系统设置GPS服务页面
this.main.startActivity(intent)
}
})
return false
}
// 开始搜寻附近的蓝牙外围设备
startBluetoothDevicesDiscovery() {
const that = this
let main = this.main
let BluetoothAdapter = this.BluetoothAdapter
uni.startBluetoothDevicesDiscovery({
success: (res) => {
let BAdapter = BluetoothAdapter.getDefaultAdapter()
const isEnabled = BAdapter !== null && !BAdapter.isEnabled()
if (isEnabled) {
const intent = new this.Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
main.startActivityForResult(intent, 200)
}
uni.showLoading({
title: "开始搜索设备",
})
let filter = new this.IntentFilter()
let bdevice = new this.BluetoothDevice()
let BluetoothDevice = this.BluetoothDevice
// 开启搜索
BAdapter.startDiscovery()
// 获取配得和未配对蓝牙回调
let receiver = plus.android.implements('io.dcloud.android.content.BroadcastReceiver', {
//实现onReceiver回调函数
onReceive: function (context, intent) {
// 通过 intent 实例引入 intent 类,方便以后的‘.’操作
plus.android.importClass(intent)
let BleDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE)
// 获取已经配对的蓝牙设备
const getBondState = BleDevice.getBondState() !== bdevice.BOND_NONE
if (getBondState) {
// 已配对蓝牙设备
const addressLen = that.unpairedList.filter(item => item.address === BleDevice.getAddress())
if (addressLen.length > 0) return
that.unpairedList.push({
name: BleDevice.getName(),
address: BleDevice.getAddress(),
})
}
const action = intent.getAction() === "android.bluetooth.adapter.action.DISCOVERY_FINISHED"
if (action) {
// 取消监听
main.unregisterReceiver(receiver)
uni.hideLoading()
return false
}
const isBondState = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE).getBondState() === 10
if (isBondState) {
const name = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE).getName()
const address = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE).getAddress()
var y = 0
const unpairedListLength = that.unpairedList.length
for (let x = 0; x < unpairedListLength; x++) {
let isAddress = that.unpairedList[x].address === address
if (isAddress) {
y++
}
}
if (y > 0) {
return y = 0
}
if (!name) {
return
}
that.unpairedList.push({
name,
address,
})
}
}
})
filter.addAction(bdevice.ACTION_FOUND)
filter.addAction(BAdapter.ACTION_DISCOVERY_STARTED)
filter.addAction(BAdapter.ACTION_DISCOVERY_FINISHED)
filter.addAction(BAdapter.ACTION_STATE_CHANGED)
main.registerReceiver(receiver, filter)
},
fail: (res) => {
console.log('startBluetoothDevicesDiscovery fail', res)
},
})
}
// 搜索蓝牙设备
searchDevices() {
this.openBluetooth(() => {
if (!this.bluetooth) {
return false
}
this.startBluetoothDevicesDiscovery()
})
}
// 打开蓝牙模块
openBluetooth(callBack) {
const that = this
uni.openBluetoothAdapter({
success(res) {
uni.getBluetoothAdapterState({
success(res) {
that.bluetooth = res.available
callBack && callBack()
}
})
},
fail(res) {
console.log('openBluetoothAdapter fail', res)
}
})
}
// 获取蓝牙状态
getBluetoothState() {
/* 判断是否打开蓝牙 */
this.openBluetooth()
}
}
export default Ble