使用微信开发者工具编写蓝牙与MCU 8个字数组通讯的程序要求有发送及接收功能分别编写出indexjs、indexwxml、appjs、appwxml等文件需要有手机界面连接蓝牙按钮及连接状态要求自动获取UUID和特征值UUID值
以下是一个基本的微信小程序蓝牙与MCU通讯的示例代码,包括index.js、index.wxml、app.js、app.json等文件:
index.js:
const app = getApp()
Page({
data: {
devices: [],
connected: false,
services: [],
characteristics: [],
receiveData: []
},
openBluetoothAdapter: function() {
wx.openBluetoothAdapter({
success: (res) => {
console.log(res)
wx.showToast({
title: '蓝牙适配器已打开',
})
},
fail: (err) => {
console.error(err)
wx.showToast({
title: '蓝牙适配器打开失败',
icon: 'none'
})
}
})
},
startBluetoothDevicesDiscovery: function() {
wx.startBluetoothDevicesDiscovery({
success: (res) => {
console.log(res)
wx.showToast({
title: '正在搜索设备',
})
setTimeout(() => {
this.getBluetoothDevices()
}, 3000)
},
fail: (err) => {
console.error(err)
wx.showToast({
title: '搜索设备失败',
icon: 'none'
})
}
})
},
getBluetoothDevices: function() {
wx.getBluetoothDevices({
success: (res) => {
console.log(res)
const devices = res.devices
this.setData({
devices: devices
})
},
fail: (err) => {
console.error(err)
wx.showToast({
title: '获取设备列表失败',
icon: 'none'
})
}
})
},
createBLEConnection: function(e) {
const device = e.currentTarget.dataset.device
const deviceId = device.deviceId
const name = device.name
wx.createBLEConnection({
deviceId: deviceId,
success: (res) => {
console.log(res)
this.setData({
connected: true
})
wx.showToast({
title: '已连接到' + name,
})
this.getBLEDeviceServices(deviceId)
},
fail: (err) => {
console.error(err)
wx.showToast({
title: '连接失败',
icon: 'none'
})
}
})
},
getBLEDeviceServices: function(deviceId) {
wx.getBLEDeviceServices({
deviceId: deviceId,
success: (res) => {
console.log(res)
const services = res.services
this.setData({
services: services
})
for (let service of services) {
if (service.isPrimary) {
this.getBLEDeviceCharacteristics(deviceId, service.uuid)
}
}
},
fail: (err) => {
console.error(err)
wx.showToast({
title: '获取服务列表失败',
icon: 'none'
})
}
})
},
getBLEDeviceCharacteristics: function(deviceId, serviceId) {
wx.getBLEDeviceCharacteristics({
deviceId: deviceId,
serviceId: serviceId,
success: (res) => {
console.log(res)
const characteristics = res.characteristics
this.setData({
characteristics: characteristics
})
for (let characteristic of characteristics) {
if (characteristic.properties.read) {
wx.readBLECharacteristicValue({
deviceId: deviceId,
serviceId: serviceId,
characteristicId: characteristic.uuid
})
}
if (characteristic.properties.notify || characteristic.properties.indicate) {
wx.notifyBLECharacteristicValueChange({
deviceId: deviceId,
serviceId: serviceId,
characteristicId: characteristic.uuid,
state: true
})
}
}
},
fail: (err) => {
console.error(err)
wx.showToast({
title: '获取特征值列表失败',
icon: 'none'
})
}
})
},
onBLECharacteristicValueChange: function() {
wx.onBLECharacteristicValueChange((res) => {
console.log(res)
const receiveData = this.data.receiveData
receiveData.push(this.ab2hex(res.value))
this.setData({
receiveData: receiveData
})
})
},
writeBLECharacteristicValue: function(e) {
const value = e.detail.value
const deviceId = this.data.deviceId
const serviceId = this.data.serviceId
const characteristicId = this.data.characteristicId
const buffer = new ArrayBuffer(value.length)
const dataView = new DataView(buffer)
for (let i = 0; i < value.length; i++) {
dataView.setUint8(i, value[i])
}
wx.writeBLECharacteristicValue({
deviceId: deviceId,
serviceId: serviceId,
characteristicId: characteristicId,
value: buffer,
success: (res) => {
console.log(res)
wx.showToast({
title: '发送成功',
})
},
fail: (err) => {
console.error(err)
wx.showToast({
title: '发送失败',
icon: 'none'
})
}
})
},
ab2hex: function(buffer) {
const hexArr = Array.prototype.map.call(
new Uint8Array(buffer),
function(bit) {
return ('00' + bit.toString(16)).slice(-2)
}
)
return hexArr.join('')
}
})
index.wxml:
<view class="container">
<view class="content">
<view class="button-group">
<button class="button" bindtap="openBluetoothAdapter">打开蓝牙适配器</button>
<button class="button" bindtap="startBluetoothDevicesDiscovery">搜索设备</button>
</view>
<view class="device-list">
<view class="device-item" wx:for="{{devices}}" wx:key="{{index}}">
<text>{{item.name}}</text>
<button class="connect-button" bindtap="createBLEConnection" data-device="{{item}}">连接</button>
</view>
</view>
<view class="status">
<text>连接状态:{{connected ? '已连接' : '未连接'}}</text>
</view>
<view class="services">
<text>服务列表:</text>
<view class="service-item" wx:for="{{services}}" wx:key="{{index}}">
<text>{{item.uuid}}</text>
</view>
</view>
<view class="characteristics">
<text>特征值列表:</text>
<view class="characteristic-item" wx:for="{{characteristics}}" wx:key="{{index}}">
<text>{{item.uuid}}</text>
</view>
</view>
<view class="receive-data">
<text>接收的数据:</text>
<view class="data-item" wx:for="{{receiveData}}" wx:key="{{index}}">
<text>{{item}}</text>
</view>
</view>
<view class="send-data">
<text>发送的数据:</text>
<input class="input" bindinput="writeBLECharacteristicValue"></input>
</view>
</view>
</view>
app.js:
App({
onLaunch: function () {
wx.onBluetoothAdapterStateChange((res) => {
console.log(res)
if (!res.available) {
wx.showToast({
title: '蓝牙适配器已断开',
icon: 'none'
})
}
})
}
})
app.json:
{
"pages": [
"pages/index/index"
],
"window": {
"backgroundTextStyle": "light",
"navigationBarBackgroundColor": "#fff",
"navigationBarTitleText": "蓝牙通讯",
"navigationBarTextStyle": "black"
},
"networkTimeout": {
"request": 10000,
"downloadFile": 10000
},
"debug": true,
"tabBar": {}
}
以上代码实现了打开蓝牙适配器、搜索设备、连接设备、获取服务列表、获取特征值列表、发送数据和接收数据的功能。手机界面上会显示搜索到的设备列表、连接状态、服务列表、特征值列表、接收到的数据和发送的数据。
在使用时,可以将以上代码保存成对应的文件,并在微信开发者工具中运行
原文地址: http://www.cveoy.top/t/topic/iPeV 著作权归作者所有。请勿转载和采集!