|
@@ -40,7 +40,6 @@
|
|
|
</view>
|
|
|
</view>
|
|
|
<view style="flex: 1" />
|
|
|
-
|
|
|
<view
|
|
|
class="but"
|
|
|
hover-class="navigator-hover"
|
|
@@ -68,19 +67,13 @@ export default {
|
|
|
return {
|
|
|
beSearching: false, //搜索状态
|
|
|
connected: "", //连接状态
|
|
|
- device: {}, //连接的蓝牙设备
|
|
|
- devices: [], //蓝牙设备表
|
|
|
- serviceid: 0,
|
|
|
- services: [], //蓝牙设备服务列表
|
|
|
- characteristics: [], //特征列表
|
|
|
- characteristic: {
|
|
|
- uuid: null,
|
|
|
- }, //选中的特征
|
|
|
-
|
|
|
empty: true,
|
|
|
+ services: [],
|
|
|
+ devices: [],
|
|
|
};
|
|
|
},
|
|
|
onLoad(options) {
|
|
|
+ this.services = JSON.parse(options.services);
|
|
|
this.startBluetooth();
|
|
|
this.$refs.List.setHeight();
|
|
|
},
|
|
@@ -97,6 +90,7 @@ export default {
|
|
|
that.empty = devices.length == 0;
|
|
|
});
|
|
|
uni.startBluetoothDevicesDiscovery({
|
|
|
+ services: this.services,
|
|
|
success(res) {
|
|
|
that.beSearching = true;
|
|
|
that.$refs.List.setHeight();
|
|
@@ -127,6 +121,7 @@ export default {
|
|
|
that.connected = item.deviceId;
|
|
|
uni.createBLEConnection({
|
|
|
deviceId: item.deviceId,
|
|
|
+ services: that.services || [],
|
|
|
success(res) {
|
|
|
if (res.errCode == 0) {
|
|
|
uni.getBLEDeviceServices({
|
|
@@ -157,7 +152,7 @@ export default {
|
|
|
that.closeBLEConnection();
|
|
|
} else {
|
|
|
device.characteristic = res.characteristics[0];
|
|
|
- const { write, read } =
|
|
|
+ const { write, notify } =
|
|
|
device.characteristic.properties;
|
|
|
return uni.showModal({
|
|
|
content: `${
|
|
@@ -174,7 +169,7 @@ export default {
|
|
|
}
|
|
|
},
|
|
|
});
|
|
|
- if (write && read) {
|
|
|
+ if (write && notify) {
|
|
|
uni.showModal({
|
|
|
content: `${
|
|
|
item.name || item.deviceId
|
|
@@ -192,7 +187,7 @@ export default {
|
|
|
});
|
|
|
} else {
|
|
|
uni.showModal({
|
|
|
- content: "设备未开启read或write权限,已断开连接",
|
|
|
+ content: "设备未开启notify或write权限,已断开连接",
|
|
|
showCancel: false,
|
|
|
confirmText: "确认",
|
|
|
});
|
|
@@ -223,84 +218,16 @@ export default {
|
|
|
},
|
|
|
});
|
|
|
},
|
|
|
- /* 发送 */
|
|
|
- send() {
|
|
|
- let msg = JSON.stringify({
|
|
|
- password: "87sjy9",
|
|
|
- msgid: "3c1dfed63972c4441b4f0f1a097f884a",
|
|
|
- ts: 1695354434598,
|
|
|
- }),
|
|
|
- that = this;
|
|
|
- const buffer = new ArrayBuffer(msg.length);
|
|
|
- const dataView = new DataView(buffer);
|
|
|
- for (var i = 0; i < msg.length; i++) {
|
|
|
- dataView.setUint8(i, msg.charAt(i).charCodeAt());
|
|
|
- }
|
|
|
- uni.writeBLECharacteristicValue({
|
|
|
- deviceId: that.device.deviceId,
|
|
|
- serviceId: that.serviceid,
|
|
|
- characteristicId: that.characteristic.uuid,
|
|
|
- value: buffer,
|
|
|
- success(res) {
|
|
|
- console.log("发送信息", res);
|
|
|
- },
|
|
|
- fail(err) {
|
|
|
- console.error("发送信息失败", err);
|
|
|
- },
|
|
|
- });
|
|
|
- },
|
|
|
-
|
|
|
- /* 开始监听 */
|
|
|
- startNotice() {
|
|
|
+ /* 关闭连接 */
|
|
|
+ closeBLEConnection() {
|
|
|
let that = this;
|
|
|
- uni.notifyBLECharacteristicValueChange({
|
|
|
- deviceId: that.device.deviceId,
|
|
|
- serviceId: that.serviceid,
|
|
|
- characteristicId: that.characteristic.uuid,
|
|
|
- state: true,
|
|
|
- success(res) {
|
|
|
- uni.onBLECharacteristicValueChange((res) => {
|
|
|
- // 结果
|
|
|
- console.log("结果", res);
|
|
|
- // 结果里有个value值,该值为 ArrayBuffer 类型,所以在控制台无法用肉眼观察到,必须将该值转换为16进制
|
|
|
- let resHex = that.ab2hex(res.value);
|
|
|
- console.log("16进制", resHex);
|
|
|
- // 最后将16进制转换为,就能看到对应的结果
|
|
|
- let result = that.hexCharCodeToStr(resHex);
|
|
|
- console.log("ascii码", result);
|
|
|
- });
|
|
|
- },
|
|
|
- });
|
|
|
- },
|
|
|
- ab2hex(buffer) {
|
|
|
- const hexArr = Array.prototype.map.call(
|
|
|
- new Uint8Array(buffer),
|
|
|
- function (bit) {
|
|
|
- return ("00" + bit.toString(16)).slice(-2);
|
|
|
- }
|
|
|
- );
|
|
|
- return hexArr.join("");
|
|
|
- },
|
|
|
- hexCharCodeToStr(hexCharCodeStr) {
|
|
|
- var trimedStr = hexCharCodeStr.trim();
|
|
|
- var rawStr =
|
|
|
- trimedStr.substr(0, 2).toLowerCase() === "0x"
|
|
|
- ? trimedStr.substr(2)
|
|
|
- : trimedStr;
|
|
|
- var len = rawStr.length;
|
|
|
- if (len % 2 !== 0) {
|
|
|
- alert("存在非法字符!");
|
|
|
- return "";
|
|
|
- }
|
|
|
- var curCharCode;
|
|
|
- var resultStr = [];
|
|
|
- for (var i = 0; i < len; i = i + 2) {
|
|
|
- curCharCode = parseInt(rawStr.substr(i, 2), 16);
|
|
|
- resultStr.push(String.fromCharCode(curCharCode));
|
|
|
- }
|
|
|
- return resultStr.join("");
|
|
|
+ if (this.connected)
|
|
|
+ uni.closeBLEConnection({
|
|
|
+ deviceId: that.connected,
|
|
|
+ success(res) {},
|
|
|
+ });
|
|
|
+ this.connected = "";
|
|
|
},
|
|
|
-
|
|
|
handleFail(fail) {
|
|
|
const codes = {
|
|
|
0: "ok",
|
|
@@ -328,16 +255,6 @@ export default {
|
|
|
});
|
|
|
this.closeBLEConnection();
|
|
|
},
|
|
|
- /* 关闭连接 */
|
|
|
- closeBLEConnection() {
|
|
|
- let that = this;
|
|
|
- if (this.connected)
|
|
|
- uni.closeBLEConnection({
|
|
|
- deviceId: that.connected,
|
|
|
- success(res) {},
|
|
|
- });
|
|
|
- this.connected = "";
|
|
|
- },
|
|
|
getRSSIText: function (rssi) {
|
|
|
let text = "";
|
|
|
if (rssi >= -70) {
|