Page({
data: {
info:"未初始化蓝牙适配器",
devices:[],
connectedDeviceId:"",
deviceId:"",
services:"",
servicesUUID:"0000ff00-0000-1000-8000-00805f9b34fb",
serviceId:"",
notifyCharacteristicsId:"",
writeCharacteristicsId: "",
wifiname:"",
wifipwd:""
},
lanyatest1(event){
var that = this;
wx.openBluetoothAdapter({
success: function (res) {
console.log('初始化蓝牙适配器成功')
that.setData({
info: '初始化蓝牙适配器成功'
})
},
fail: function (res) {
console.log('请打开蓝牙和定位功能')
that.setData({
info: '请打开蓝牙和定位功能'
})
}
})
},
lanyatest2(event){
var that = this;
wx.getBluetoothAdapterState({
success: function (res) {
console.log(JSON.stringify(res.errMsg) + "\n蓝牙是否可用:" + res.available);
that.setData({
info: JSON.stringify(res.errMsg) +"\n蓝牙是否可用:" + res.available
})
},
fail: function (res) {
console.log(JSON.stringify(res.errMsg) + "\n蓝牙是否可用:" + res.available);
that.setData({
info: JSON.stringify(res.errMsg) + "\n蓝牙是否可用:" + res.available
})
}
})
},
lanyatest3(event){
var that = this;
wx.startBluetoothDevicesDiscovery({
services: ['FEE7'],
success: function (res) {
that.setData({
info: "搜索设备" + JSON.stringify(res),
})
console.log('搜索设备返回' + JSON.stringify(res))
}
})
},
lanyatest4(event){
var that = this;
wx.getBluetoothDevices({
success: function (res) {
that.setData({
info: "设备列表\n" + JSON.stringify(res.devices),
devices: res.devices
})
console.log('搜设备数目:' + res.devices.length)
console.log('设备信息:\n' + JSON.stringify(res.devices)+"\n")
}
})
},
lanyaconnect(event){
var that = this;
wx.createBLEConnection({
deviceId: event.currentTarget.id,
success: function (res) {
console.log('调试信息:' + res.errMsg);
that.setData({
connectedDeviceId: event.currentTarget.id,
info: "MAC地址:" + event.currentTarget.id + ' 调试信息:' + res.errMsg,
})
},
fail: function () {
console.log("连接失败");
},
})
},
lanyatest6(event){
var that = this;
wx.stopBluetoothDevicesDiscovery({
success: function (res) {
console.log("停止搜索" + JSON.stringify(res.errMsg));
that.setData({
info: "停止搜索" + JSON.stringify(res.errMsg),
})
}
})
},
lanyatest7(event){
var that = this;
wx.getBLEDeviceServices({
deviceId: that.data.connectedDeviceId,
success: function (res) {
console.log('services UUID:\n', JSON.stringify(res.services));
for (var i = 0; i < res.services.length; i++) {
console.log("第"+(i+1) + "个UUID:" + res.services[i].uuid+"\n")
}
that.setData({
services: res.services,
servicesUUID: res.services[0].uuid,
info: JSON.stringify(res.services),
})
}
})
},
lanyatest8(event){
var that = this;
var myUUID = that.data.servicesUUID;
console.log('UUID' + myUUID)
wx.getBLEDeviceCharacteristics({
deviceId: that.data.connectedDeviceId,
serviceId: myUUID,
success: function (res) {
console.log("%c getBLEDeviceCharacteristics", "color:red;");
for (var i = 0; i < res.characteristics.length; i++) {
console.log('特征值:' + res.characteristics[i].uuid)
if (res.characteristics[i].properties.notify) {
console.log("notifyServicweId:", myUUID);
console.log("notifyCharacteristicsId:", res.characteristics[i].uuid);
that.setData({
notifyServicweId: myUUID,
notifyCharacteristicsId: res.characteristics[i].uuid,
})
}
if (res.characteristics[i].properties.write) {
console.log("writeServicweId:", myUUID);
console.log("writeCharacteristicsId:", res.characteristics[i].uuid);
that.setData({
writeServicweId: myUUID,
writeCharacteristicsId: res.characteristics[i].uuid,
})
}
}
console.log('device getBLEDeviceCharacteristics:', res.characteristics);
that.setData({
msg: JSON.stringify(res.characteristics),
})
},
fail: function () {
console.log("fail");
},
})
},
lanyatest9(event){
var that = this;
var notifyServicweId = that.data.servicesUUID;
var notifyCharacteristicsId = that.data.notifyCharacteristicsId;
console.log("启用notify的serviceId", notifyServicweId);
console.log("启用notify的notifyCharacteristicsId", notifyCharacteristicsId);
wx.notifyBLECharacteristicValueChange({
state: true,
deviceId: that.data.connectedDeviceId,
serviceId: notifyServicweId,
characteristicId: that.data.notifyCharacteristicsId,
success: function (res) {
console.log('notifyBLECharacteristicValueChange success', res.errMsg)
var msg = '启动notify:' + res.errMsg
that.setData({
info: msg
})
},
fail: function (res) {
console.log('启动notify:' + res.errMsg);
},
})
},
lanyatest10(event){
var that = this;
console.log("开始接收数据");
wx.onBLECharacteristicValueChange(function (res) {
console.log("characteristicId:" + res.characteristicId)
console.log("serviceId:" + res.serviceId)
console.log("deviceId" + res.deviceId)
console.log("Length:" + res.value.byteLength)
console.log("hexvalue:" + ab2hex(res.value))
that.setData({
info: that.data.info + ab2hex(res.value)
})
})
},
lanyatest11(event){
var that = this
var hex = 'WiFi_Config:' + that.data.wifiname + ',' + that.data.wifipwd + '#END#';
console.log('要发送的信息是:', hex);
var bytesArray = [];
for (var i = 0; i < hex.length; i++) {
var encoded = unescape(encodeURIComponent(hex[i]));
for (var j = 0; j < encoded.length; j++) {
bytesArray.push(encoded.charCodeAt(j) & 0xFF);
}
}
var typedArray = new Uint8Array(bytesArray);
console.log('typedArray:', typedArray);
var buffer1 = typedArray.buffer;
console.log('buffer1:', buffer1);
wx.writeBLECharacteristicValue({
deviceId: that.data.connectedDeviceId,
serviceId: that.data.servicesUUID,
characteristicId: that.data.writeCharacteristicsId,
value: buffer1,
success: function(res) {
console.log('写入成功', res.errMsg);
},
fail: function(res) {
console.log('写入失败', res.errMsg);
console.warn('真机环境下需验证设备UUID有效性,开发者工具可能无法模拟蓝牙特性');
}
});
},
getwifiname(event){
this.setData({
wifiname:event.detail.value
})
},
getwifipwd(event){
this.setData({
wifipwd:event.detail.value
})
},
})
function ab2hex(buffer) {
var hexArr = Array.prototype.map.call(
new Uint8Array(buffer),
function (bit) {
return ('00' + bit.toString(16)).slice(-2)
}
)
return hexArr.join(',');
}