|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341 |
-
- const plugin = requirePlugin('ppScale-plugin');
- import { CommandButtonList } from '../../model/index'
- let app = getApp();
- Page({
- data: {
-
- selmac: null,
-
- scaleing: false,
-
-
-
-
- devLink: false,
-
-
-
- selectDev: null,
-
- deviceModel: null,
-
- contnectResult: null,
-
- sendData: null,
-
- callBack: null,
-
- sendBtnList: CommandButtonList.send,
-
- inputValue: '',
-
-
- },
-
-
-
- onUnload() {
-
-
- },
-
-
-
- onHide() {
-
- },
- onLoad() {
- let that = this;
-
- wx.onBLEConnectionStateChange(function (res) {
-
- console.log(`设备蓝牙连接状态`, res);
-
- if (!res.connected) {
-
- that.setData({
- devLink: false,
- })
- wx.showToast({
- title: '蓝牙已断开',
- })
-
-
-
- } else {
- that.setData({
- devLink: true,
- })
- }
- })
- },
- onShow() {
- let that = this;
-
- this.setData({
- selmac: app.globalData.selmac
- })
- console.log("selmac", that.data.selmac);
-
- wx.createBLEConnection({
- deviceId: app.globalData.selmac.deviceId,
- success: (res) => {
- console.log("连接成功", res);
- that.setData({
- devLink: true
- })
- },
- fail: (e) => {
- console.log("连接失败", e);
- }
- })
-
-
-
- setTimeout(() => {
-
- wx.getBLEDeviceServices({
- deviceId: app.globalData.selmac.deviceId,
- success: (res) => {
- console.log("获取蓝牙低功耗设备所有服务成功", res);
- let serviceId = res.services[0].uuid;
-
- app.globalData.serviceId = serviceId;
-
-
- wx.getBLEDeviceCharacteristics({
- deviceId: app.globalData.selmac.deviceId,
- serviceId: serviceId,
- success: (res) => {
- console.log("获取蓝牙低功耗设备某个服务中所有特征", res);
- let characteristicId = res.characteristics[0].uuid;
-
- app.globalData.characteristicId = characteristicId;
- },
- fail: (e) => {
- console.log("获取蓝牙低功耗设备某个服务中所有特征", e);
- }
- })
- },
- fail: (e) => {
- console.log("获取蓝牙低功耗设备所有服务失败", e);
- }
- })
-
-
-
-
- }, 1500)
-
- },
- connect() {
- let that = this;
- if (this.data.selmac.deviceId) {
- console.log("可以初始化");
- wx.openBluetoothAdapter({
- mode: 'peripheral',
- })
-
- wx.createBLEConnection({
- deviceId: that.data.selmac.deviceId,
- success: (res) => {
- console.log("建立连接成功", res);
- },
- fail: (e) => {
- console.log("建立连接失败", e);
- wx.showToast({
- title: '重新连接失败',
- icon: 'error'
- })
- }
- })
- }
- },
- disconnect() {
- let that = this;
- if (this.data.selmac.deviceId) {
-
- wx.closeBLEConnection({
- deviceId: that.data.selmac.deviceId,
- success: (res) => {
- console.log("断开连接成功", res);
- },
- fail: (e) => {
- console.log("断开连接失败", e);
- }
- })
- }
- },
- onClearInput() {
- this.setData({
- callBack: '',
- sendData: '',
- });
-
- },
- startwrite() {
-
- let text = this.cleanHexString('hello');
- let buffer = this.stringToCmdBuffer(text);
- let hexData = this.ab2hex(buffer);
- let string = this.hexCharCodeToStr(hexData);
- console.log("十六进制字符串转buffer", buffer);
- console.log("buffer转hexData", hexData);
- console.log("hexData转string", string);
- this.sendMessage(text);
-
- },
- bindTextAreaBlur(e) {
- this.data.inputValue = e.detail.value;
- console.log(e.detail.value)
- },
-
- onInputSend() {
- console.log("this.data.inputValue", this.data.inputValue);
- if(this.data.inputValue) {
- this.sendMessage(this.data.inputValue, true);
- } else {
- wx.showToast({
- title: '指令不能为空',
- icon: 'error'
- })
- }
- },
-
- sendMessage(event,type) {
- let that = this;
- let sendData = '';
- if(type) {
- console.log("手动输出方式");
- sendData = event
- } else {
- console.log("按钮点击输出方式");
- console.log("event", event.target.dataset);
- sendData = event.target.dataset.command.defaultSendData;
- }
-
- if(!that.data.devLink) {
- wx.showToast({
- title: '蓝牙已断开',
- icon: 'error'
- })
- return
- }
-
- if(!sendData) {
- wx.showToast({
- title: '指令不存在',
- icon: 'error'
- })
- return
- }
- this.setData({
- sendData: sendData
- })
- let buffer = this.stringToCmdBuffer(sendData);
- console.log("发送的指令",buffer);
- wx.writeBLECharacteristicValue({
- deviceId: app.globalData.selmac.deviceId,
- serviceId: app.globalData.selmac.serviceId,
- characteristicId: app.globalData.selmac.characteristicId,
- value: buffer,
- success(res) {
- console.log("write指令发送成功", res)
- that.notify();
- },
- fail(err) {
- console.log("write指令发送失败", err);
- that.setData({
- callBack: err.errMsg
- });
- wx.showToast({
- title: '指令发送失败',
- icon: 'error'
- })
-
- }
- })
- },
- cleanHexString(hexStr) {
-
- let cleanedHex = hexStr.replace(/0x/g, '').replace(/\b0+/g, '');
- return cleanedHex;
- },
-
- notify() {
- let that = this;
- wx.notifyBLECharacteristicValueChange({
- deviceId: app.globalData.selmac.deviceId,
- serviceId: app.globalData.selmac.serviceId,
- characteristicId: app.globalData.selmac.characteristicId,
- success(res) {
- console.log(res)
-
-
- that.listenValueChange()
- },
- fail(err) {
- console.error(err)
- }
- })
- },
-
- listenValueChange() {
- let that = this;
- wx.onBLECharacteristicValueChange(res => {
- console.log(res)
- let resHex = that.ab2hex(res.value)
- console.log("resHex", resHex)
- let result = that.hexCharCodeToStr(resHex)
- console.log("消息变化", String(result))
- that.setData({
- callBack: String(result)
- });
- })
- },
-
- hexCharCodeToStr(hexCharCodeStr) {
- let trimedStr = hexCharCodeStr.trim();
- let rawStr = trimedStr.substr(0, 2).toLowerCase() === "0x" ? trimedStr.substr(2) : trimedStr;
- let len = rawStr.length;
- if (len % 2 !== 0) {
- alert("存在非法字符!");
- return "";
- }
- let curCharCode;
- let resultStr = [];
- for (let i = 0; i < len; i = i + 2) {
- curCharCode = parseInt(rawStr.substr(i, 2), 16);
- resultStr.push(String.fromCharCode(curCharCode));
- }
- return resultStr.join("");
- },
-
- stringToCmdBuffer(msg) {
- const buffer = new ArrayBuffer(msg.length)
- const dataView = new DataView(buffer)
-
-
- for (let i = 0; i < msg.length; i++) {
- dataView.setUint8(i, msg.charAt(i).charCodeAt())
- };
-
- return buffer
-
- },
-
- ab2hex(buffer) {
- const hexArr = Array.prototype.map.call(
- new Uint8Array(buffer),
- function (bit) {
- return ('00' + bit.toString(16)).slice(-2)
- }
- )
- return hexArr.join('')
- }
-
- })
|