项目中用到的,贴上部份代码
1、扫描配对蓝牙打印机,address为打印机mac地址
function searchDevices(address) {
let that = this;
uni.showLoading({
title: '搜索中...',
mask: true
});
//注册类
var main = plus.android.runtimeMainActivity();
var IntentFilter = plus.android.importClass('android.content.IntentFilter');
var BluetoothAdapter = plus.android.importClass("android.bluetooth.BluetoothAdapter");
var BluetoothDevice = plus.android.importClass("android.bluetooth.BluetoothDevice");
var BAdapter = BluetoothAdapter.getDefaultAdapter();
var filter = new IntentFilter();
var bdevice = new BluetoothDevice();
var on = null;
var un = null;
that.ulist = []; //清空容器
that.blist = []; //清空容器
BAdapter.startDiscovery(); //开启搜索
var receiver;
receiver = plus.android.implements('io.dcloud.android.content.BroadcastReceiver', {
onReceive: function(context, intent) { //实现onReceiver回调函数
plus.android.importClass(intent); //通过intent实例引入intent类,方便以后的‘.’操作
if (intent.getAction() == "android.bluetooth.adapter.action.DISCOVERY_FINISHED") {
main.unregisterReceiver(receiver); //取消监听
uni.hideLoading();
} else {
var BleDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
//判断是否配对
if (BleDevice.getBondState() == bdevice.BOND_NONE) {
//参数如果跟取得的mac地址一样就配对
if (address == BleDevice.getAddress()) {
if (BleDevice.createBond()) {
that.blist.push({
id: BleDevice.getAddress(),
name: BleDevice.getName()
})
}
main.unregisterReceiver(receiver); //取消监听
uni.hideLoading();
} else {
if (BleDevice.getName() != on) { //判断防止重复添加
that.ulist.push({
id: BleDevice.getAddress(),
name: BleDevice.getName()
})
}
}
} else {
if (address == BleDevice.getAddress()) {
main.unregisterReceiver(receiver); //取消监听
uni.hideLoading();
}
if (BleDevice.getName() != un) { //判断防止重复添加
that.blist.push({
id: BleDevice.getAddress(),
name: BleDevice.getName()
})
}
}
}
}
});
filter.addAction(bdevice.ACTION_FOUND);
filter.addAction(BAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BAdapter.ACTION_DISCOVERY_FINISHED);
filter.addAction(BAdapter.ACTION_STATE_CHANGED);
main.registerReceiver(receiver, filter); //注册监听
}
2、打印
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();
function print(list) {
const mac_address = uni.getStorageSync("macAddress");
const reg = /[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}:[A-Fa-f0-9]{2}/;
let resMsg = {
success: false,
message: ""
};
let that = this;
if (!mac_address || !reg.test(mac_address)) {
resMsg.message = '请选择蓝牙打印机';
return resMsg;
}
uni.showLoading({
title: '打印中...',
mask: true
})
let device = BAdapter.getRemoteDevice(mac_address);
plus.android.importClass(device);
let bluetoothSocket = device.createInsecureRfcommSocketToServiceRecord(uuid);
plus.android.importClass(bluetoothSocket);
if (!bluetoothSocket.isConnected()) {
try {
bluetoothSocket.connect();
} catch (e) {
uni.hideLoading();
resMsg.message = '打印机连接失败';
return resMsg;
}
}
if (bluetoothSocket.isConnected()) {
var outputStream = bluetoothSocket.getOutputStream();
plus.android.importClass(outputStream);
list.forEach(t => {
var bytes = plus.android.invoke(t.data, 'getBytes', 'UTF-8');
outputStream.write(bytes);
});
outputStream.flush();
device = null //这里关键
bluetoothSocket.close(); //必须关闭蓝牙连接否则意外断开的话打印错误
uni.hideLoading();
resMsg.success = true;
resMsg.message = '数据发送成功';
return resMsg;
}
}
3、调用方法打印
uni.openBluetoothAdapter({
success(res) {
let list = [];
//打印ZPL命令数据,XA开始XZ结束,可多个拼接到一起为一条数据。
let ss = "^XA^MNW^JUS^CI28";
ss += "^FO0,45^A@N,36,18,E:MSUNG24.FNT^FD箱码:^FS";
ss += "^FO90,25^JMA^MD20^BY2,3,320^BCN,60,Y,N,N^FD1234567890^FS";
ss += "^FO0,100^A@N,36,18^FD类型:直通^FS";
ss += "^PQ1,0,1,Y";
ss += "^XZ";
//拼接多个打印数据
ss = ss + ss;
list.push({id: 1,data: ss});
list.push({id: 2,data: ss + ss});
let msg = print(list);
if (msg.success){
console.log("成功")
}
},
fail(e){}
})