|
@@ -19,79 +19,158 @@ Page({
|
|
|
})
|
|
|
}
|
|
|
},
|
|
|
- searchTips: "正在搜索设备,请保持设备开机状态",
|
|
|
+ connectStatus: 0,
|
|
|
+ searchTips: "正在搜索设备,请保持开机状态…",
|
|
|
+ subTips: "确认手机蓝牙已打开",
|
|
|
buttonTips: "正在搜索设备"
|
|
|
+
|
|
|
},
|
|
|
- async connectDeviceTap() {
|
|
|
+ setStatus(bleType) {
|
|
|
+ var searchTips = ""
|
|
|
+ var subTips = ""
|
|
|
+ var buttonTips = ""
|
|
|
+ let name = this.data.connectDevice.name;
|
|
|
+ console.log("搜索状态", bleType)
|
|
|
+ switch (bleType) {
|
|
|
+ case 0:
|
|
|
+ // 搜索中
|
|
|
+ searchTips = "正在搜索设备,请保持开机状态…"
|
|
|
+ subTips = "确认手机蓝牙已打开"
|
|
|
+ buttonTips = "正在搜索设备"
|
|
|
+
|
|
|
+ break;
|
|
|
+ case 1:
|
|
|
+ // 搜索失败
|
|
|
+ searchTips = "未搜索到" + name
|
|
|
+ subTips = "请检查设备是否被其他手机连接,或在手机蓝牙忽略掉原来的蓝牙连接重新连接。"
|
|
|
+ buttonTips = "重新搜索"
|
|
|
+
|
|
|
+ break;
|
|
|
+ case 2:
|
|
|
+ // 搜索到
|
|
|
+ searchTips = "搜索到" + name
|
|
|
+ subTips = ""
|
|
|
+ buttonTips = "连接"
|
|
|
+ break;
|
|
|
+ case 3:
|
|
|
+ // 连接成功
|
|
|
+ searchTips = "搜索到" + name
|
|
|
+ subTips = ""
|
|
|
+ buttonTips = "连接成功"
|
|
|
+ break;
|
|
|
+ case 4:
|
|
|
+ // 连接失败
|
|
|
+ searchTips = "未连接到" + name
|
|
|
+ subTips = "请检查设备是否被其他手机连接,或在手机蓝牙忽略掉原来的蓝牙连接重新连接。"
|
|
|
+ buttonTips = "重新搜索"
|
|
|
+ break;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
this.setData({
|
|
|
- searchTips: "正在搜索设备,请保持设备开机状态",
|
|
|
- buttonTips: "正在搜索设备"
|
|
|
+ searchTips: searchTips,
|
|
|
+ subTips: subTips,
|
|
|
+ buttonTips: buttonTips,
|
|
|
+ connectStatus: bleType
|
|
|
+ })
|
|
|
+ },
|
|
|
+ async connectDeviceTap() {
|
|
|
+ let _this = this
|
|
|
+ console.log("点击搜索状态", this.data.connectStatus)
|
|
|
+
|
|
|
+ switch (this.data.connectStatus) {
|
|
|
+ case 0:
|
|
|
+ // 搜索中
|
|
|
+ break;
|
|
|
+ case 1:
|
|
|
+ case 4:
|
|
|
+ // 搜索失败,点击重新搜索
|
|
|
+ _this.startSearch()
|
|
|
+ break;
|
|
|
+ case 2:
|
|
|
+ // 搜索到
|
|
|
+ btHelper.connect(element, function (data) {
|
|
|
+ // console.log("连接成功", data)
|
|
|
+ toastUtil.show("开始连接设备")
|
|
|
+
|
|
|
+ _this.setStatus(data ? 3 : 4)
|
|
|
+ // 蓝牙模式
|
|
|
+ _this.data.connectDevice.connectType = 1
|
|
|
+ getApp().globalData.device = _this.data.connectDevice
|
|
|
+ setTimeout(() => {
|
|
|
+ wx.reLaunch({
|
|
|
+ url: '/pages/index/index', // 替换为首页的路径
|
|
|
+ });
|
|
|
+ }, 500);
|
|
|
+ })
|
|
|
+ break;
|
|
|
+ case 3:
|
|
|
+ // 连接成功
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }, getConnectedDevices: async function () {
|
|
|
+ let _this = this
|
|
|
+ const connectedDevices = await btHelper.getConnectedDevices()
|
|
|
+ console.log("全部设备", connectedDevices)
|
|
|
+ if (connectedDevices.length > 0) {
|
|
|
+ connectedDevices.forEach(element => {
|
|
|
+ console.log('已连接的蓝牙设备:', element);
|
|
|
+ _this.setStatus(3)
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ }
|
|
|
+ return connectedDevices;
|
|
|
+ }, async startSearch() {
|
|
|
+ this.setStatus(0)
|
|
|
+ let _this = this
|
|
|
+ btHelper.search(async function () {
|
|
|
+ const connectedDevices = await _this.getConnectedDevices()
|
|
|
+ if (connectedDevices.length == 0) {
|
|
|
+ _this.setStatus(1)
|
|
|
+ }
|
|
|
})
|
|
|
- await btHelper.search()
|
|
|
- await this.getConnectedDevices()
|
|
|
+
|
|
|
+ },
|
|
|
+
|
|
|
+ didFindDevice() {
|
|
|
let _this = this
|
|
|
btHelper.findDevices(function (devices) {
|
|
|
devices.forEach(element => {
|
|
|
console.log('发现设备:', element.deviceId, element.uuid, element.mac);
|
|
|
|
|
|
let deviceId = element.deviceId ?? ""
|
|
|
+ // todo
|
|
|
if (deviceId.includes("D8:24:07:89:31") ||
|
|
|
element.deviceId.includes("F5:A5:43:70:C8:F1") ||
|
|
|
element.deviceId.includes("D1:4D:DA:76:42:50")
|
|
|
) {
|
|
|
- toastUtil.show("开始连接设备")
|
|
|
btHelper.stopSearch()
|
|
|
- _this.setData({
|
|
|
- searchTips: "正在连接设备,请保持设备开机状态",
|
|
|
- buttonTips: "正在连接设备"
|
|
|
- })
|
|
|
- btHelper.connect(element, function (data) {
|
|
|
- // console.log("连接成功", data)
|
|
|
- _this.setData({
|
|
|
- searchTips: data ? "连接成功" : "连接失败",
|
|
|
- buttonTips: data ? "连接成功" : "连接失败"
|
|
|
- })
|
|
|
- })
|
|
|
- return
|
|
|
+ _this.setStatus(2)
|
|
|
}
|
|
|
});
|
|
|
})
|
|
|
- }, getConnectedDevices: async function () {
|
|
|
- try {
|
|
|
- const connectedDevices = await btHelper.getConnectedDevices()
|
|
|
- console.log("全部设备", connectedDevices)
|
|
|
- if (connectedDevices) {
|
|
|
- connectedDevices.forEach(element => {
|
|
|
- console.log('已连接的蓝牙设备:', element);
|
|
|
-
|
|
|
- // if (element.deviceId.includes("D8:24:07:89:31") ||
|
|
|
- // element.deviceId.includes("F5:A5:43:70:C8:F1") ||
|
|
|
- // element.deviceId.includes("D1:4D:DA:76:42:50")
|
|
|
-
|
|
|
- // ) {
|
|
|
- // toastUtil.show("开始连接设备")
|
|
|
- // btHelper.stopSearch()
|
|
|
- // btHelper.connect(element, function (data) {
|
|
|
- // // console.log("连接成功", data)
|
|
|
- // })
|
|
|
- // }
|
|
|
- return;
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
- // console.log('已连接的蓝牙设备:', connectedDevices);
|
|
|
- } catch (error) {
|
|
|
- console.error(error);
|
|
|
- }
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
* 生命周期函数--监听页面加载
|
|
|
*/
|
|
|
onLoad(options) {
|
|
|
- btHelper.initBluetooth()
|
|
|
+ let _this = this
|
|
|
+ btHelper.initBluetooth(function (adapterState, hasPermission) {
|
|
|
+ console.log("蓝牙状态", adapterState, hasPermission)
|
|
|
+ if (adapterState && hasPermission) {
|
|
|
+ _this.setStatus(0)
|
|
|
+ _this.startSearch()
|
|
|
+ } else {
|
|
|
+ _this.setStatus(1)
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ _this.didFindDevice()
|
|
|
|
|
|
- let device = options.param;
|
|
|
+ let json = JSON.parse(options.param)
|
|
|
+ console.log(json)
|
|
|
+ let device = json
|
|
|
this.setData({
|
|
|
connectDevice: device
|
|
|
})
|