var e = getApp();

Page({
    data: {
        qrCode: "",
        status: "",
        sousuo: "",
        connectedDeviceId: "",
        services: "",
        characteristics: "",
        writeServicweId: "",
        writeCharacteristicsId: "",
        readServicweId: "",
        readCharacteristicsId: "",
        notifyServicweId: "",
        notifyCharacteristicsId: "",
        inputValue: "",
        characteristics1: ""
    },
    onPullDownRefresh: function() {
        wx.showNavigationBarLoading(), this.lanya1();
    },
    onLoad: function() {
        wx.closeBluetoothAdapter({
            success: function(e) {
                console.log(e);
            }
        }), this.lanya1();
    },
    scan: function() {
        var e = this;
        wx.scanCode({
            onlyFromCamera: !0,
            success: function(t) {
                var a = t.result;
                e.setData({
                    qrCode: a
                });
            }
        });
    },
    lanya1: function() {
        var e = this;
        wx.openBluetoothAdapter({
            success: function(t) {
                console.log("初始化蓝牙适配器:" + JSON.stringify(t)), e.setData({
                    msg: "初始化蓝牙适配器成功！" + JSON.stringify(t)
                }), wx.onBluetoothAdapterStateChange(function(t) {
                    e.setData({
                        sousuo: t.discovering ? "在搜索。" : "未搜索。",
                        status: t.available ? "可用。" : "不可用。"
                    });
                }), e.lanya2();
            },
            fail: function(e) {
                wx.showModal({
                    title: "提示",
                    content: "请检查手机蓝牙是否打开",
                    success: function(e) {}
                });
            }
        });
    },
    lanya2: function() {
        var e = this;
        wx.getBluetoothAdapterState({
            success: function(t) {
                e.setData({
                    msg: "本机蓝牙适配器状态/" + JSON.stringify(t.errMsg),
                    sousuo: t.discovering ? "在搜索。" : "未搜索。",
                    status: t.available ? "可用。" : "不可用。"
                }), e.lanya3();
            }
        });
    },
    lanya3: function() {
        var e = this;
        wx.startBluetoothDevicesDiscovery({
            success: function(t) {
                console.log("搜索33设备:" + JSON.stringify(t)), e.setData({
                    msg: "搜索设备" + JSON.stringify(t)
                }), setTimeout(function() {
                    e.lanya4();
                }, 1e3);
            }
        });
    },
    lanya4: function() {
        var e = this;
        wx.getBluetoothDevices({
            success: function(t) {
                console.log("444:" + JSON.stringify(t.devices));
                for (var a = t.devices, c = new Array(), s = 0, i = 0; i < a.length; i++) {
                    var o = a[i];
                    "未知设备" != o.name && (-1 == o.name.indexOf("MT") && -1 == o.name.indexOf("Wa") || (c[s] = o, 
                    s++));
                }
                e.setData({
                    devices: c
                }), wx.hideNavigationBarLoading(), wx.stopPullDownRefresh();
            }
        });
    },
    lanya5: function() {
        var e = this;
        wx.stopBluetoothDevicesDiscovery({
            success: function(t) {
                e.setData({
                    msg: "停止搜索周边设备/" + JSON.stringify(t.errMsg),
                    sousuo: t.discovering ? "在搜索。" : "未搜索。",
                    status: t.available ? "可用。" : "不可用。"
                });
            }
        });
    },
    connectTO: function(t) {
        var a = this;
        "" == a.data.qrCode ? e.showToast("先扫码", "none") : wx.getSystemInfo({
            success: function(c) {
                console.log(c.platform), wx.request({
                    url: e.url + "/applet/saveMacOrUuid",
                    data: {
                        deviceId: t.currentTarget.id,
                        qrCode: a.data.qrCode,
                        platform: c.platform
                    },
                    success: function(e) {
                        200 == e.data.statusCode && a.connectTOBlue(t.currentTarget.id);
                    }
                });
            }
        });
    },
    connectTOBlue: function(t) {
        var a = this, c = t;
        wx.createBLEConnection({
            deviceId: c,
            success: function(e) {
                console.log(e.errMsg), a.setData({
                    connectedDeviceId: c
                }), a.lanya6();
            },
            fail: function() {
                console.log("调用失败"), e.showToast("蓝牙连接失败或被占用", "none");
            },
            complete: function() {
                console.log("调用结束");
            }
        });
    },
    lanya0: function() {
        var e = this;
        wx.closeBLEConnection({
            deviceId: e.data.connectedDeviceId,
            success: function(t) {
                console.log(t.errMsg), e.setData({
                    connectedDeviceId: ""
                });
            }
        });
    },
    inputTextchange: function(e) {
        this.setData({
            inputValue: e.detail.value
        });
    },
    lanya8: function(t) {
        var a = this, c = new Uint8Array(t.match(/[\da-f]{2}/gi).map(function(e) {
            return parseInt(e, 16);
        }));
        console.log(c);
        var s = c.buffer;
        console.log(s), console.log("deviceId", a.data.connectedDeviceId), console.log("writeServicweId", a.data.writeServicweId), 
        console.log("writeCharacteristicsId", a.data.writeCharacteristicsId), wx.writeBLECharacteristicValue({
            deviceId: a.data.connectedDeviceId,
            serviceId: a.data.writeServicweId,
            characteristicId: a.data.writeCharacteristicsId,
            value: s,
            success: function(e) {
                console.log("writeBLECharacteristicValue success", e.errMsg);
            }
        }), wx.onBLECharacteristicValueChange(function(c) {
            console.log("characteristic.characteristicId:" + c.characteristicId), console.log("characteristic.value:" + JSON.stringify(c.value));
            var s = a.buf2hex(c.value).toUpperCase();
            t.toUpperCase() != s && (console.log(s), console.log("value.length:" + s.length), 
            wx.hideLoading(), "FDFD09B800000155" == s ? e.showToast("设置成功", "success") : e.showToast("设置失败", "success"));
        });
    },
    buf2hex: function(e) {
        return Array.prototype.map.call(new Uint8Array(e), function(e) {
            return ("00" + e.toString(16)).slice(-2);
        }).join("");
    },
    lanya6: function() {
        var e = this;
        wx.getBLEDeviceServices({
            deviceId: e.data.connectedDeviceId,
            success: function(t) {
                console.log("device services:", JSON.stringify(t.services)), e.setData({
                    services: t.services
                }), e.lanya7();
            }
        });
    },
    lanya7: function() {
        var e = this;
        wx.getBLEDeviceCharacteristics({
            deviceId: e.data.connectedDeviceId,
            serviceId: e.data.services[0].uuid,
            success: function(t) {
                for (var a = 0; a < t.characteristics.length; a++) t.characteristics[a].properties.notify && (console.log("11111111", e.data.services[0].uuid), 
                console.log("22222222222222222", t.characteristics[a].uuid), e.setData({
                    notifyServicweId: e.data.services[0].uuid,
                    notifyCharacteristicsId: t.characteristics[a].uuid
                })), t.characteristics[a].properties.write ? e.setData({
                    writeServicweId: e.data.services[0].uuid,
                    writeCharacteristicsId: t.characteristics[a].uuid
                }) : t.characteristics[a].properties.read && e.setData({
                    readServicweId: e.data.services[0].uuid,
                    readCharacteristicsId: t.characteristics[a].uuid
                });
                console.log("device getBLEDeviceCharacteristics:", t.characteristics);
            },
            fail: function() {
                console.log("fail");
            },
            complete: function() {
                console.log("complete"), e.lanya9();
            }
        });
    },
    lanya9: function() {
        var e = this;
        wx.notifyBLECharacteristicValueChange({
            state: !0,
            deviceId: e.data.connectedDeviceId,
            serviceId: e.data.notifyServicweId,
            characteristicId: e.data.notifyCharacteristicsId,
            success: function(t) {
                console.log("notifyBLECharacteristicValueChange success", t.errMsg), e.open();
            },
            fail: function() {
                console.log("失败"), console.log(e.data.notifyServicweId), console.log(e.data.notifyCharacteristicsId);
            }
        });
    },
    open: function() {
        console.log("开");
        var e = this;
        wx.showLoading({
            title: "加载中"
        }), setTimeout(function() {
            wx.hideLoading();
        }, 15e3);
        var t = e.data.qrCode, a = t.substring(t.length - 5, t.length);
        console.log("qrCodeTemp:" + a), a = "0" + a.substring(0, 1) + "0" + a.substring(1, 2) + "0" + a.substring(2, 3) + "0" + a.substring(3, 4) + "0" + a.substring(4, 5), 
        console.log("qrCodeTemp:" + a);
        var c = "FEFE09B8000005" + a;
        e.lanya8(c);
    }
});