key="index">
<view style="width: 30rpx;">{{index+1}}</view>
<view style="width: 180rpx;">{{device.name}}</view>
<view style="width: 310rpx;">{{device.address}}</view>
<view class="flex-item"><button type="primary" plain size="mini" @click="print()">打印测试</button></view>
<view class="flex-item"><button type="warn" plain size="mini"
@click="connectBlue(device)">{{curBlue==device.address?"已连接":"连接蓝牙"}}</button></view>
</view>
</view>
</template>
<script>
var tsc = require('../../components/gprint/tsc.js')
var androidBlue = require('../../components/gprint/androidBluetooth.js')
export default {
data() {
return {
curBlue: "",
show: {
setting: false
},
deviceList: []
}
},
onShow() {
},
onLoad() {
var that = this;
// #ifdef APP-PLUS
that.initPrinter();
// #endif
uni.getStorage({
key: 'myAndroidLanya',
success: function(res) {
console.log(111111111, res.data)
that.curBlue = res.data;
},
fail(e) {
uni.showToast({
title: '未连接蓝牙打印机',
duration: 2000,
icon: "none"
})
}
});
},
methods: {
connectBlue(device) {
let dev = device
let that = this;
uni.setStorage({
key: 'myAndroidLanya',
data: dev.address
});
uni.setStorage({
key: 'myAndroidLanyaName',
data: dev.name
});
this.curBlue = device.address
uni.showToast({
title: '连接成功',
icon: 'success',
duration: 2000
});
this.$forceUpdate()
},
initPrinter: function() {
var that = this;
that.deviceList = [];
var main = plus.android.runtimeMainActivity();
var Context = plus.android.importClass("android.content.Context");
var BManager = main.getSystemService(Context.BLUETOOTH_SERVICE);
plus.android.importClass(BManager);
var BAdapter = BManager.getAdapter();
plus.android.importClass(BAdapter);
var lists = BAdapter.getBondedDevices();
plus.android.importClass(lists);
var iterator = lists.iterator();
plus.android.importClass(iterator);
while (iterator.hasNext()) {
var d = iterator.next();
plus.android.importClass(d);
var temp = {
name: d.getName(),
address: d.getAddress(),
status: d.getBondState(),
uuids: d.getUuids(),
op: d
};
that.deviceList.push(temp);
}
console.log(that.deviceList, 44444444444444)
},
print() {
let command = this.getCommand()
console.log(111111111, androidBlue, command)
androidBlue.androidBlue.printToLanya(command)
},
printSomething(dev, print) {
var that = this;
var main = plus.android.runtimeMainActivity();
var BluetoothAdapter = plus.android.importClass("android.bluetooth.BluetoothAdapter");
var UUID = plus.android.importClass("java.util.UUID");
var uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
var BAdapter = BluetoothAdapter.getDefaultAdapter();
var device = BAdapter.getRemoteDevice(dev.address);
plus.android.importClass(device);
var bluetoothSocket = device.createInsecureRfcommSocketToServiceRecord(uuid);
plus.android.importClass(bluetoothSocket);
console.log("开始连接打印机:" + dev.name);
if (!bluetoothSocket.isConnected()) {
bluetoothSocket.connect();
if (bluetoothSocket.isConnected()) {
console.log("设备已连接,开始发送打印文件");
var outputStream = bluetoothSocket.getOutputStream();
plus.android.importClass(outputStream);
print(outputStream);
bluetoothSocket.close();
if (!bluetoothSocket.isConnected()) {
console.log("设备已关闭");
}
} else {
uni.showToast({
title: '设备连接失败',
icon: 'error',
duration: 2000
});
}
}
},
getCommand() {
var command = tsc.jpPrinter.createNew();
command.setCls() //清除缓冲区,防止下一个没生效
command.setSize(100, 50) //设置标签大小,单位mm.具体参数请用尺子量一下
command.setGap(2) //设置两个标签之间的间隙,单位mm.具体参数请用尺子量一下
command.setCls() //清除缓冲区
// command.setBox(10, 10, 100, 100, 5) //绘制一个边框
let x = 12;
let y = 12;
let lineHeight = 60;
let curLine = 0;
let font = "TSS24.BF2"
let fangdax = 2;
let fangday = 2
command.setText(x, y + lineHeight * curLine, font, 0, fangdax, fangday, "名称:") //绘制文字
curLine++;
command.setText(x, y + lineHeight * curLine, font, 0, fangdax, fangday, "批号:") //绘制文字
curLine++;
command.setText(x, y + lineHeight * curLine, font, 0, fangdax, fangday, "人员:") //绘制文字
curLine++;
command.setText(x, y + lineHeight * curLine, font, 0, fangdax, fangday, "日期") //绘制文字
// curLine++;
// command.setText(x, y + lineHeight * curLine, "TSS24.BF2", 0, 1, 1, "数量:") //绘制文字
// command.setText(x + 80, y + lineHeight * curLine, "TSS24.BF2", 0, 1, 1, item.createFqty) //绘制文字
// command.setText(x + 120, y + lineHeight * curLine, "TSS24.BF2", 0, 1, 1, "单位:") //绘制文字
// command.setText(x + 200, y + lineHeight * curLine, "TSS24.BF2", 0, 1, 1, item.unitName) //绘制文字
// command.setText(1, 275, "TSS24.BF2", 0, 1, 1, "条码号:") //绘制文字
// command.setText(110, 275, "TSS24.BF2", 0, 1, 1, item.barCode) //绘制文字
// command.setQrcode(x + 200, y + 40, "L", 5, 'M', barcodeInfo.barCode) //绘制二维码
command.setPagePrint() //执行打印指令
return command;
},
mySleep: async function(time) {
await this.mypromise(time);
},
mypromise: function(time) {
return new Promise((resolve) => setTimeout(resolve, time));
}
}
}
</script>
<style >
/* page {
padding: 0;
margin: 0;
}
.picked {
background-color: lavender;
} */
</style>
1 个回复
1***@qq.com (作者) - 搬砖工人
请问搞安卓原生的大佬,android 版本提升后,是因为权限没有配置,还是要改变方法。在uni文档找好久没找到解决办法。