更新记录
1.0.0(2025-02-20)
平台兼容性
App |
快应用 |
微信小程序 |
支付宝小程序 |
百度小程序 |
字节小程序 |
QQ小程序 |
HBuilderX 3.6.8,Android:5.0,iOS:11,HarmonyNext:不支持 |
× |
× |
× |
× |
× |
× |
钉钉小程序 |
快手小程序 |
飞书小程序 |
京东小程序 |
鸿蒙元服务 |
× |
× |
× |
× |
× |
H5-Safari |
Android Browser |
微信浏览器(Android) |
QQ浏览器(Android) |
Chrome |
IE |
Edge |
Firefox |
PC-Safari |
× |
× |
× |
× |
× |
× |
× |
× |
× |
ly-uts-ble
开发文档
- 购买本插件前,请先试用、请先试用、请先试用,并充分自测确认满足需求之后再行购买。虚拟物品一旦购买之后无法退款;
- 如有使用上的疑问、bug,可以进交流群联系作者;
- 作者可承接各种插件定制;
- 请在合法范围内使用,若使用本插件做非法开发,本方概不负责;
- 请尽量使用示例代码测试功能,若自己项目中若遇到使用问题,请打包Demo工程复现后,反馈问题;
- 每个方法的入参可在插件interface.uts文件中查看,文件里有各个参数的说明。
uniapp-x 完整示例 试用时将以下代码复制到 .uvue页面中调试
<template>
<view class="content">
<view class="header">
<view class="btn" @click="initConfig()">初始化</view>
<view class="btn" @click="scanDevices()">扫描蓝牙</view>
<view class="btn" @click="closeBluetooth()">断开蓝牙</view>
<view class="btn" @click="sendDataAction()">发送数据</view>
</view>
<view class="header" style="margin: 20rpx 0;">
<view class="btn" @click="setMtu()">设置MTU</view>
<view class="btn" @click="characteristicNotification()">监听Notifi</view>
<view class="btn" @click="readData()">read数据</view>
<view class="btn" @click="getDeviceServiceIds()">获取服务与特征</view>
</view>
<view style="width: 750rpx;display: flex;flex-direction: column;" v-if="devices.length>0">
<view style="width: 750rpx;height: 80rpx;background-color: bisque;display: flex;align-items: center;">周边设备
</view>
<view class="list">
<view class="cell" style="background-color: azure;" v-for="(item,index) in devices"
:key="'bondDevice'+index" @click.stop="connectBluetooth(item.address)">
<text style="color:blueviolet;">名称:{{item.deviceName}}</text>
<text style="color: blue;">地址:{{item.address}}</text>
<view class="line" style="width: 750rpx;height: 1px;background-color: rgba(0, 0, 0, 0.4);"></view>
</view>
</view>
</view>
</view>
</template>
<script>
import {
InitOptions,
initBluetooth,
ScanResultData,
ScanMode,
MatchMode,
NumOfMatches,
MyApiFail,
scanBluetoothDevice,//扫描设备
ScanBluetoothOptions,
ConnectOptions,
connectDevice,
disConnectDevice,//断开连接
sendDataToBluetooth,//发送数据方法
SendDataOptions,//发送数据参数
SendDataResult,//发送数据回调
settingMtu,//设置mtu方法
SettingMtuOptions,//设置MTU配置
SettingMtuResult,//设置mtu回调
enableCharacteristicNotification,
NotifiOptions,//监听read 或Notifi
GetServicesOptions,//获取蓝牙的服务与特征
getCurrentDeviceServiceIds,//获取蓝牙所有服务方法
} from "@/uni_modules/ly-uts-ble"
export default {
data() {
return {
devices: [] as ScanResultData[]
}
},
onLoad() {
},
methods: {
//初始化
initConfig() {
initBluetooth({
onStatusChanged: (status) => {
if (status == 1) {
uni.showToast({
title: '蓝牙已连接',
icon: 'none'
})
} else if (status == 3) {
uni.showToast({
title: '蓝牙已断开',
icon: 'none'
})
}
},
fail: (fail) => {
switch (fail.code) {
case 10001://该设备蓝牙不可用
break;
case 10002://有部分权限未允许
break;
}
}
} as InitOptions);
},
//扫描蓝牙
scanDevices() {
scanBluetoothDevice({
scanTime: 5000,
deviceAddress: "C8:B2:1E:74:77:1D",//换成自己的
// serviceUuid: "",
scanResultBackcall: (result) => {
console.log(`蓝牙地址:${result.address}`)
if (!this.devices.includes(result) && result.deviceName != '') {
this.devices.push(result)
}
},
fail: (error) => {
uni.showToast({
title: error.mes ?? '',
icon: 'none'
})
}
} as ScanBluetoothOptions)
},
/**
* serviceID: "0000ffb0-0000-1000-8000-00805f9b34fb",
characteristicID: "0000FFB2",
*/
//连接蓝牙
connectBluetooth(address : string) {
connectDevice({
address: address,//扫描蓝牙返回的地址
connectResult: (result) => {
uni.showToast({
title: result == 200 ? '连接成功' : '连接失败'
})
}
} as ConnectOptions)
},
//断开蓝牙
closeBluetooth() {
disConnectDevice()
},
//发送数据
sendDataAction() {
sendDataToBluetooth({
content: 'hello worldhello worldhello worldhello worldhello worldhello world',
serviceID: '0000ffb0-0000-1000-8000-00805f9b34fb',//换成自己的
characteristicID: '0000FFB1',//换成自己的
sendDataResultHandle: (res) => {
uni.showToast({
title: res.status == 200 ? '发送成功' : '发送失败',
icon: 'none'
})
},
fail: (error) => {
uni.showToast({
title: error.mes ?? '',
icon: 'none'
})
}
} as SendDataOptions)
},
//设置mtu
setMtu() {
settingMtu({
mtu: 20,
settingMtuResultHandle: (res) => {
uni.showToast({
title: res.status == 200 ? '设置成功' : '设置失败',
icon: 'none'
})
console.log(res.mtu ?? "")
},
fail: (error) => {
uni.showToast({
title: error.mes ?? '',
icon: 'none'
})
}
} as SettingMtuOptions)
},
//监听notifi
characteristicNotification() {
enableCharacteristicNotification({
serviceID: '0000ffb0-0000-1000-8000-00805f9b34fb',
characteristicID: '0000FFB2',
onCharacteristicChanged: (value) => {
console.log(value.value ?? '')
},
fail: (error) => {
uni.showToast({
title: error.mes ?? '',
icon: 'none'
})
}
} as NotifiOptions)
},
//read数据
readData() {
},
//获取服务与特征
getDeviceServiceIds() {
getCurrentDeviceServiceIds({
servicesResult: (res) => {
console.log(res)
},
fail: (error) => {
uni.showToast({
title: error.mes ?? '',
icon: 'none'
})
}
} as GetServicesOptions)
}
}
}
</script>
<style>
.content {
width: 750rpx;
display: flex;
height: 100%;
flex-direction: column;
align-items: center;
}
.header {
width: 750rpx;
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-between;
padding: 0 10rpx;
box-sizing: border-box;
}
.btn {
display: flex;
height: 80rpx;
flex: 1;
align-items: center;
justify-content: center;
background-color: cadetblue;
color: white;
margin-right: 10rpx;
font-size: 28rpx;
}
</style>
uniapp 完整示例
uniapp接入时 Android端需设置targetSdkVersion >= 31, 设置教程
<template>
<view class="content">
<view class="header">
<view class="btn" @click="initConfig()">初始化</view>
<view class="btn" @click="scanDevices()">扫描蓝牙</view>
<view class="btn" @click="closeBluetooth()">断开蓝牙</view>
<view class="btn" @click="sendDataAction()">发送数据</view>
</view>
<view class="header" style="margin: 20rpx 0;">
<view class="btn" @click="setMtu()">设置MTU</view>
<view class="btn" @click="characteristicNotification()">监听Notifi</view>
<view class="btn" @click="readData()">read数据</view>
<view class="btn" @click="getDeviceServiceIds()">获取服务与特征</view>
</view>
<view style="width: 750rpx;display: flex;flex-direction: column;" v-if="devices.length>0">
<view style="width: 750rpx;height: 80rpx;background-color: bisque;display: flex;align-items: center;">周边设备
</view>
<view class="list">
<view class="cell" style="background-color: azure;" v-for="(item,index) in devices"
:key="'bondDevice'+index" @click.stop="connectBluetooth(item.address)">
<text style="color:blueviolet;">名称:{{item.deviceName}}</text>
<text style="color: blue;">地址:{{item.address}}</text>
<view class="line" style="width: 750rpx;height: 1px;background-color: rgba(0, 0, 0, 0.4);"></view>
</view>
</view>
</view>
</view>
</template>
<script>
import {
InitOptions,
initBluetooth,
ScanResultData,
ScanMode,
MatchMode,
NumOfMatches,
MyApiFail,
scanBluetoothDevice, //扫描设备
ScanBluetoothOptions,
ConnectOptions,
connectDevice,
disConnectDevice, //断开连接
sendDataToBluetooth, //发送数据方法
SendDataOptions, //发送数据参数
SendDataResult, //发送数据回调
settingMtu, //设置mtu方法
SettingMtuOptions, //设置MTU配置
SettingMtuResult, //设置mtu回调
enableCharacteristicNotification,
NotifiOptions, //监听read 或Notifi
GetServicesOptions, //获取蓝牙的服务与特征
getCurrentDeviceServiceIds, //获取蓝牙所有服务方法
} from "@/uni_modules/ly-uts-ble"
export default {
data() {
return {
devices: []
}
},
onLoad() {
},
methods: {
//初始化
initConfig() {
initBluetooth({
onStatusChanged: (status) => {
if (status == 1) {
uni.showToast({
title: '蓝牙已连接',
icon: 'none'
})
} else if (status == 3) {
uni.showToast({
title: '蓝牙已断开',
icon: 'none'
})
}
},
fail: (fail) => {
switch (fail.code) {
case 10001: //该设备蓝牙不可用
break;
case 10002: //有部分权限未允许
break;
}
}
});
},
//扫描蓝牙
scanDevices() {
scanBluetoothDevice({
scanTime: 5000,
deviceAddress: "C8:B2:1E:74:77:1D",
matchMode: "MATCH_MODE_STICKY",
// serviceUuid: "",
scanResultBackcall: (result) => {
console.log(`蓝牙地址:${result.address}`)
if (!this.devices.includes(result) && result.deviceName != '') {
this.devices.push(result)
}
},
fail: (error) => {
uni.showToast({
title: error.mes ?? '',
icon: 'none'
})
}
})
},
/**
* serviceID: "0000ffb0-0000-1000-8000-00805f9b34fb",
characteristicID: "0000FFB2",
*/
//连接蓝牙
connectBluetooth(address) {
connectDevice({
address: address,
connectResult: (result) => {
uni.showToast({
title: result == 200 ? '连接成功' : '连接失败'
})
}
})
},
//断开蓝牙
closeBluetooth() {
disConnectDevice()
},
//发送数据
sendDataAction() {
sendDataToBluetooth({
content: 'hello worldhello worldhello worldhello worldhello worldhello world',
serviceID: '0000ffb0-0000-1000-8000-00805f9b34fb',
characteristicID: '0000FFB1',
sendDataResultHandle: (res) => {
uni.showToast({
title: res.status == 200 ? '发送成功' : '发送失败',
icon: 'none'
})
},
fail: (error) => {
uni.showToast({
title: error.mes ?? '',
icon: 'none'
})
}
})
},
//设置mtu
setMtu() {
settingMtu({
mtu: 20,
settingMtuResultHandle: (res) => {
uni.showToast({
title: res.status == 200 ? '设置成功' : '设置失败',
icon: 'none'
})
console.log(res.mtu ?? "")
},
fail: (error) => {
uni.showToast({
title: error.mes ?? '',
icon: 'none'
})
}
})
},
//监听notifi
characteristicNotification() {
enableCharacteristicNotification({
serviceID: '0000ffb0-0000-1000-8000-00805f9b34fb',
characteristicID: '0000FFB2',
onCharacteristicChanged: (value) => {
console.log(value.value ?? '')
},
fail: (error) => {
uni.showToast({
title: error.mes ?? '',
icon: 'none'
})
}
})
},
//read数据
readData() {
},
//获取服务与特征
getDeviceServiceIds() {
getCurrentDeviceServiceIds({
servicesResult: (res) => {
console.log(res)
},
fail: (error) => {
uni.showToast({
title: error.mes ?? '',
icon: 'none'
})
}
})
}
}
}
</script>
<style>
.content {
width: 750rpx;
display: flex;
height: 100%;
flex-direction: column;
align-items: center;
}
.header {
width: 750rpx;
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-between;
padding: 0 10rpx;
box-sizing: border-box;
}
.btn {
display: flex;
height: 80rpx;
flex: 1;
align-items: center;
justify-content: center;
background-color: cadetblue;
color: white;
margin-right: 10rpx;
font-size: 28rpx;
}
</style>