有需求就有供应,话不多说上demo
<template>
<view class="printer">
<view class="insert_blue">
<view class="title">
<text style="font-weight:bold;">打印机</text>
<text style="color:#7c7c7c;font-size:25rpx; " @click="this.showpopup = true">添加蓝牙打印机</text>
</view>
<view class="blue" v-if="printer">
<view > <text>{{printer}}</text> </view>
<view>
<u-button type="error" size="mini" @click="onBreakConnect">断开连接</u-button>
<u-button type="error" size="mini" @click="this.showpopup = true">重新连接</u-button>
</view>
</view>
<view class="empty" v-else>
<text class="iconfont icon-wulanya"></text>
尚未连接任何设备
<view style="padding:10rpx 100rpx">
<u-button shape="circle" @click="this.showpopup = true">添加蓝牙打印机</u-button>
</view>
</view>
</view>
<view class="demo_blue" v-if="printer">
<u-button type="success" @click="printSomething" >{{printer}} 打印机测试</u-button>
</view>
<u-popup v-model="showpopup" mode="bottom">
<view class="popup">
<view class="label">
附近可连接设备
</view>
<view
class="device"
v-for="(device,index) in deviceList" :key="index">
<view>
<view class="name">{{device.name}}</view>
<view class="address">{{device.address}}</view>
</view>
<view class="flex-item">
<u-button plain size="mini" @click="onConnect(device,printTest)" shape="circle" >连接</u-button>
</view>
</view>
</view>
</u-popup>
</view>
</template><script>
export default {
data() {
return {
showpopup:false,
printer:"",
device:null,
deviceList: []
}
},
onLoad() {
this.initPrinter();
uni.getStorage({
key:"Bluetooth"
}).then(res=>{
console.log(res.data.devices);
})
},
methods: {
onConnect(device,printTest){
this.printer = device.name
this.device = device
let func = printTest.toString()
if(this.printer){
uni.showToast({title:"连接成功"}).then(()=>{
uni.setStorage({
key:"Bluetooth",
data:{devices:device}
})
this.showpopup = false
})
}
},
onBreakConnect(){
// 断开蓝牙
this.printer = ""
this.device = null
printTests = null
},
initPrinter() {
this.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
};
this.deviceList.push(temp);
}
},
printSomething() {
let dev = this.device
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()) {
var outputStream = bluetoothSocket.getOutputStream();
plus.android.importClass(outputStream);
this.printTest(outputStream)
bluetoothSocket.close();
if (!bluetoothSocket.isConnected()) {
console.log("设备已关闭");
}
} else {
uni.showToast({
title: '设备连接失败',
icon:'error',
duration: 2000
});
}
}
},
printTest(outputStream) {
var text =
`
\x1B\x21\x10 \t#易派到家# \r\n \x1B\x21\x01普通字体\r\n
`
// text += "\x1B\x45\x10 蓝斑打印测试-字体变大 -加粗\r\n";
// text += "\x1B\x21\x01 普通字体\r\n";
const arrayBuffer = plus.android.invoke(text, 'getBytes', 'gbk');
outputStream.write(arrayBuffer);
outputStream.flush();
},
}
}
</script><style scoped lang="scss">
.demo_blue{
background: #fff;
padding:30rpx;border-radius:20rpx; margin:30rpx 0;
}
.blue{
padding:30rpx; display: flex; justify-content: space-between;
font-weight:bold;
}
.device{
padding:20rpx 20rpx;
margin:30rpx 0;
display: flex; justify-content:space-between; align-items:center; line-height:50rpx;
.address{
color:#ff9900;font-size:25rpx;
}
}
.popup{
height:70vh; width:100%;
padding:30rpx;
.label{
color:#999; font-size:25rpx;
}
}
.empty{
font-size:30rpx; color:#555555;
line-height:70rpx;text-align: center;
.iconfont{ font-size:40rpx }
}
.insert_blue{
background: #fff;
padding:30rpx;border-radius:20rpx;
.title{
padding:0 0 20rpx 0;display: flex;justify-content: space-between;
}
}
.printer {
padding:20rpx;
font-size:30rpx;
}
</style>
<style>
page{
background: #f8f8f8;
}
</style>