Page({
data: {
motto: &
#39;Hello World',
userInfo: {},
deviceId: &
#39;',
name: &
#39;',
serviceId: &
#39;',
services: [],
cd20: &
#39;',
cd01: &
#39;',
cd02: &
#39;',
cd03: &
#39;',
cd04: &
#39;',
characteristics20:
null
,
characteristics01:
null
,
characteristics02:
null
,
characteristics03:
null
,
characteristics04:
null
,
result,
},
onLoad:
function
(opt) {
var
that =
this
;
console.log(
"onLoad"
);
console.log(&
#39;deviceId=' + opt.deviceId);
console.log(&
#39;name=' + opt.name);
that.setData({ deviceId: opt.deviceId });
wx.onBLEConnectionStateChanged(
function
(res) {
console.log(`device ${res.deviceId} state has changed, connected: ${res.connected}`)
})
wx.createBLEConnection({
deviceId: that.data.deviceId,
success:
function
(res) {
console.log(res);
wx.getBLEDeviceServices({
deviceId: that.data.deviceId,
success:
function
(res) {
console.log(&
#39;device services:', res.services)
that.setData({ services: res.services });
console.log(&
#39;device services:', that.data.services[1].uuid);
that.setData({ serviceId: that.data.services[1].uuid });
console.log(&
#39;--------------------------------------');
console.log(&
#39;device设备的id:', that.data.deviceId);
console.log(&
#39;device设备的服务id:', that.data.serviceId);
setTimeout(
function
() {
wx.getBLEDeviceCharacteristics({
deviceId: that.data.deviceId,
serviceId: that.data.serviceId,
success:
function
(res) {
console.log(&
#39;000000000000' + that.data.serviceId);
console.log(&
#39;device getBLEDeviceCharacteristics:', res.characteristics)
for
(
var
i = 0; i < 5; i++) {
if
(res.characteristics[i].uuid.indexOf(
"cd20"
) != -1) {
that.setData({
cd20: res.characteristics[i].uuid,
characteristics20: res.characteristics[i]
});
}
if
(res.characteristics[i].uuid.indexOf(
"cd01"
) != -1) {
that.setData({
cd01: res.characteristics[i].uuid,
characteristics01: res.characteristics[i]
});
}
if
(res.characteristics[i].uuid.indexOf(
"cd02"
) != -1) {
that.setData({
cd02: res.characteristics[i].uuid,
characteristics02: res.characteristics[i]
});
}
if
(res.characteristics[i].uuid.indexOf(
"cd03"
) != -1) {
that.setData({
cd03: res.characteristics[i].uuid,
characteristics03: res.characteristics[i]
});
}
if
(res.characteristics[i].uuid.indexOf(
"cd04"
) != -1) {
that.setData({
cd04: res.characteristics[i].uuid,
characteristics04: res.characteristics[i]
});
}
}
console.log(&
#39;cd01= ' + that.data.cd01 + 'cd02= ' + that.data.cd02 + 'cd03= ' + that.data.cd03 + 'cd04= ' + that.data.cd04 + 'cd20= ' + that.data.cd20);
wx.onBLECharacteristicValueChange(
function
(characteristic) {
console.log(&
#39;characteristic value comed:', characteristic.value)
if
(characteristic.characteristicId.indexOf(
"cd01"
) != -1) {
const result = characteristic.value;
const hex = that.buf2hex(result);
console.log(hex);
}
if
(characteristic.characteristicId.indexOf(
"cd04"
) != -1) {
const result = characteristic.value;
const hex = that.buf2hex(result);
console.log(hex);
that.setData({ result: hex });
}
})
wx.notifyBLECharacteristicValueChanged({
deviceId: that.data.deviceId,
serviceId: that.data.serviceId,
characteristicId: that.data.cd01,
state:
true
,
success:
function
(res) {
console.log(&
#39;notifyBLECharacteristicValueChanged success', res);
},
fail:
function
(res) {
},
complete:
function
(res) {
}
})
wx.notifyBLECharacteristicValueChanged({
deviceId: that.data.deviceId,
serviceId: that.data.serviceId,
characteristicId: that.data.cd02,
state:
true
,
success:
function
(res) {
console.log(&
#39;notifyBLECharacteristicValueChanged success', res);
},
fail:
function
(res) {
},
complete:
function
(res) {
}
})
wx.notifyBLECharacteristicValueChanged({
deviceId: that.data.deviceId,
serviceId: that.data.serviceId,
characteristicId: that.data.cd03,
state:
true
,
success:
function
(res) {
console.log(&
#39;notifyBLECharacteristicValueChanged success', res);
},
fail:
function
(res) {
},
complete:
function
(res) {
}
})
wx.notifyBLECharacteristicValueChanged({
deviceId: that.data.deviceId,
serviceId: that.data.serviceId,
characteristicId: that.data.cd04,
state:
true
,
success:
function
(res) {
console.log(&
#39;notifyBLECharacteristicValueChanged success', res)
}
})
}, fail:
function
(res) {
console.log(res);
}
})
}
, 1500);
}
})
},
fail:
function
(res) {
},
complete:
function
(res) {
}
})
},
bindViewTap:
function
() {
var
that =
this
;
var
hex = &
#39;AA5504B10000B5'
var
typedArray =
new
Uint8Array(hex.match(/[\da-f]{2}/gi).map(
function
(h) {
return
parseInt(h, 16)
}))
console.log(typedArray)
console.log([0xAA, 0x55, 0x04, 0xB1, 0x00, 0x00, 0xB5])
var
buffer1 = typedArray.buffer
console.log(buffer1)
wx.writeBLECharacteristicValue({
deviceId: that.data.deviceId,
serviceId: that.data.serviceId,
characteristicId: that.data.cd20,
value: buffer1,
success:
function
(res) {
console.log(
"success 指令发送成功"
);
console.log(res);
},
fail:
function
(res) {
console.log(res);
},
complete:
function
(res) {
}
})
},
buf2hex:
function
(buffer) {
return
Array.prototype.map.call(
new
Uint8Array(buffer), x => (&
#39;00' + x.toString(16)).slice(-2)).join('');
}
})
暂时没有评论,来抢沙发吧~