国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Vue H5項(xiàng)目,怎么引入uni.webview sdk,調(diào)用uni postMessage實(shí)現(xiàn)手機(jī)藍(lán)牙連接打印功能(uniapp)

這篇具有很好參考價(jià)值的文章主要介紹了Vue H5項(xiàng)目,怎么引入uni.webview sdk,調(diào)用uni postMessage實(shí)現(xiàn)手機(jī)藍(lán)牙連接打印功能(uniapp)。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

前言

目前公司Vue H5項(xiàng)目,用webview打包成APP,現(xiàn)產(chǎn)品提出這樣打包出來的app運(yùn)行較慢,需要用uniapp方式(即使用HBuilder編輯器來打包H5)來打包,那需要的基座就不是安卓的基座而是uniapp的基座,而H5項(xiàng)目實(shí)現(xiàn)手機(jī)掃描功能就需要調(diào)用uniapp的基座的方法。

需求&流程說明

Vue2 開發(fā)的移動(dòng)端項(xiàng)目(H5項(xiàng)目及ipad端項(xiàng)目),需要連接藍(lán)牙設(shè)備打印

需求說明:

1、點(diǎn)擊打印按鈕時(shí),先判斷當(dāng)前設(shè)備是否已連接過藍(lán)牙(即是否存在藍(lán)牙設(shè)備ID)

a、若已連接過:直接調(diào)用打印配置(即:type:bluetoothPrint)
b、若未連接過:

1、先獲取當(dāng)前設(shè)備的所有藍(lán)牙list(即:type:getBluetoothList)

uniappwebview 引入sdk,vue專欄,Vue H5移動(dòng)端項(xiàng)目,vue.js,uni-app,uni.webview,postMessage,藍(lán)牙打印,移動(dòng)端,H5移動(dòng)端
2、選中設(shè)備后調(diào)用藍(lán)牙連接(即:type:connBluetooth)
3、連接成功后存儲(chǔ)已連接的設(shè)備ID(即選中的設(shè)備)

具體步驟

一、Uniapp Webview 源碼

<template>
  <view>
    <web-view :src="src" @message="showMessage"></web-view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      src: 'http://******/', // H5項(xiàng)目地址
      qrCodeWv: null,
      devices: [],
      currDev: null,
      connId: '',
    }
  },
  onReady() {
    // #ifdef APP-PLUS
    let currentWebview = this.$scope.$getAppWebview()
    setTimeout(() => {
      this.wv = currentWebview.children()[0]
      this.qrCodeWv = currentWebview.children()[0]
      this.wv.setStyle({ scalable: true })
    }, 1000)
    // #endif
  },
  methods: {
    showMessage(event) {
      if (event.detail.data && event.detail.data.length > 0) {
        let dataInfo = event.detail.data[0]
        console.log(dataInfo)
        let type = dataInfo.type
        if (type === 'getBluetoothList') {
          this.getBluetoothList()
        }
        if (type === 'connBluetooth') {
          console.log(dataInfo.params)
          let args = dataInfo.params;
          let deviceId = args.deviceId;
          let device = this.devices.find((item) => {
            return item.deviceId == deviceId;
          })
          console.log(device)
          this.connBluetooth(device)
        }
        if (type === 'bluetoothPrint') {
          let args = dataInfo.params;
          let deviceId = args.deviceId;
          let command = args.command;
          let device = this.devices.find((item) => {
            return item.deviceId == deviceId;
          })
          //當(dāng)設(shè)備沒有連接時(shí)需要重新連接設(shè)備
          if (this.connId == '') {
            this.initBluetoothList();
            this.connBluetooth(device);
          }
          let serviceId = this.currDev.services[0].serviceId;
          let characteristicId = this.currDev.services[0].characteristicId;
          this.senBlData(deviceId, serviceId, characteristicId, command);
        }
      }
    },
    // 獲取藍(lán)牙設(shè)備list
    getBluetoothList() {
      this.initBluetoothList();
      const data = JSON.stringify(this.devices)
      console.log('獲取藍(lán)牙設(shè)備list', data)
      this.qrCodeWv.evalJS(`appBluetoothListResult('${data}')`)
    },
    initBluetoothList() {
      this.searchBle();
      setTimeout(() => {
        this.stopFindBule();
      }, 10000)
    },
    // 查找藍(lán)牙設(shè)備
    searchBle() {
      var self = this
      console.log("initBule")
      uni.openBluetoothAdapter({
        success(res) {
          console.log("打開 藍(lán)牙模塊")
          console.log(res)
          self.onDevice()
          uni.getBluetoothAdapterState({
            success: function (res) {
              console.log(res)
              if (res.available) {
                if (res.discovering) {
                  self.stopFindBule()
                }
                //搜索藍(lán)牙
                //開始搜尋附近的藍(lán)牙外圍設(shè)備
                console.log("開始搜尋附近的藍(lán)牙外圍設(shè)備")
                uni.startBluetoothDevicesDiscovery({
                  success(res) {
                    console.log(res)
                  }
                })
              } else {
                console.log('本機(jī)藍(lán)牙不可用')
              }
            },
          })
        }
      })
    },
    onDevice() {
      console.log("監(jiān)聽尋找到新設(shè)備的事件---------------")
      var self = this
      //監(jiān)聽尋找到新設(shè)備的事件
      uni.onBluetoothDeviceFound(function (devices) {
        //獲取在藍(lán)牙模塊生效期間所有已發(fā)現(xiàn)的藍(lán)牙設(shè)備
        console.log('--------------new-----------------------' + JSON.stringify(devices))
        var re = JSON.parse(JSON.stringify(devices))
        let name = re.devices[0].name
        if (name != "未知設(shè)備" && name.length != 0) {
          console.log(name.length)
          let deviceId = re.devices[0].deviceId
          //信號(hào)過濾。大于50
          //如果已經(jīng)存在也不用加入
          if (re.devices[0].RSSI > self.filterRSSI) {
            if (!self.devices.some(v => v.deviceId == deviceId)) {
              self.devices.push({
                name: name,
                deviceId: deviceId,
                services: []
              })
            }
          }
        }
      })
    },
    stopFindBule() {
      console.log("停止搜尋附近的藍(lán)牙外圍設(shè)備---------------")
      uni.stopBluetoothDevicesDiscovery({
        success(res) {
          console.log(res)
        }
      })
    },
    // 連接藍(lán)牙
    connBluetooth(device) {
      this.onConn(device);
    },
    // 連接藍(lán)牙
    onConn(item) {
      var self = this
      console.log(`連接藍(lán)牙---------------${item.deviceId}`)
      let deviceId = item.deviceId
      uni.createBLEConnection({
        deviceId: deviceId,
        complete(res) {
          let result = false;
          if (res.errMsg == "createBLEConnection:ok") {
            plus.nativeUI.toast(`設(shè)備:${item.name} 已連接`, {
              verticalAlign: 'center'
            })
            self.connId = deviceId;
            self.currDev = item,
              setTimeout(function () {
                self.getBLEServices(deviceId)
              }, 2000)
            result = true;
          } else {
            plus.nativeUI.toast(`設(shè)備: ${item.name} 連接失敗。請(qǐng)重試!`, {
              verticalAlign: 'center',
            })
            //切換異常時(shí)釋放掉鏈接
            if (self.connId != '') {
              uni.closeBLEConnection({
                deviceId: self.connId,
                success(res) {
                  console.log(res)
                }
              })
            }
          }
          //連接成功 關(guān)閉搜索
          self.stopFindBule()
          //發(fā)生是否成功結(jié)果
          var data = {};
          data.result = result;
          var data1 = JSON.stringify(data)
          self.wv.evalJS(`appConnBluetoothResult('${data1}')`)
        },
      })

    },
    getBLEServices(_deviceId) {
      var self = this;
      let deviceId = _deviceId
      console.log("獲取藍(lán)牙設(shè)備所有服務(wù)(service)。---------------")
      uni.getBLEDeviceServices({
        // 這里的 deviceId 需要已經(jīng)通過 createBLEConnection 與對(duì)應(yīng)設(shè)備建立鏈接
        deviceId: deviceId,
        complete(res) {
          console.log(res)
          let serviceId = ""
          for (var s = 0; s < res.services.length; s++) {
            console.log(res.services[s].uuid)
            let serviceId = res.services[s].uuid
            uni.getBLEDeviceCharacteristics({
              // 這里的 deviceId 需要已經(jīng)通過 createBLEConnection 與對(duì)應(yīng)設(shè)備建立鏈接
              deviceId: deviceId,
              // 這里的 serviceId 需要在 getBLEDeviceServices 接口中獲取
              serviceId: serviceId,
              success(res) {
                var re = JSON.parse(JSON.stringify(res))

                console.log(`deviceId =[${deviceId}] serviceId = [${serviceId}]`)
                for (var c = 0; c < re.characteristics.length; c++) {
                  if (re.characteristics[c].properties.write == true) {
                    let uuid = re.characteristics[c].uuid
                    console.log(`deviceId =[${deviceId}] serviceId = [${serviceId}] characteristics=[${uuid}]`)
                    for (var index in self.devices) {
                      if (self.devices[index].deviceId == deviceId) {
                        self.devices[index].services.push({
                          serviceId: serviceId,
                          characteristicId: uuid
                        })
                        break
                      }

                    }
                    console.log(JSON.stringify(self.devices))
                  }
                }
              }
            })

          }
        },
        fail(res) {
          console.log(res)
        },
      })

    },
    senBlData(deviceId, serviceId, characteristicId, uint8Array) {
      var uint8Buf = Array.from(uint8Array);

      function split_array(datas, size) {
        var result = {};
        var j = 0
        if (datas.length < size) {
          size = datas.length
        }
        for (var i = 0; i < datas.length; i += size) {
          result[j] = datas.slice(i, i + size)
          j++
        }
        //result[j] = datas
        console.log(result)
        return result
      }
      var sendloop = split_array(uint8Buf, 20);
      // console.log(sendloop.length)
      function realWriteData(sendloop, i) {
        var data = sendloop[i]
        if (typeof (data) == "undefined") {
          return
        }
        //console.log("第【" + i + "】次寫數(shù)據(jù)"+data)
        var buffer = new ArrayBuffer(data.length)
        var dataView = new DataView(buffer)
        for (var j = 0; j < data.length; j++) {
          dataView.setUint8(j, data[j]);
        }
        uni.writeBLECharacteristicValue({
          deviceId,
          serviceId,
          characteristicId,
          value: buffer,
          success(res) {
            console.log('發(fā)送成功', i)
            setTimeout(() => {
              realWriteData(sendloop, i + 1);
            }, 100)
          },
          fail(e) {
            console.log('發(fā)送數(shù)據(jù)失敗')
            console.log(e)
          }
        })
      }
      var i = 0;
      realWriteData(sendloop, i);
    },
  }
}
</script>

二、H5 Vue項(xiàng)目引入js

1、在public新建js文件夾uni.webview.1.5.4.js文件,其源碼地址

https://gitee.com/dcloud/uni-app/raw/dev/dist/uni.webview.1.5.4.js

2、index.html 引入 public/js 下文件

<script src="<%= BASE_URL %>js/uni.webview.1.5.4.js"></script>

3、main.js 定義回調(diào)方法和對(duì)象

// 藍(lán)牙設(shè)備列表
window.appBluetoothListResult = function (val) {
  window.appBluetoothListResultString = val
  window.dispatchEvent(new CustomEvent('bluetoothListResult'))
}

// 藍(lán)牙連接成功或失敗
window.appConnBluetoothResult = function (val) {
  window.appConnBluetoothResultString = val
  window.dispatchEvent(new CustomEvent('connBluetoothResult'))
}

4、Vue掃碼頁(yè)面代碼

1、在mixins文件夾下新建bluetoothMixins.js:代碼如下
export default {
  data() {
    return {
      bluetoothShow: false, // 藍(lán)牙設(shè)備彈窗
      deviceId: '', // 藍(lán)牙設(shè)備ID
      listArr: [] // 獲取所有藍(lán)牙設(shè)備
    }
  },
  created() {
    window.addEventListener('bluetoothListResult', this.handleBluetoothList, false)
    window.addEventListener('connBluetoothResult', this.handleConnBluetoothResult, false)
  },
  beforeDestroy() {
    window.removeEventListener('bluetoothListResult', this.handleBluetoothList)
    window.removeEventListener('connBluetoothResult', this.handleConnBluetoothResult)
  },
  methods: {
    handleConnBluetoothResult() {
      const result = window.appConnBluetoothResultString
      console.log('返回藍(lán)牙是否連接成功', result)
      if (JSON.parse(result).result) {
        console.log('連接成功')
        const deviceId = localStorage.getItem('bluetoothDeviceId')
        localStorage.setItem('bluetoothDeviceId', deviceId || this.deviceId)
        // alert(`${this.deviceId}---設(shè)置值選中的值`)
        // alert(`${deviceId}---設(shè)置值緩存的值`)
        this.bluetoothShow = false
      }
    },
    handleBluetoothList() {
      const result = window.appBluetoothListResultString
      console.log('返回藍(lán)牙list', result)
      if (result) {
        this.bluetoothShow = true
        this.listArr = JSON.parse(result)
      }
    },
    // 選中設(shè)備
    selectBluetooth(item) {
      console.log('選中設(shè)備', item)
      this.deviceId = item.deviceId
      uni.postMessage({
        data: {
          action: 'connBluetooth',
          params: { deviceId: this.deviceId }
        }
      })
    }
  }
}

2、實(shí)際Vue頁(yè)面:如下
import BluetoothMixins from '@/mixins/bluetoothMixins'
export default {
  mixins: [BluetoothMixins],
  methods: {
  // 點(diǎn)擊打印按鈕
   async print(deliveryNo) {
      console.log('配送單deliveryNo----', deliveryNo)
      // 獲取藍(lán)牙打印參數(shù)
      const res = await this.$api.getDeliveryPrintParam({ deliveryNo })
      if (res.success) {
      // 判斷之前是否有連接過藍(lán)牙
        const deviceId = localStorage.getItem('bluetoothDeviceId')
        // alert(`${deviceId}---配送單緩存值`)
        if (deviceId) {
          // 連接過直接打印
          uni.postMessage({
            data: {
              action: 'bluetoothPrint',
              params: { deviceId, command: JSON.parse(res.data) }
            }
          })
        } else {
        // 沒有連接過,先去獲取藍(lán)牙設(shè)備數(shù)據(jù)(list)
          uni.postMessage({
            data: {
              action: 'getBluetoothList'
            }
          })
        }
      }
    }
   }
 }

相關(guān)文章

基于ElementUi再次封裝基礎(chǔ)組件文檔


基于ant-design-vue再次封裝基礎(chǔ)組件文檔


vue3+ts基于Element-plus再次封裝基礎(chǔ)組件文檔文章來源地址http://www.zghlxwxcb.cn/news/detail-831298.html

到了這里,關(guān)于Vue H5項(xiàng)目,怎么引入uni.webview sdk,調(diào)用uni postMessage實(shí)現(xiàn)手機(jī)藍(lán)牙連接打印功能(uniapp)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 安卓WebView(H5)調(diào)用原生相機(jī)及相冊(cè)

    安卓WebView(H5)調(diào)用原生相機(jī)及相冊(cè)

    在開始敘述正文之前筆者先聲明一下應(yīng)用場(chǎng)景:例如在網(wǎng)頁(yè)上的即時(shí)通訊需要能拍照或者從圖庫(kù)選擇圖片來進(jìn)行上傳,此場(chǎng)景下就可以用到這篇文章的內(nèi)容 正文 首先,如果你已經(jīng)把相機(jī)以及訪問文件夾的權(quán)限都加上了并且WebView的基礎(chǔ)操作都做完了,就差上傳圖片了的話那就參

    2024年02月11日
    瀏覽(27)
  • uni-app 使用webview加載H5打開微信小程序

    uni-app 使用webview加載H5打開微信小程序

    最近公司有個(gè)需求要求在app里點(diǎn)擊一個(gè)功能打開小程序,并且關(guān)閉小程序回到app,模仿平安保險(xiǎn)app。 畢竟我也是剛學(xué)習(xí)uni-app,找了很多資料,找到了一個(gè)天天外鏈的網(wǎng)站可以生成一個(gè)小程序的鏈接,使用uni的webview去加載這個(gè)鏈接,很好,需求滿足,但是收費(fèi),那能不能自己

    2023年04月18日
    瀏覽(84)
  • 使用vue2開發(fā)uni-app項(xiàng)目--引入uview-ui

    使用vue2開發(fā)uni-app項(xiàng)目--引入uview-ui

    提示:文章寫完后,目錄可以自動(dòng)生成,如何生成可參考右邊的幫助文檔 目錄 前言 一、安裝 1、安裝uview-ui 2、安裝scss支持 二、配置 1、在main.js中引入uView庫(kù) 2、uni.scss文件中引入uView的全局SCSS主題文件 ?3、在APP.vue文件中引入uView基礎(chǔ)樣式 4、在pages.json中 配置easycom組件模式

    2024年02月04日
    瀏覽(33)
  • 微信小程序和webview使用postMessage交互

    微信小程序和webview使用postMessage交互

    小程序和webview能交互,但是沒有你想的那個(gè)完美 小程序向webview傳遞參數(shù)只能使用url攜帶參數(shù) webview向小程序傳遞參數(shù)可以使用postMessage, 但是注意了,postMessage只會(huì)在特定的時(shí)機(jī)執(zhí)行 ,請(qǐng)看官方文檔 由此可見,如果你想點(diǎn)擊webview中的一個(gè)按鈕A,然后給小程序發(fā)消息,然后由

    2024年02月02日
    瀏覽(25)
  • 前后端分離項(xiàng)目,vue+uni-app+php+mysql電影院售票系統(tǒng)(H5移動(dòng)項(xiàng)目) 開題報(bào)告

    ? 畢業(yè)論文 基于Vue.js電影院售票系統(tǒng)(H5) 開題報(bào)告 學(xué)??? 院: ?????????????????????? 專??? 業(yè): ? ????????????????? ???? 年??? 級(jí): ???????????????????? ?? 學(xué)生姓名: ?????????????????????? 指導(dǎo)教師: ????? 黃菊華??

    2024年02月07日
    瀏覽(29)
  • 微信小程序webview(H5頁(yè)面)調(diào)用微信小程序支付

    1.業(yè)務(wù)描述:微信小程序商城入口進(jìn)入的頁(yè)面是商城H5頁(yè)面,在H5頁(yè)面進(jìn)行微信支付如何實(shí)現(xiàn); 2.微信小程序(webview訪問H5頁(yè)面)必須使用微信小程序支付; 如何實(shí)現(xiàn)以及實(shí)現(xiàn)方式以及支付后頁(yè)面返回功能: 商品詳情(h5頁(yè)面)--商品確認(rèn)頁(yè)(h5頁(yè)面)--收銀臺(tái)(h5頁(yè)面)(點(diǎn)擊調(diào)

    2024年02月11日
    瀏覽(94)
  • js封裝SDK 在VUE、小程序、公眾號(hào)直接調(diào)用js調(diào)用后端接口(本文以vue項(xiàng)目為例)

    1.封裝一個(gè)js文件 msgSdk.js 注意:需要修改這個(gè)請(qǐng)求地址? apiServiceAddress 2.在index.html中引入 msgSdk.js文件 和 jquery文件 3.在頁(yè)面中調(diào)用

    2024年04月27日
    瀏覽(24)
  • uniapp+vue3+vite+ts搭建項(xiàng)目引入uni-ui和uviewPlus組件庫(kù)

    一、創(chuàng)建項(xiàng)目架構(gòu) 首先使用官方提供的腳手架創(chuàng)建一個(gè)項(xiàng)目 在這里插入代碼片 ,這里我創(chuàng)建的 vue3 + vite + ts 的項(xiàng)目: (如命令行創(chuàng)建失敗,請(qǐng)直接訪問 gitee下載模板) 二、下載依賴 啟動(dòng) 三、下載安裝包 引入uni-ui src/package.json 文件配置easycom模式 引入uview-plus main.ts配置 u

    2024年02月13日
    瀏覽(16)
  • uni-app(Vue3/Vite) + vant UI(Vue3版本)+ js 按需引入的項(xiàng)目搭建

    uni-app(Vue3/Vite) + vant UI(Vue3版本)+ js 按需引入的項(xiàng)目搭建

    ????????因?yàn)橐瓿绍浖こ痰捻?xiàng)目,要做一個(gè)nativeApp,看了很多的技術(shù)文檔以后決定使用多端兼容的uni-app來開發(fā)。組件方面的話最后決定使用目前比較火的Vant UI。但是看了CSDN和掘金上面的很多文章,似乎沒有一篇是關(guān)于uni-app中使用Vite對(duì)vant組件進(jìn)行按需引入(可能這個(gè)

    2023年04月09日
    瀏覽(93)
  • 已解決:安卓自帶的webview加載前端h5項(xiàng)目白屏?xí)r長(zhǎng)嚴(yán)重,vue首頁(yè)加載白屏?xí)r間過長(zhǎng),那我讓app進(jìn)入的時(shí)候就提前加載網(wǎng)頁(yè)

    自己寫的vue項(xiàng)目,自己寫的安卓殼子,本來自己覺得慢,忍忍就過去了,但是人家覺得慢,你不得改么?結(jié)果是前端自己開發(fā),安卓也自己開發(fā),想甩個(gè)鍋都沒法甩,總不能甩給后端吧?哈哈哈 描述一下我的情況,我寫了一個(gè)vue項(xiàng)目,需要嵌在安卓里運(yùn)行,沒想到安卓webvi

    2024年02月03日
    瀏覽(23)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包