| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- <style lang="scss" scoped>
- .center{
- font-size: 32upx;
- padding: 30upx;
- .connected-title{
- text{color: #2079ff;}
- }
- .seek-btn{
- margin: 30upx 0;
- width: 60%;
- height: 80upx;
- color: #FFFFFF;
- background: #169bd5;
- border-radius: 20upx;
- }
- .ble-list{
- width: 65%;
- height: 80upx;
- text{color: #2079ff;}
- }
- }
- </style>
- <template>
- <view class="center">
- <view class="flex connected-title">已连接蓝牙:<text>{{nameBLE}}</text></view>
- <view class="flex-center seek-btn" @click="searchBle">搜索蓝牙打印机</view>
- <!--<button @click="searchBle">搜索蓝牙打印机</button>-->
- <!--<view style="margin-top: 30upx;" :key="index" v-for="(item, index) in devices">-->
- <view class="flex-space ble-list" :key="index" v-for="(item, index) in devices" @click="onConn(item)">
- {{item.name}} <text>连接</text>
- </view>
- <!--<button style="margin-top: 100upx;" @click="senBleLabel()">标签打印</button>-->
- <!--<textarea @blur="bindTextAreaBlur" auto-height placeholder-style="color:#F76260" placeholder="请输入票据信息" v-model="piaojuText"/>-->
- <!--<button style="margin-top: 100upx;" @click="billPrint()">票据打印</button>-->
- </view>
- </template>
- <script>
- import BLE from "@/mixins/BLE";
- // var tsc = require("./js/gprint/tsc.js");
- // var esc = require("./js/gprint/esc.js");
- export default {
- mixins: [BLE],
- data() {
- return {
- // devices: [],
- // currDev: null,
- // connId: "",
- // piaojuText: "",
- // deviceId:'6CE888E4-6BF4-2D2A-60AE-07480F6A39A6',
- };
- },
- onLoad: function() {},
- // methods: {
- // destroyed: function() {
- // console.log("destroyed----------");
- // if (this.connId != "") {
- // uni.closeBLEConnection({
- // deviceId: this.connId,
- // success(res) {
- // console.log(res);
- // }
- // });
- // }
- // },
- // searchBle() {
- // var that = this;
- // uni.openBluetoothAdapter({
- // success(res) {
- // console.log(res,"打开 蓝牙模块");
- // that.onDevice();//监听搜索新设备
- // //获取本机蓝牙适配器状态。
- // uni.getBluetoothAdapterState({
- // success: function(res) {
- // console.log(res);
- // if (res.available) {
- // if (res.discovering) {
- // that.stopFindBule();
- // }
- // //搜索蓝牙
- // //开始搜寻附近的蓝牙外围设备
- // console.log("开始搜寻附近的蓝牙外围设备");
- // uni.startBluetoothDevicesDiscovery({
- // success(res) {
- // console.log(res);
- // }
- // });
- // } else {
- // console.log("本机蓝牙不可用");
- // }
- // }
- // });
- // }
- // });
- // },
- // onDevice() {
- // var that = this;
- // //监听寻找到新设备的事件
- // uni.onBluetoothDeviceFound(function(devices) {
- // console.log(devices,'设备列表');
- // var re = JSON.parse(JSON.stringify(devices));
- // let name = re.devices[0].name?re.devices[0].name:'未知设备';
- // let deviceId = re.devices[0].deviceId;
- // that.devices.push({name: name,deviceId: deviceId,services: []});
- // // that.zidonglianjie();
- // // if (name != "未知设备" && name != "") {
- // // let deviceId = re.devices[0].deviceId;
- // // that.devices.push({name: name,deviceId: deviceId,services: []});
- // // }
- // });
- // },
- // zidonglianjie(){
- // this.devices.forEach((ele)=>{
- // if(ele.deviceId==this.deviceId){
- // this.onConn(ele);
- // }
- // })
- // },
- // stopFindBule() {
- // console.log("停止搜寻附近的蓝牙外围设备---------------");
- // uni.stopBluetoothDevicesDiscovery({
- // success(res) {
- // console.log(res);
- // }
- // });
- // },
- // onConn(item) {
- // var that = this;
- // console.log("连接蓝牙---------------" + item.deviceId);
- // let deviceId = item.deviceId;
- // uni.createBLEConnection({
- // deviceId: deviceId,
- // complete(res) {
- // if (res.errMsg == "createBLEConnection:ok") {
- // uni.setStorageSync('BLE',item);
- // console.log("连接蓝牙-[" + item.name + "]--成功");
- // that.$msg("连接蓝牙-[" + item.name + "]--成功");
- // that.connId = deviceId;
- // that.currDev = item;
- // setTimeout(function() {
- // that.getBLEServices(deviceId);
- // }, 2000);
- // } else {
- // console.log(res);
- // }
- // //连接成功 关闭搜索
- // that.stopFindBule();
- // }
- // });
- // },
- //
- // getBLEServices(_deviceId) {
- // var that = this;
- // let deviceId = _deviceId;
- // console.log("获取蓝牙设备所有服务(service)");
- //
- // uni.getBLEDeviceServices({
- // // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
- // 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 需要已经通过 createBLEConnection 与对应设备建立链接
- // 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 that.devices) {
- // if (that.devices[index].deviceId == deviceId) {
- // that.devices[index].services.push({
- // serviceId: serviceId,
- // characteristicId: uuid
- // });
- // break;
- // }
- // }
- // console.log(JSON.stringify(that.devices));
- // }
- // }
- // }
- // });
- // }
- // },
- // fail(res) {
- // console.log(res);
- // }
- // });
- // },
- // senBlData(deviceId, serviceId, characteristicId, uint8Array) {
- // console.log("************deviceId = [" +deviceId +"] serviceId = [" +serviceId +"] characteristics=[" +characteristicId +"]");
- // var uint8Buf = Array.from(uint8Array);
- // function split_array(datas, size) {
- // var result = {};
- // var j = 0;
- // for (var i = 0; i < datas.length; i += size) {
- // result[j] = datas.slice(i, i + size);
- // j++;
- // }
- // 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 + "】次写数据" + 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) {
- // realWriteData(sendloop, i + 1);
- // }
- // });
- // }
- // var i = 0;
- // realWriteData(sendloop, i);
- // },
- // senBleLabel() {
- // //标签模式
- // let deviceId = this.currDev.deviceId;
- // let serviceId = this.currDev.services[0].serviceId;
- // let characteristicId = this.currDev.services[0].characteristicId;
- // var command = tsc.jpPrinter.createNew();
- // command.setSize(40, 60);
- // command.setGap(2);
- // command.setCls();
- // // command.setText(50, 10, "TSS24.BF2", 1, 1, "打印测试")
- // // command.setQR(50, 50, "L", 5, "A", "977767937@qq.com")
- // // command.setQR(a, b, c, d, e, f ")
- // command.setText(30, 0, 2, 0, "arial", "测试");
- // command.setText("福建省厦门市集美管控黄庄三里9号绿意花街");
- // // command.setText("阿斯顿",'','13333333333');
- // // command.setText(10, 90, "TSS24.BF2", 1, 1, "收花人电话:5521667788");
- // // command.setText(10, 130, "TSS24.BF2", 1, 1, "收花时间:2020-6-10 上午");
- // // command.setText(10,170,"TSS24.BF2",1,1,"收花地址:11111厦门市集美区凤林前山路147号110室");
- // command.setPagePrint();
- // this.senBlData(deviceId, serviceId, characteristicId, command.getData());
- // },
- // billPrint() {
- // //票据模式
- // let deviceId = this.currDev.deviceId;
- // let serviceId = this.currDev.services[0].serviceId;
- // let characteristicId = this.currDev.services[0].characteristicId;
- // var command = esc.jpPrinter.createNew();
- // command.init();
- // command.setText(this.piaojuText);
- // command.setPrintAndFeedRow(1);
- // this.senBlData(deviceId, serviceId, characteristicId, command.getData());
- // }
- // }
- };
- </script>
|