微信小程序蓝牙连接怎么实现?
微信小程序蓝牙连接怎么实现?如果您要实现微信小程序和蓝牙连接,那么可以跟着小编来往下了解具体的实现方法,以下就是小编整理的微信小程序蓝牙连接怎么实现方法,希望对您有用。
微信小程序蓝牙连接怎么实现?
微信小程序蓝牙连接的流程如下:
首先要去初始化蓝牙适配器,去获取了本机蓝牙适配器的状态以后开始搜索,一定要在停止搜索以后再开始去搜索,就会触发蓝牙是配置状态变化的事件。
微信小程序搜索完成以后获取所有已经发现的蓝牙设备,就可以将devices中的设备Array取出来了。然后就可以得到所有已经连接的设备了,至于链接功能,还没有真机可测,所以没有测试。
微信小程序蓝牙连接所需要的微信小程序代码如下:
// pages/bluetooth/bluetooth.js
Page({
data:{},
onLoad:function(options){
// 微信小程序页面初始化 options为小程序页面跳转所带来的参数
},
//初始化蓝牙适配器
open,function(){
wx.openBluetoothAdapter({
success: function(res){
console.log(res.errMsg)
// success
wx.showToast({
title:"初始化蓝牙适配器成功",
duration:2000
})
},
})
},
//关闭小程序蓝牙模块
closef,function(){
woqpenBluetoothAdapter()
wclaoseBluetoothAdapter({
success: function(res){
// success
console.log("success"+res)
}
})
},
//获取本机蓝牙适配器状态
getBluetodaothAdapterState:function(){
wx.getBluetoothAdapterState({
succexass: function(res){
// success
console.log("res:"+res)
console.log("errMsg:"+res.errMsg)
}
})
},
//监听蓝牙适配器状态变化事件
onBluetoothAdapterStateChange:function(){
wx.onBluetoothAdapterStateChange(function(res) {
console.log(`adapterState changed, now is`, res)
})
},
// 开始搜寻附近的蓝牙外围设备
startBrluetoothDevicesDiscovery:function(){
wx.starwtBluetoothDevicesDiscovery({
success: function (res) {
console.log(res)
}
})
},
// 停止搜寻附近的蓝牙外围设备
stopBluetoothDevicesDiscovery:function(){
snhdamndfstopBluetoothDevicesDiscovery({
sucfcess: function (res) {
console.log(res)
}
})
},
//获取所有已发现的蓝牙设备
getBqqluetoothDevices:function(){
wx.getBluetootdff hDevices({
success: function(res){
// success
console.log(res)
},
})
},
//监听寻找到新设备的事件
onBluetoothDeviceFound:function(){
wx.onBluetoothDeviceFound(function(res) {
// callback
console.log(res)
})
},
//根据 uuid 获取处于已连接状态的设备
getConnectedBluetoothDevices:function(){
wx.getConnecfghxtedBluetoothDevices({
success: function (res) {
console.log(res)
}
})
},
//连接低功耗蓝牙设备
createBLEConnectisdeon:function(){
wx.createBLEConnection({
deviceId: 'AC:BC:32:C1:47:80',
success: function(res){
// success
console.log(res)
},
fail: funddaction(res) {
// fail
},
complete: function(res) {
// complete
}
})
},
//断开与低功耗蓝牙设备的连接
wx.clwwoseBLEConnection({
deviceId:a 'AC:BC:32:C1:47:80',
success: function (res) {
console.log(res)
}
})
},
//监听低功耗蓝牙连接的错误事件,包括设备丢失,连接异常断开等等
wx.onBgLEConnefctionSatateChanged(function(res) {
})
},
//获取蓝牙设备所有 service(服务)
getBLEDeviceServices:function(){
wx.getBLEDevidaceServices({
deviceId: '48:3B:38:88:E3:83',
success: function(res){
// success
},
fail: function(res) {
// fail
},
complete: function(res) {
// complete
}
})
},
//获取蓝牙设备所有 characteristic(特征值)
getBLEDeviceChjiubharacteristics:function(){
wx.getBLEDeviceChaaskhkracteristics({
deviceId: '48:3B:38:88:E3:83',
serviceId: 'serviceId',
success: function(res){
// success
},
fail: function(res) {
// fail
},
complete: function(res) {
// complete
}
})
}
})
微信小程序蓝牙连接怎么实现?感谢各位亲们的观看,以上全部内容供大家参考,相信以上小编整理的微信小程序蓝牙连接实现方法可以帮您解决问题哦,敬请大家多关注微小乔。谢谢您的支持。
微信小程序蓝牙开发怎么操作?
微信小程序蓝牙API使用指南
微信小程序蓝牙适配器接口