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();
    },
    lanya1: function() {
        var e = this;
        wx.openBluetoothAdapter({
            success: function(t) {
                console.log("初始化蓝牙适配器:" + JSON.stringify(t)), e.lanya2();
            },
            fail: function(e) {
                wx.showModal({
                    title: "提示",
                    content: "请检查手机蓝牙是否打开",
                    success: function(e) {}
                });
            }
        });
    },
    lanya2: function() {
        var e = this;
        wx.getBluetoothAdapterState({
            success: function(t) {
                console.log("本机蓝牙适配器状态/" + JSON.stringify(t.errMsg)), e.lanya3();
            }
        });
    },
    lanya3: function() {
        var e = this;
        wx.startBluetoothDevicesDiscovery({
            success: function(t) {
                console.log("搜索33设备:" + JSON.stringify(t)), setTimeout(function() {
                    e.lanya4();
                }, 1500);
            }
        });
    },
    lanya4: function() {
        var e = this;
        wx.getBluetoothDevices({
            success: function(t) {
                console.log("444:" + JSON.stringify(t.devices));
                for (var c = t.devices, a = new Array(), i = 0, s = 0; s < c.length; s++) {
                    var r = c[s];
                    "未知设备" != r.name && (-1 == r.name.indexOf("MT") && -1 == r.name.indexOf("Wa") || (a[i] = r, 
                    i++));
                }
                e.setData({
                    devices: a
                }), 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(e) {
        var t = this;
        wx.showLoading({
            title: "连接中"
        }), t.connectTOBlue(e.currentTarget.id);
    },
    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(e) {
        var t = this, c = new Uint8Array(e.match(/[\da-f]{2}/gi).map(function(e) {
            return parseInt(e, 16);
        }));
        console.log(c);
        var a = c.buffer;
        console.log(a), console.log("deviceId", t.data.connectedDeviceId), console.log("writeServicweId", t.data.writeServicweId), 
        console.log("writeCharacteristicsId", t.data.writeCharacteristicsId), wx.writeBLECharacteristicValue({
            deviceId: t.data.connectedDeviceId,
            serviceId: t.data.writeServicweId,
            characteristicId: t.data.writeCharacteristicsId,
            value: a,
            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 a = t.buf2hex(c.value).toUpperCase();
            e.toUpperCase() != a && console.log(a);
        });
    },
    buf2hex: function(e) {
        return Array.prototype.map.call(new Uint8Array(e), function(e) {
            return ("00" + e.toString(16)).slice(-2);
        }).join("");
    },
    connectTOBlue: function(t) {
        var c = this, a = t;
        wx.getStorageSync("schoolName"), wx.createBLEConnection({
            deviceId: a,
            success: function(e) {
                console.log(e.errMsg), c.setData({
                    connectedDeviceId: a
                }), wx.setStorageSync("device_id", a), c.lanya6();
            },
            fail: function() {
                console.log("调用失败"), e.showToast("蓝牙连接失败或被占用", "none");
            },
            complete: function() {
                console.log("调用结束");
            }
        });
    },
    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,
                    msg: JSON.stringify(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 c = 0; c < t.characteristics.length; c++) t.characteristics[c].properties.notify && (console.log("11111111", e.data.services[0].uuid), 
                console.log("22222222222222222", t.characteristics[c].uuid), e.setData({
                    notifyServicweId: e.data.services[0].uuid,
                    notifyCharacteristicsId: t.characteristics[c].uuid
                })), t.characteristics[c].properties.write ? e.setData({
                    writeServicweId: e.data.services[0].uuid,
                    writeCharacteristicsId: t.characteristics[c].uuid
                }) : t.characteristics[c].properties.read && e.setData({
                    readServicweId: e.data.services[0].uuid,
                    readCharacteristicsId: t.characteristics[c].uuid
                });
                console.log("device getBLEDeviceCharacteristics:", t.characteristics), e.setData({
                    msg: JSON.stringify(t.characteristics)
                });
            },
            fail: function() {
                console.log("fail");
            },
            complete: function() {
                console.log("complete"), e.lanya9();
            }
        });
    },
    lanya9: function() {
        var t = this;
        wx.notifyBLECharacteristicValueChange({
            state: !0,
            deviceId: t.data.connectedDeviceId,
            serviceId: t.data.notifyServicweId,
            characteristicId: t.data.notifyCharacteristicsId,
            success: function(c) {
                console.log("notifyBLECharacteristicValueChange success", c.errMsg), e.globalData.connectedDeviceId = t.data.connectedDeviceId, 
                e.globalData.writeServicweId = t.data.writeServicweId, e.globalData.writeCharacteristicsId = t.data.writeCharacteristicsId, 
                e.globalData.readServicweId = t.data.readServicweId, e.globalData.readCharacteristicsId = t.data.readCharacteristicsId, 
                e.globalData.notifyServicweId = t.data.notifyServicweId, e.globalData.notifyCharacteristicsId = t.data.notifyCharacteristicsId, 
                wx.hideLoading(), wx.switchTab({
                    url: "../index/index"
                });
            },
            fail: function() {
                console.log("失败");
            }
        });
    }
});