欧美三级国产三级日韩三级_亚洲熟妇丰满大屁股熟妇_欧美亚洲成人一区二区三区_国产精品久久久久久模特

微信小程序藍(lán)牙連接打印機(jī) - 新聞資訊 - 云南小程序開發(fā)|云南軟件開發(fā)|云南網(wǎng)站建設(shè)-昆明葵宇信息科技有限公司

159-8711-8523

云南網(wǎng)建設(shè)/小程序開發(fā)/軟件開發(fā)

知識(shí)

不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價(jià)值,我們?cè)谧非笃湟曈X表現(xiàn)的同時(shí),更側(cè)重于功能的便捷,營(yíng)銷的便利,運(yùn)營(yíng)的高效,讓網(wǎng)站成為營(yíng)銷工具,讓軟件能切實(shí)提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序?yàn)楹笃谏?jí)提供便捷的支持!

您當(dāng)前位置>首頁(yè) » 新聞資訊 » 小程序相關(guān) >

微信小程序藍(lán)牙連接打印機(jī)

發(fā)表時(shí)間:2020-10-12

發(fā)布人:葵宇科技

瀏覽次數(shù):91

需求:藍(lán)牙搜索打印機(jī)并打印信息

微信小程序中需要打印訂單信息
1.搜索打印機(jī)藍(lán)牙,并鏈接
2.發(fā)送打印

微信小程序連接藍(lán)牙

1.初始化藍(lán)牙
1.1 wx.openBluetoothAdapter(Object object) :初始化藍(lán)牙
1.2 wx.getBluetoothAdapterState():檢測(cè)本機(jī)藍(lán)牙是否可用
wx.openBluetoothAdapter({//初始化藍(lán)牙
  success: function (res) {
    wx.getBluetoothAdapterState({//獲取本機(jī)藍(lán)牙適配器狀態(tài)。
      success: function (res) {
        if (res.available) {//藍(lán)牙適配器是否可用
         //下一步操作
        } else {
          wx.showModal({
            title: '提示',
            content: '本機(jī)藍(lán)牙不可用',
          })
        }
      }
    })
  },
  fail: function () {
    wx.showModal({
      title: '提示',
      content: '藍(lán)牙初始化失敗,請(qǐng)打開藍(lán)牙',
    })
  }
})
2.開始搜索藍(lán)牙設(shè)備
2.1 wx.startBluetoothDevicesDiscovery() : 開始搜索藍(lán)牙設(shè)備
2.2 wx.getBluetoothDevices():獲取搜索到的藍(lán)牙設(shè)備
wx.startBluetoothDevicesDiscovery({//搜索附近的藍(lán)牙設(shè)備
      services: [],//篩選過濾搜索的藍(lán)牙設(shè)備
      success(res) {
        setTimeout(() => {
          wx.getBluetoothDevices({//獲取藍(lán)牙列表
            services: [],
            success: function (res) {
              callback(res);//返回藍(lán)牙列表
            },
            fail(res) {
              wx.showModal({
                title: '提示',
                content: '獲取藍(lán)牙設(shè)備列表失敗',
              })
            }
          })
        }, 2000)
        //關(guān)閉搜索
        wx.stopBluetoothDevicesDiscovery({
          success: function (res) {}
        })
      }
    })
   注:搜索完后要停止搜索
3.連接藍(lán)牙
wx.createBLEConnection():連接低功耗藍(lán)牙設(shè)備
 匹配到的藍(lán)牙設(shè)備ID 發(fā)送連接藍(lán)牙的請(qǐng)求
 連接成功之后 應(yīng)該斷開藍(lán)牙搜索的api,然后去獲取所連接藍(lán)牙設(shè)備的service服務(wù)
 
 wx.createBLEConnection({//連接低功耗藍(lán)牙設(shè)備
  deviceId: _this.blue_data.device_id,//用于區(qū)分設(shè)備的 id
  success(res) {
  	
  },
  fail: function fail(err) {
    wx.showToast({
      title: '連接失敗!',
      icon: 'none',
      duration: 2000
    });
  } 
})
4.獲取藍(lán)牙設(shè)備的service服務(wù)
wx.getBLEDeviceServices():連接低功耗藍(lán)牙設(shè)備
獲取的serviceId有多個(gè)要試著連接最終確定哪個(gè)是穩(wěn)定版本的service 獲取服務(wù)完后獲取設(shè)備特征值

 wx.getBLEDeviceServices({//獲取藍(lán)牙設(shè)備所有服務(wù)(service)
  deviceId: _this.blue_data.device_id,
  success: function (res) {
    _this.blue_data.service_id = service_id;
    _this.getDeviceCharacter();
  }
})
5.獲取連接設(shè)備服務(wù)的所有特征值
wx.getBLEDeviceCharacteristics():獲取藍(lán)牙設(shè)備特征值
 獲取到的特征值有多個(gè),最后要用的是能讀,能寫,能監(jiān)聽的那個(gè)值的uuid作為特征值id

 wx.getBLEDeviceCharacteristics({
  deviceId: _this.blue_data.device_id,
  serviceId: _this.blue_data.service_id,
  success: function (res) {
    let notify_id, write_id, read_id;
    for (let i = 0; i < res.characteristics.length; i++) {
      let charc = res.characteristics[i];
      if (charc.properties.notify) {
        notify_id = charc.uuid;
      }
      if (charc.properties.write) {
        write_id = charc.uuid;
      }
      if (charc.properties.write) {
        read_id = charc.uuid;
      }
    }
    if (notify_id != null && write_id != null) {
      _this.blue_data.notify_id = notify_id;
      _this.blue_data.write_id = write_id;
      _this.blue_data.read_id = read_id;
    }
  }
})
完整代碼
let blueApi ={
  //連接的藍(lán)牙信息
  blue_data: {
    device_id: "",
    service_id: "",
    write_id: "",
    notify_id:"",
    connFlag:false
  },
  //第一步:連接藍(lán)牙
  connect() {
    if (!wx.openBluetoothAdapter) {
      wx.showModal({
        title: '提示',
        content: '當(dāng)前微信版本過低,無(wú)法使用該功能,請(qǐng)升級(jí)到最新微信版本后重試。',
      })
      return;
    }
    var _this = this;
    wx.openBluetoothAdapter({//初始化藍(lán)牙
      success: function (res) {
        wx.getBluetoothAdapterState({//獲取本機(jī)藍(lán)牙適配器狀態(tài)。
          success: function (res) {
            if (res.available) {//藍(lán)牙適配器是否可用
              if (res.discovering) {//藍(lán)牙適配器是否處于搜索狀態(tài)
                wx.stopBluetoothDevicesDiscovery({
                  success: function (res) {}
                })
              }
              _this.getBlueState();
            } else {
              wx.showModal({
                title: '提示',
                content: '本機(jī)藍(lán)牙不可用',
              })
            }
          }
        })
      },
      fail: function () {
        wx.showModal({
          title: '提示',
          content: '藍(lán)牙初始化失敗,請(qǐng)打開藍(lán)牙',
        })
      }
    })
  },
  //第二步:搜索附近藍(lán)牙,并獲取藍(lán)牙列表
  startSearch(callback) {
    var _this = this;
    wx.startBluetoothDevicesDiscovery({//搜索附近的藍(lán)牙設(shè)備
      services: [],//篩選過濾搜索的藍(lán)牙設(shè)備
      success(res) {
        setTimeout(() => {
          wx.getBluetoothDevices({//獲取藍(lán)牙列表
            services: [],
            success: function (res) {
              callback(res);//返回藍(lán)牙列表
            },
            fail(res) {
              wx.showModal({
                title: '提示',
                content: '獲取藍(lán)牙設(shè)備列表失敗',
              })
            }
          })
        }, 2000)
        _this.stopSearch();
      }
    })
  },
  //第三步:選中列表中藍(lán)牙設(shè)備,連接藍(lán)牙設(shè)備
  connectDevice(callback) {
    var _this = this;
    wx.createBLEConnection({//連接低功耗藍(lán)牙設(shè)備
      deviceId: _this.blue_data.device_id,//用于區(qū)分設(shè)備的 id
      success(res) {
        callback(res);
      },
      fail: function fail(err) {
        wx.showToast({
          title: '連接失敗!',
          icon: 'none',
          duration: 2000
        });
      } 
    })
  },
  //第四步:獲取藍(lán)牙設(shè)備所有服務(wù)
  getDeviceService() {
    var _this = this;
    wx.getBLEDeviceServices({//獲取藍(lán)牙設(shè)備所有服務(wù)(service)
      deviceId: _this.blue_data.device_id,
      success: function (res) {
        _this.blue_data.service_id = service_id;
        _this.getDeviceCharacter();
      }
    })
  },
  //第五步:獲取連接設(shè)備服務(wù)的所有特征值
  getDeviceCharacter() {
    let _this = this;
    wx.getBLEDeviceCharacteristics({
      deviceId: _this.blue_data.device_id,
      serviceId: _this.blue_data.service_id,
      success: function (res) {
        let notify_id, write_id, read_id;
        for (let i = 0; i < res.characteristics.length; i++) {
          let charc = res.characteristics[i];
          if (charc.properties.notify) {
            notify_id = charc.uuid;
          }
          if (charc.properties.write) {
            write_id = charc.uuid;
          }
          if (charc.properties.write) {
            read_id = charc.uuid;
          }
        }
        if (notify_id != null && write_id != null) {
          _this.blue_data.notify_id = notify_id;
          _this.blue_data.write_id = write_id;
          _this.blue_data.read_id = read_id;
        }
      }
    })
  },
  //打印
  printer(buffer,success){
    var _this = this;
    wx.showLoading({
      title: '正在打印...',
    });
    _this.getDeviceService();
    wx.writeBLECharacteristicValue({
      deviceId: _this.blue_data.device_id,
      serviceId: _this.blue_data.service_id,
      characteristicId:_this.blue_data.notify_id,
      value: buffer, // 這里的value是ArrayBuffer類型,中間層傳過來(lái)的打印數(shù)據(jù)前端自己做轉(zhuǎn)換,轉(zhuǎn)換過程我這邊就不描述了;
      success(res) {
        success(res)
        wx.hideLoading();
      },
      fail: function fail(res) {
        console.log("數(shù)據(jù)發(fā)送失敗:" + JSON.stringify(res));
      },
      complete:function(){
        _this.closeBluetoothAdapter();
      }
    })
  },
  /* 輔助模塊 */
  stopSearch() {
    var _this = this;
    wx.stopBluetoothDevicesDiscovery({
      success: function (res) { }
    })
  },
  //關(guān)閉藍(lán)牙適配器
  closeBluetoothAdapter() {
    wx.closeBluetoothAdapter({
      success: function success(res) {
        console.log('關(guān)閉藍(lán)牙適配器');
      }
    });
  },
  breakConnction(){
    var _this = this;
    wx.closeBLEConnection({
      deviceId: _this.blue_data.device_id,
      success:function(){
        _this.blue_data.connFlag = false;
        console.log("斷開連接");
      }
    })
  },
  /* 優(yōu)化 */
  //第二步:連接設(shè)備模塊
  getBlueState() {
    console.log("連接設(shè)備");
    var _this = this;
    if (_this.blue_data.device_id != "") {
      wx.showLoading({
        title: '正在連接',
      })
      _this.connectDevice(function(res){
        wx.hideLoading();
        if (res.errCode == 0) {
          _this.blue_data.connFlag = true;
          wx.showToast({
            title: '連接成功',
            icon: 'success',
            duration: 1000
          })
        }
        blue.stopSearch();
      });
      return;
    } else {
      wx.navigateTo({
        url: '/printer/scan/scan',
      })
    }
  }
}
module.exports = {
  blue:blueApi
}

參考:https://imweb.io/topic/5bf3ce90611a25cc7bf1d788

相關(guān)案例查看更多