钟铮锁App web部分 需要打包放进对应的安卓项目 生成apk 才能正常使用功能
he wei
2024-12-20 552a8341d159e463e2946074e21deddb185c90ee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import { BluetoothLE } from "@ionic-native/bluetooth-le";
import { ref, onMounted } from "vue";
 
import { Buffer } from "buffer";
 
import CRC from "crc";
 
/**
 * 要求开锁前先请求后台鉴权
 * 有权限后 先连蓝牙 再读数据 计算密钥 写数据开锁 开锁成功或失败 都断开连接
 */
// MAC 为蓝牙设备的MAC地址  :分隔的六个字节 全部要大小
export default (MAC) => {
  MAC = MAC.toUpperCase();
  const service = ref("00FF");
  const characteristic = ref("FF01");
 
  // 请求蓝牙权限
  function _initBL() {
    BluetoothLE.hasPermission().then(
      (hasPermission) => {
        console.log("hasPermission", hasPermission);
        if (!hasPermission.isEnabled) {
          BluetoothLE.requestPermission().then(
            () => {
              console.log("蓝牙权限已授予");
              initBL();
            },
            (error) => {
              console.log("蓝牙权限未授予", error);
            }
          );
        } else {
          console.log("蓝牙权限已授予");
          initBL();
        }
      },
      (error) => {
        console.log("蓝牙权限未授予", error);
      }
    );
  }
 
  // 计算开锁密钥  data 从蓝牙读取到的数据 数组 8个字节
  function calcKey(data) {
    // 计算出crc 校验码  结果要低字节在前
    // let res = CRC.crc16modbus(data).toString(16).split("");
    // let _res = ["0X" + (res[2] + res[3]), "0X" + (res[0] + res[1])];
 
    let res = CRC.crc16modbus(data);
    let _res = [res & 0xff, (res >> 8) & 0xff];
    // console.log("res", res, CRC.crc16modbus(data), _res, "=============");
    let __res = [..._res, data[6], data[7]];
    // 遇到0 变成128
    // 然后再前两字节相乘 结果高字节放前  后两字节相乘 高字节在前
    // console.log("__res", __res, "=============");
    __res = __res.map((v) => (v > 0 ? v : 128));
    let a = __res[0] * __res[1];
    let b = __res[2] * __res[3];
    // console.log("a, b", a, b, "=============");
 
    return [a >> 8, a & 0xff, b >> 8, b & 0xff];
  }
 
  // 检查蓝牙是否可用
  async function checkBluetoothEnabled() {
    try {
      const isEnabled = await BluetoothLE.isEnabled();
      console.log("蓝牙是否可用", isEnabled, "=============");
 
      if (!isEnabled.isEnabled) {
        console.log("蓝牙不可用 请求开启", "=============");
      }
      console.log("return true", "=============");
 
      return true;
    } catch (error) {
      console.log("蓝牙不可用", error);
      return false;
    }
  }
 
  // 初始化蓝牙
  async function initBL(params) {
    const isBluetoothEnabled = await checkBluetoothEnabled();
    if (!isBluetoothEnabled) {
      console.log("蓝牙不可用,无法初始化");
      return;
    }
    // 初始化蓝牙
    BluetoothLE.initialize().subscribe(
      (result) => {
        console.log("蓝牙初始化成功", result);
 
        // scan();
      },
      (error) => {
        console.log("蓝牙初始化失败", error);
      }
    );
  }
 
  const connect = async () => {
    return BluetoothLE.connect(
      {
        address: MAC,
      },
      (device) => {
        console.log("连接成功", device);
      },
      (error) => {
        console.log("连接失败", error);
      }
    ).subscribe({
      next: (device) => {
        console.log("连接 next", device);
        if (device.status === "connected") {
          // connected.value = true;
          // getServer();
        }
      },
      error: (error) => {
        console.error("连接失败2", error);
      },
      complete: () => {
        console.log("连接完成");
      },
    });
  };
 
  const disconnect = async () => {
    try {
      await BluetoothLE.disconnect({
        address: MAC,
      });
      console.log("断开成功");
      // connected.value = false;
    } catch (error) {
      console.log("断开失败", error);
    }
  };
 
  const scan = async () => {
    try {
      await BluetoothLE.startScan().subscribe({
        next: (device) => {
          console.log("发现设备", device);
          if (device.address.toUperCase() === MAC) {
            BluetoothLE.stopScan();
            connect();
          }
        },
        error: (error) => {
          console.error("扫描设备失败", error);
        },
        complete: () => {
          console.log("扫描设备完成");
        },
      });
    } catch (error) {
      console.log("扫描失败", error);
    }
  };
 
  // 发现服务和 发现特征
  function getServer() {
    console.log("getServer", "发现服务", "=============");
 
    BluetoothLE.discover({
      address: MAC,
    })
      .then((res) => {
        console.log("res", res, "=============");
 
        const { services } = res;
        console.log("发现服务", services, JSON.stringify(services));
      })
      .catch((error) => {
        console.log("发现服务失败", error);
      });
  }
 
  // 读取数据
  async function read() {
    console.log("read", service.value, characteristic.value, "=============");
    return BluetoothLE.read({
      address: MAC,
      service: service.value,
      characteristic: characteristic.value,
    })
      .then((data) => {
        console.log("读取数据成功", data);
        let str = data.value;
 
        let bytes = BluetoothLE.encodedStringToBytes(data.value);
        let readStr = Array.from(bytes)
          .map((v) => v.toString(16))
          .join(" ");
        let res = calcKey(Array.from(bytes));
        key.value = res;
        console.log(
          "str:",
          str,
          "=======readStr:",
          readStr,
          "=======bytes:",
          bytes,
          "=======res:",
          res,
          "============="
        );
      })
      .catch((error) => {
        console.log("读取数据失败", error);
      });
  }
 
  // 写入数据
  function write() {
    console.log(
      "write",
      service.value,
      characteristic.value,
      "=============",
      key.value
    );
 
   return BluetoothLE.write({
      address: MAC,
      service: service.value,
      characteristic: characteristic.value,
      // value: new Uint8Array([0xff, 0x05, 0x00, 0x01, 0xff, 0x00, 0xc8, 0x24]),
      value: BluetoothLE.bytesToEncodedString(new Uint8Array(key.value)),
    })
      .then(() => {
        console.log("写入数据成功");
      })
      .catch((error) => {
        console.log("写入数据失败", error);
      });
  }
 
  function close() {
    BluetoothLE.close({
      address: MAC,
    });
  }
 
  async function open() {
    // TODO 鉴权
 
    let connected = await connect();
    console.log('connected', connected, '=============');
    await read();
    let writeRes = await write();
    console.log('writeRes', writeRes, '=============');
    disconnect();
    close();
  }
 
  onMounted(() => {
    _initBL();
  });
 
  return { read, write, close, scan, connect, disconnect, open };
};