浏览代码

5/31合并完成

Luhan1997 4 年之前
父节点
当前提交
bbb0262fe0
共有 3 个文件被更改,包括 50 次插入25 次删除
  1. 16 25
      src/components/device/device.vue
  2. 5 0
      src/pages/test/index.vue
  3. 29 0
      src/utils/bluetooth.js

+ 16 - 25
src/components/device/device.vue

@@ -220,34 +220,25 @@ export default {
             // 判断新的标识值
             $this.code = $code[1].toUpperCase();
             console.log("头环码", $code);
-            //打开蓝牙设备
-            wx.getSystemInfo({
+            //打开蓝牙设备 
+            
+
+            wx.openBluetoothAdapter({
+              mode: "peripheral",
               success(res) {
-                console.log("获取系统信息:", res);
-                if (res.platform == "ios") {
-                } else {
-                  wx.openBluetoothAdapter({
-                    success(res) {
-                      console.log("初始化蓝牙模块res信息:", res);
-                      //判断已经打开连接了
-                      if (res["errMsg"] == "openBluetoothAdapter:ok") {
-                        $this.startBluetoothDevicesDiscovery();
-                      }
-                    },
-                    fail(err) {
-                      if (err["errCode"] == 10001) {
-                        Toast.fail({
-                          message: "请打开蓝牙和位置信息!",
-                        });
-                      } else {
-                        Toast.fail({
-                          message: "蓝牙连接失败",
-                        });
-                      }
-                    },
-                  });
+                //判断已经打开连接了
+                if (res["errMsg"] == "openBluetoothAdapter:ok") {
+                  $this.startBluetoothDevicesDiscovery();
                 }
               },
+              fail(err) {
+                let $msg = bluetooth.GetopenBluetoothAdapterError(
+                  err["errCode"]
+                );
+                Toast.fail({
+                  message: $msg,
+                });
+              },
             });
 
             // wx.openBluetoothAdapter({

+ 5 - 0
src/pages/test/index.vue

@@ -0,0 +1,5 @@
+<template>
+	<div>
+		<h1>Hello WOrld</h1>
+	</div>
+</template>

+ 29 - 0
src/utils/bluetooth.js

@@ -123,6 +123,8 @@ export default {
     dataView.setUint8(6, "0x0a");
     dataView.setUint8(7, $CheckSum);
     // 向低功耗蓝牙设备特征值中写入二进制数据
+    console.log(buffer);
+    console.log(dataView);
 
     wx.writeBLECharacteristicValue({
       deviceId: deviceId,
@@ -733,4 +735,31 @@ export default {
     }
     return result;
   },
+
+  //根据错误代码返回字符串信息
+  GetopenBluetoothAdapterError(errCode){
+
+    let $errmsg = "";
+    if(errCode == 10000){
+      $errmsg = "未初始化蓝牙适配器"
+    }
+
+    if(errCode == 10001){
+      $errmsg = "当前蓝牙适配器不可用"
+    }
+
+    if(errCode == 10002){
+      $errmsg = "没有找到指定设备"
+    }
+
+    if(errCode == 10003){
+      $errmsg = "连接失败"
+    }
+
+    if(errCode == 10006){
+      $errmsg = "当前连接已断开"
+    }
+
+    return $errmsg;
+  }
 };