更新记录
1.1.1(2025-05-20)
更新文档
1.1.0(2025-05-13)
【新增】【检查设备是否已配对】【获取已配对设备列表】【蓝牙状态监听】【检查蓝牙是否开启并开启蓝牙】【检查位置服务是否开启】等多个接口 【新增】设备参数内新增【RSSI】参数方便设备开发 【修复】多个警告,修复设备连接时出现一些异常报错的情况。 【修复】删除多余没必要的权限
1.0.0(2025-04-09)
项目发布
查看更多平台兼容性
uni-app x
Chrome | Safari | Android | iOS | 鸿蒙 | 微信小程序 |
---|---|---|---|---|---|
- | - | 5.0 | × | × | × |
uni-Bluetooth(安卓经典蓝牙)
注意:本插件仅支持Android,请注意相关的条件编译。
开发文档
此插件以原生混编的方式实现了 Android/IOS 同步/异步 获取设备内存使用信息的功能。 注意:首次使用请打自定义基座,打自定义基座。 //目前已经支持了蓝牙初始化、配对、连接、发送数据、接收数据、断开连接、获取已配对设备、监听蓝牙状态并开启蓝牙、检查位置服务状态、检查相关权限并获取权限。 //如果需要其他接口的可与作者联系,作者上班之余可以开发更新。
- 更多功能请参考示例项目
使用示例:
<template>
<scroll-view direction="vertical" style="height: 100%;">
<text>注意:在操作之前,都需要先进行蓝牙初始化,初始化返回true即为初始化成功</text>
<text class="title">初始化和权限</text>
<view class="actions display-flex">
<button class="btn" @tap="initialize">蓝牙初始化</button>
<button class="btn" style="width: 300rpx;" @tap="requestPermission">检查并获取权限</button>
</view>
<view class="actions display-flex">
<button class="btn" style="width: 700rpx;" @tap="checkBluetoothStatus">检查蓝牙是否已开启并请求开启</button>
</view>
<view class="actions display-flex">
<button class="btn" style="width: 700rpx;" @tap="checkLocation">检查位置服务是否已开启</button>
</view>
<text class="title">设备扫描</text>
<view class="actions display-flex">
<button class="btn" @tap="scanDevice">设备扫描</button>
<button class="btn" @tap="stopScan">停止扫描</button>
</view>
<text class="title">蓝牙状态及设备信息获取</text>
<view class="actions display-flex">
<button class="btn" style="width: 300rpx;" @tap="setStatusLisen">蓝牙状态监听</button>
<button class="btn" style="width: 350rpx;" @tap="checkDevicePaired">检查设备是否已配对</button>
</view>
<text class="title">收发数据</text>
<view class="actions display-flex">
<button class="btn" style="width: 300rpx;" @tap="sendBluetoothData">蓝牙状态监听</button>
<button class="btn" style="width: 350rpx;" @tap="setDataReceive">设置接收监听</button>
</view>
<text class="title">已配对设备</text>
<view class="actions display-flex">
<button class="btn" style="width: 300rpx;" @tap="getPairedList">获取已配对设备</button>
</view>
<view v-if="deviceList2.length>0">
<text class="title">已配对设备列表</text>
<view class="list">
<view class="item space-between" v-for="(item,index) in deviceList2" :key="index">
<text class="name">{{item.name}}({{item.address}})</text>
<view class="action">
<button class="btn" @tap="connect(index)">链接</button>
</view>
</view>
</view>
</view>
<view v-if="deviceList.length>0">
<text class="title">设备扫描结果</text>
<view class="list">
<view class="item space-between" v-for="(item,index) in deviceList" :key="index">
<text class="name">{{item.name}}({{item.address}})</text>
<view class="action">
<button class="btn" @tap="paried(index)">配对</button>
</view>
</view>
</view>
</view>
</scroll-view>
</template>
<script>
import { init,checkBluetoothPermissions,getPairedDevices,isDevicePaired,setBluetoothStateCallback,isLocationServiceEnabled,checkAndEnableBluetooth ,blueDevice, startDiscovery, stopDiscovery,pairDevice,connectDevice, sendData,setDataReceiveListener} from "@/uni_modules/iboxs-Bluetooth";
export default {
data() {
return {
deviceList:[] as blueDevice[],
deviceList2:[] as blueDevice[],
}
},
methods: {
//蓝牙初始化
initialize() {
console.log('蓝牙初始化结果',init()); //输出初始化结果(true为初始化成功)
},
//权限检查
requestPermission(){
checkBluetoothPermissions(function(res:Boolean){ //检查权限
if(res){
console.log('已获得权限');
} else{
console.log('权限获取失败');
}
})
},
checkBluetooth(){
var res=checkBluetooth();
console.log('蓝牙状态',res);
},
//检查蓝牙状态,若关闭则请求开启蓝牙
checkBluetoothStatus(){
checkAndEnableBluetooth(function(res:Boolean){ //检查蓝牙是否已开启,未开启会请求开启,并将结果返回
if(res){
console.log('蓝牙已开启');
} else{
console.log('蓝牙未开启并请求开启失败');
}
})
},
//检查位置服务是否已开启(android使用蓝牙必须开启位置服务)
checkLocation(){
var res=isLocationServiceEnabled();
console.log('位置服务开启状态',res);
},
//开始扫描设备
scanDevice(){
this.deviceList=[] as blueDevice[];
let that=this;
var includePaired=true; //是否包括已配对的设备
let status=startDiscovery(includePaired,function(res:blueDevice){
console.log('扫描到设备',res);
if(res.name==null){
return;
}
if(that.deviceList.indexOf(res)>-1){
return;
}
that.deviceList.push(res);
})
console.log('开始扫描状态',status); //false为开启扫描失败
},
//停止扫描
stopScan(){
console.log('停止扫描设备',stopDiscovery());
},
//设置一个蓝牙状态的监听,监听系统蓝牙状态的改变(避免在使用过程中若用户关闭蓝牙时能及时发现)
setStatusLisen(){
setBluetoothStateCallback(function(res:Boolean){
console.log('监听到蓝牙状态变化,当前状态:',res);
})
console.log('设置蓝牙状态变化监听');
uni.showToast({
title:'设置监听成功'
})
},
//检查设备是否已配对
checkDevicePaired(){
var address="5C:C3:36:02:56:04";
let res=isDevicePaired(address);
console.log('设备配对情况',res);
},
//获取已配对设备列表
getPairedList(){
let list=getPairedDevices();
console.log('已配对设备列表',list)
this.deviceList2=list;
},
//设备配对
paried(index:Number){
var device:blueDevice=this.deviceList[index];
console.log('开始配对',device)
console.log(pairDevice(device,function(res:Boolean){
console.log('配对结果',res);
if(res==true){ //有些设备需要在配对成功后立刻链接,否则就会出错,目前尚不知什么原因
this.deviceList2.push(device);
connectDevice(device,function(res:Boolean){ //链接设备
if(res){
console.log('链接成功');
sendData('hello 老满'); //发送数据
setDataReceiveListener(function(data:String){ //设置接收数据的回调(新版本不再需要手动开启接收,链接完毕后即可开启接收,这里只是需要一个接受到数据时的回调)
console.log('接收到数据',data);
})
} else{
console.log('链接失败');
}
},false)
}
}))
},
//链接设备
//注意:(有些设备(例如windows电脑)需要在配对后立刻发起连接,否则会报连接超时,已配对设备需要先手动删除配对后才可以重新配对连接),这个目前尚不明原因。
connect(index:Number){
var device:blueDevice=this.deviceList2[index];
//发起连接(参数分别为:设备,链接结果回调,是否开启心跳)
connectDevice(device,function(res:Boolean){
if(res){
console.log('链接成功');
sendData('hello 老满'); //发送数据给设备
setDataReceiveListener(function(data:String){ //设置接收数据的回调(新版本不再需要手动开启接收,链接完毕后即可开启接收,这里只是需要一个接受到数据时的回调)
console.log('接收到数据',data);
})
} else{
console.log('链接失败');
}
},false);
},
sendBluetoothData(){
sendData('hello 老满');
},
setDataReceive(){
setDataReceiveListener(function(data:String){ //设置接收数据的回调(新版本不再需要手动开启接收,链接完毕后即可开启接收,这里只是需要一个接受到数据时的回调)
console.log('接收到数据',data);
})
}
}
}
</script>
<style lang="scss">
.title{
margin: 20rpx;
font-size: 16px;
}
.btn{
margin-left:10rpx;
}
.actions{
padding-bottom: 20px;
}
.list{
padding-left: 10rpx;
padding-right: 10rpx;
.item{
height: 40px;
border-bottom: 1px solid #aaa;
.name{
line-height: 40px;
}
.action{
.btn{
height: 30px;
width: 80px;
line-height: 30px;
margin-top: 5px;
}
}
}
}
</style>