| | |
| | | <view> |
| | | <!-- 蓝牙连接状态 MAC地址 --> |
| | | <view class="connection-status"> |
| | | <view>连接状态:</view> |
| | | <view style="display: flex; flex-direction: row; gap: 20rpx;padding: 20rpx;"> |
| | | <view style="width: 5rem;">连接设备: </view> |
| | | <view v-if="$printer.isConnected() === false">无</view> |
| | | <view v-else="$printer.isConnected() === true" |
| | | style="flex: 1;display: inline-flex;flex-direction: row;justify-content: space-between;"> |
| | | <view>{{ $printer.connectedDevice().name }}</view> |
| | | <view>{{ $printer.connectedDevice().address }}</view> |
| | | </view> |
| | | </view> |
| | | </view> |
| | | <!-- 操作区 --> |
| | | <view class="buttons"> |
| | |
| | | <button class="btn-d" @click="closeBluetooth()">断开连接</button> |
| | | </view> |
| | | <!-- 设备列表 --> |
| | | <view v-for="(device, index) in discoveredDevices" :key="device.address"> |
| | | <uni-card :title="device.name" :extra="connectedDeviceId === device.address?'已连接':'未连接'" |
| | | style="margin: 10px;"> |
| | | <view class="operation-zone"> |
| | | <button class="op1" size="mini" plain @click="connectBT(device)">连接设备</button> |
| | | </view> |
| | | </uni-card> |
| | | </view> |
| | | <scroll-view scroll-y="true" style="height: calc(80vh - 8rem);"> |
| | | <view v-for="(device, index) in discoveredDevices" :key="device.address"> |
| | | <uni-card :title="device.name" :extra="connectedDeviceId === device.address?'已连接':'未连接'" |
| | | style="margin: 10px;"> |
| | | <view class="operation-zone"> |
| | | <button class="op1" size="mini" plain @click="connectBT(device)">连接设备</button> |
| | | </view> |
| | | </uni-card> |
| | | </view> |
| | | </scroll-view> |
| | | </view> |
| | | </template> |
| | | |
| | | <script> |
| | | import bluetoothTool from '@/plugins/BluetoothTool.js' |
| | | import printerMeta from "../../static/json/printerMeta.json"; |
| | | import { |
| | | CommonUtils |
| | | } from '../../utils/common'; |
| | | import permission from '@/plugins/permission.js' |
| | | import { |
| | | InputImage |
| | |
| | | FakeConnectedDevice, |
| | | WriteOptions, |
| | | } from '@psdk/frame-father'; |
| | | import { |
| | | getAddress |
| | | } from '../../utils/auth'; |
| | | import blueToothTool from '@/plugins/BluetoothTool.js'; |
| | | import { nextTick } from "vue"; |
| | | export default { |
| | | name: "blueToothConnector", |
| | | name: "blueToothPrinterConnector", |
| | | data() { |
| | | return { |
| | | discoveredDevices: [], // 查询到的设备 |
| | | connectedDeviceId: "" |
| | | connectedDeviceId: "", |
| | | readSuccess: false, |
| | | clock: null, |
| | | }; |
| | | }, |
| | | methods: { |
| | |
| | | console.log('start discovery devices'); |
| | | this.discoveredDevices = []; |
| | | console.log(res) |
| | | bluetoothTool.discoveryNewDevice(); |
| | | blueToothTool.discoveryNewDevice(); |
| | | }, |
| | | fail: async (e) => { |
| | | console.error(e) |
| | | switch (e.code) { |
| | | case "10009": |
| | | this.showToast("此设备不支持设备搜索功能!"); |
| | | break; |
| | | default: |
| | | console.error(e); |
| | | if(e.errCode == 10009) { |
| | | this.showToast('此设备不支持设备搜索功能!'); |
| | | } |
| | | } |
| | | }) |
| | | |
| | | }, |
| | | onDevice(device) { |
| | | console.log("监听寻找到新设备的事件---------------") |
| | |
| | | uni.showLoading({ |
| | | title: '连接中' |
| | | }); |
| | | bluetoothTool.connDevice(device.address, (result) => { |
| | | console.log(result) |
| | | blueToothTool.connDevice(device.address, (result) => { |
| | | console.log("设备MAC地址: ", device.address) |
| | | console.log("printer:", vm.$printer._connectedDevice) |
| | | uni.hideLoading() |
| | | if (result) { |
| | | // // console.log(result); |
| | | bluetoothTool.cancelDiscovery(); |
| | | blueToothTool.cancelDiscovery(); |
| | | // console.log(vm.$printer) |
| | | vm.$printer.init(new FakeConnectedDevice()); |
| | | vm.connectedDeviceId = device.address; |
| | | vm.$printer._connectedDevice.address = device.address |
| | | vm.$printer._connectedDevice.name = device.name |
| | | console.log("printer:", vm.$printer._connectedDevice) |
| | | uni.showToast({ |
| | | icon: 'none', |
| | | title: '连接成功' |
| | | }) |
| | | |
| | | } else { |
| | | uni.showToast({ |
| | | icon: 'none', |
| | |
| | | }, |
| | | stopSearchBT() { |
| | | console.log("停止搜寻附近的蓝牙外围设备---------------") |
| | | bluetoothTool.cancelDiscovery(); |
| | | blueToothTool.cancelDiscovery(); |
| | | }, |
| | | closeBluetooth() { |
| | | console.log("停止蓝牙连接") |
| | | const vm = this; |
| | | if (vm.connectedDeviceId != '') { |
| | | bluetoothTool.closeBtSocket(); |
| | | blueToothTool.closeBtSocket(); |
| | | vm.connectedDeviceId = ""; |
| | | vm.$printer._connectedDevice = null |
| | | } |
| | | |
| | | }, |
| | | showToast(msg, status = "none") { |
| | | uni.showToast({ |
| | | title: msg, |
| | | icon: status, |
| | | duration: 2000 |
| | | }); |
| | | }, |
| | | async checkReadSuccess(count) { |
| | | // 批量插入时,如果前一次发送成功,则清除前一次的时钟 |
| | | if (this.clock != null) { |
| | | this.clock.stop(); |
| | | this.clock = null; |
| | | } |
| | | let that = this |
| | | return new Promise((resolve, reject) => { |
| | | let currentCount = 0; |
| | | that.clock = new CommonUtils.timeClock(() => { |
| | | console.log(that.readSuccess) |
| | | currentCount++; |
| | | if (that.readSuccess == true) { |
| | | that.clock.stop(); |
| | | resolve(true); |
| | | } else if (currentCount >= count) { |
| | | that.clock.stop(); |
| | | reject(new Error('读取超时')); |
| | | } |
| | | }, printerMeta['timeoutPerMillSecond']); |
| | | |
| | | // 启动时钟 |
| | | that.clock.start(); |
| | | }); |
| | | }, |
| | | async sendMessage(cmd) { |
| | | cmd = Array.from(this.uint8ArrayToSignedArray(cmd)) |
| | | uni.showLoading({ |
| | | title: "发送中..." |
| | | }) |
| | | // console.log("msg: " + cmd); |
| | | let that = this |
| | | return new Promise((resolve, reject) => { |
| | | const result = blueToothTool.sendByteData(cmd); |
| | | if (!result) { |
| | | this.clock.stop(); |
| | | reject("发送失败,请重试...") |
| | | } |
| | | that.checkReadSuccess(printerMeta['timeoutCount']) |
| | | .then((res) => { |
| | | if (res === true) { |
| | | resolve(true) |
| | | } |
| | | }) |
| | | .catch((err) => { |
| | | this.clock.stop(); |
| | | reject('设备异常,请检查设备状态...') |
| | | }) |
| | | .finally(() => { |
| | | that.readSuccess = false |
| | | }) |
| | | }) |
| | | |
| | | }, |
| | | uint8ArrayToSignedArray(uint8Array) { |
| | | let signedArray = new Array(uint8Array.length); |
| | | for (let i = 0; i < uint8Array.length; i++) { |
| | | if (uint8Array[i] >= 128) { |
| | | signedArray[i] = uint8Array[i] - 256; |
| | | } else { |
| | | signedArray[i] = uint8Array[i]; |
| | | } |
| | | } |
| | | return signedArray; |
| | | }, |
| | | }, |
| | | async mounted() { |
| | | //#ifdef APP-PLUS |
| | | // 蓝牙 |
| | | bluetoothTool.init({ |
| | | console.log("蓝牙初始化") |
| | | blueToothTool.init({ |
| | | listenBTStatusCallback: (state) => { |
| | | if (state == 'STATE_ON') { |
| | | console.log(state); |
| | | } |
| | | if (state == 'STATE_OFF') { |
| | | console.log(state); |
| | | this.closeBluetooth() |
| | | } |
| | | }, |
| | | discoveryDeviceCallback: this.onDevice, |
| | | discoveryFinishedCallback: function() { |
| | | console.log("搜索完成"); |
| | | }, |
| | | readDataCallback: function(dataByteArr) { |
| | | /* if(that.receiveDataArr.length >= 200) { |
| | | that.receiveDataArr = []; |
| | | } |
| | | that.receiveDataArr.push.apply(that.receiveDataArr, dataByteArr); */ |
| | | readDataCallback: (dataByteArr) => { |
| | | // if(that.receiveDataArr.length >= 200) { |
| | | // that.receiveDataArr = []; |
| | | // } |
| | | // that.receiveDataArr.push.apply(that.receiveDataArr, dataByteArr); |
| | | this.readSuccess = true |
| | | console.log("读取完成" + dataByteArr); |
| | | }, |
| | | connExceptionCallback: function(e) { |
| | | console.log(e); |
| | | } |
| | | }); |
| | | |
| | | // 查看连接设备 |
| | | let pariedDevices = bluetoothTool.getPairedDevices() |
| | | console("已配对设备", pariedDevices) |
| | | |
| | | if (this.$printer.isConnected()) { |
| | | this.connectedDeviceId = this.$printer.connectedDevice().address |
| | | } |
| | | //#endif |
| | | }, |
| | | } |