var app = getApp()
Page({
data: {
lanya: "",
state: "",
msg: "",
sousuo: "",
status: "",
connectedDeviceId: "",
devices: [],
serviceId: "",
writeCharacteristicId: "",
notifyCharacteristicId: "",
},
onLoad: function() {
var that = this;
wx.openBluetoothAdapter({
success: function(res) {
console.log(res.errMsg);
that.setData({
lanya: "初始化小程序蓝牙" + res.errMsg
});
},
fail: function(res) {
console.log(res);
that.setData({
lanya: "初始化小程序蓝牙" + res.errMsg
});
}
});
},
adapterState: function() {
var that = this;
wx.getBluetoothAdapterState({
success: function(res) {
console.log(res);
that.setData({
state: res.errMsg
});
}
});
},
selectDevices: function() {
var that = this;
wx.startBluetoothDevicesDiscovery({
allowDuplicatesKey: false,
success: function(res) {
wx.onBluetoothAdapterStateChange(function(res) {
that.setData({
sousuo: res.discovering ? "在搜索" : "未搜索",
status: res.available ? "可用" : "不可用",
});
});
setTimeout(function() {
that.getDevices();
}, 1000);
}
});
},
getDevices: function() {
var that = this;
wx.getBluetoothDevices({
success: function(res) {
that.setData({
devices: res.devices
});
}
});
},
stop: function() {
var that = this;
wx.stopBluetoothDevicesDiscovery({
success: function(res) {
that.setData({
sousuo: res.discovering ? "在搜索" : "未搜索",
status: res.available ? "可用" : "不可用",
});
}
})
},
test: function(devices) {
wx.onBluetoothDeviceFound(function(devices) {
console.log(devices);
function ab2hex(buffer) {
var hexArr = Array.prototype.map.call(
new Uint8Array(buffer),
function(bit) {
return ('00' + bit.toString(16)).slice(-2)
}
)
return hexArr.join('');
}
console.log('new device list has founded');
console.log(ab2hex(devices.devices[0].advertisData));
});
},
connect: function(e) {
var that = this;
wx.showModal({
title: '提示',
content: '确定连接此设备吗?',
success: function(res) {
if (res.confirm) {
console.log('用户点击确定');
var clickId = e.currentTarget.id;
wx.createBLEConnection({
deviceId: clickId,
success: function(res) {
that.setData({
connectedDeviceId: clickId
});
wx.showToast({
title: '成功',
icon: 'success',
duration: 2000
});
wx.getBLEDeviceServices({
deviceId: clickId,
success: function(res) {
console.log('device services:', res.services);
that.setData({
service: res.services[1]
});
}
});
that.getService(clickId);
setTimeout(function() {
that.getCharacteristics(clickId, that.data.serviceId);
}, 3000);
wx.onBLEConnectionStateChange(function(res) {
console.log(`device ${res.deviceId} state has changed, connected: ${res.connected}`)
});
},
fail: function(res) {
console.log(res.errMsg);
}
});
} else if (res.cancel) {
console.log('用户点击取消')
}
}
});
},
getService: function(deviceId) {
var that = this;
wx.getBLEDeviceServices({
deviceId: deviceId,
success: function(res) {
console.log('device services:', res.services);
that.setData({
serviceId: res.services[1].uuid
});
}
});
},
getCharacteristics: function(deviceId, serviceId) {
var that = this;
wx.getBLEDeviceCharacteristics({
deviceId: deviceId,
serviceId: serviceId,
success: function(res) {
console.log(res);
console.log('device getBLEDeviceCharacteristics:', res.characteristics);
for (var i = 0; i < res.characteristics.length; i++) {
if (res.characteristics[i].properties.write && !res.characteristics[i].properties.read &&
!res.characteristics[i].properties.notify && !res.characteristics[i].properties.indicate) {
that.setData({
writeCharacteristicId: res.characteristics[i].uuid
});
} else if (!res.characteristics[i].properties.write && !res.characteristics[i].properties.read &&
res.characteristics[i].properties.notify && !res.characteristics[i].properties.indicate) {
that.setData({
notifyCharacteristicId: res.characteristics[i].uuid
});
}
}
console.log("writeCharacteristicId=" + that.data.writeCharacteristicId);
console.log("notifyCharacteristicId=" + that.data.notifyCharacteristicId);
wx.notifyBLECharacteristicValueChange({
state: true,
deviceId: deviceId,
serviceId: serviceId,
characteristicId: that.data.notifyCharacteristicId,
success: function(res) {
console.log('notifyBLECharacteristicValueChange success', res.errMsg);
}
});
that.onBLECharacteristicValueChange();
}
});
},
writeMsg: function() {
var that = this;
let buffer = new ArrayBuffer(7);
let dataView = new DataView(buffer);
dataView.setUint8(0, 0xaa);
dataView.setUint8(1, 0x04);
dataView.setUint8(2, (1 << 7));
dataView.setUint8(3, (1 << 3 | 1));
dataView.setUint8(4, 1);
dataView.setUint8(5, 1 << 2);
console.log(buffer);
wx.writeBLECharacteristicValue({
deviceId: that.data.connectedDeviceId,
serviceId: that.data.serviceId,
characteristicId: that.data.writeCharacteristicId,
value: buffer,
success: function(res) {
console.log('writeBLECharacteristicValue success', res.errMsg)
}
});
},
onBLECharacteristicValueChange: function() {
console.log("start change listen");
wx.onBLECharacteristicValueChange(function(res) {
console.log(`characteristic ${res.characteristicId} has changed, now is ${res.value}`)
console.log(ab2hext(res.value))
});
},
closeBLEConnection: function() {
var that = this;
wx.closeBLEConnection({
deviceId: that.data.connectedDeviceId,
success: function(res) {
console.log(res);
}
})
}
});