123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- <script>
- import ble_store from "@/store/bluetooth";
- import Dialog from "../../../../static/vant/dialog/dialog";
- import Toast from "../../../../static/vant/toast/toast";
- let $this;
- export default {
- name: "ConnectionScan",
- created() {
- $this = this;
- },
- methods: {
-
- openScan() {
-
- wx.getSystemInfoAsync({
- success(res) {
-
- if (!res.bluetoothEnabled) {
- Dialog.alert({
- title: '蓝牙功能未开启',
- message: '请打开蓝牙,允许水母星球使用蓝牙服务',
- }).then(() => {
-
- wx.openSystemBluetoothSetting()
- });
- }
-
- else if (!res.locationEnabled) {
-
- if (res.system.indexOf("iOS") !== -1) {
- $this.scanBluetooth();
- } else {
- Dialog.alert({
- title: '位置信息未开启',
- message: '当前无位置访问权限,请在设置中,允许水母星球使用位置服务',
- }).then(() => {
- });
- }
- }
-
- else if (!res.locationAuthorized) {
- Dialog.alert({
- title: '位置权限未开启',
- message: '当前无位置访问权限,请在手机[设置]应用中,允许[微信]使用位置服务',
- }).then(() => {
-
- wx.openAppAuthorizeSetting()
- });
- }
-
- else {
- $this.scanBluetooth();
- }
- },
- });
- },
-
- scanBluetooth() {
-
- wx.scanCode({
-
- onlyFromCamera: true,
-
- scanType: ["barCode", "qrCode"],
- success: (scan) => {
- console.log(scan.result)
- if (scan.result) {
- let url = decodeURIComponent(scan.result);
- let $code = "";
- if (scan.scanType === "QR_CODE") {
-
- $code = url.substring(url.indexOf("AI"));
- } else {
-
- $code = scan.result.toUpperCase();
- }
- ble_store.setters.setDeviceSn($code);
- console.log("扫码得到头环SN码:", $code);
-
- this.openWechatBluetooth();
- }
- },
- fail(err) {
- console.log(err["errMsg"])
- setTimeout(() => {
- Toast.fail({
- message: err["errMsg"],
- });
- }, 3000);
- },
- });
- },
-
- openWechatBluetooth: function () {
-
- wx.getSystemInfo({
- success(res) {
-
- if (res.platform === "ios") {
-
- wx.openBluetoothAdapter({
-
- mode: "central",
- success(res) {
-
- if (res["errMsg"] === "openBluetoothAdapter:ok") {
-
- wx.openBluetoothAdapter({
-
- mode: "peripheral",
- success(res) {
- if (res["errMsg"] === "openBluetoothAdapter:ok") {
- $this.setScanStatus(1);
- }
- },
- fail(err) {
- setTimeout(() => {
- Toast.fail({
- message: $this.$connection.connectionError(err["errCode"]),
- });
- }, 3000);
- },
- });
- }
- },
- fail(err) {
- setTimeout(() => {
- Toast.fail({
- message: $this.$connection.connectionError(err["errCode"]),
- });
- }, 3000);
- },
- });
- } else {
-
- wx.openBluetoothAdapter({
- mode: "peripheral",
- success(res) {
-
- if (res["errMsg"] === "openBluetoothAdapter:ok") {
- $this.setScanStatus(1);
- }
- },
- fail(err) {
- setTimeout(() => {
- Toast.fail({
- message: err["errMsg"],
- });
- }, 3000);
- },
- });
- }
- },
- });
- },
-
- setScanStatus($status = 0) {
- $this.$emit("scanStatus", $status);
- },
- },
- };
- </script>
- <template>
- <div>
- <van-row>
- <van-col span="11" class="text-gray text-sm left" offset="1">
- <view>1.打开手机蓝牙和位置信息</view>
- <view>2.长按脑机侧面按钮启动脑机</view>
- <view>3.点击扫码开始连接</view>
- </van-col>
- <van-col span="8" offset="2">
- <img src="https://img.shuimuai.com/lanyashuimu.png" class="connect_img" alt=""/>
- </van-col>
- </van-row>
- <button class="cu-btn lg cu-btn-primary text-white text-center scan_button margin-tb" @click="openScan">扫码连接脑机</button>
- <van-toast id="van-toast"/>
- <van-dialog id="van-dialog"/>
- </div>
- </template>
- <style scoped></style>
|