目前使用网上的示例,代码如下。有偿求助解决 谢谢各位大佬 现在代码不报错,mac地址写死 打印机也没反应 求解决方案
function PrintQrCode(mac_address,byteStr) {
if (!mac_address) {
alert('请选择蓝牙打印机')
return
}
let main = window.plus.android.runtimeMainActivity()
let BluetoothAdapter = window.plus.android.importClass("android.bluetooth.BluetoothAdapter")
let UUID = window.plus.android.importClass("java.util.UUID")
let uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")
let BAdapter = BluetoothAdapter.getDefaultAdapter()
let device = BAdapter.getRemoteDevice(mac_address)
window.plus.android.importClass(device)
let bluetoothSocket = device.createInsecureRfcommSocketToServiceRecord(uuid)
window.plus.android.importClass(bluetoothSocket)
if (!bluetoothSocket.isConnected()) {
console.log('检测到设备未连接,尝试连接....')
bluetoothSocket.connect()
}
console.log('设备已连接')
if (bluetoothSocket.isConnected()) {
var outputStream = bluetoothSocket.getOutputStream();
window.plus.android.importClass(outputStream);
// var writer = new OutputStreamWriter(outputStream, 'GBK');
// window.plus.android.importClass(writer);
// var moduleSize = 8
var bytes = window.plus.android.invoke(byteStr, 'getBytes', 'gbk')
// var bar = window.plus.android.invoke(byteStr, 'getBytes', 'gbk')
// var length = bytes.length
// console.log(length)
console.log(bytes)
console.log(outputStream)
outputStream.write(bytes);
outputStream.flush();
// outputStream.write(29);
// outputStream.write(72);
// outputStream.write(2);
// outputStream.flush(); //hri字符打印位置
// outputStream.write(29);
// outputStream.write(119);
// outputStream.write(2);
// outputStream.flush(); //条码宽度
// outputStream.write(29);
// outputStream.write(104);
// outputStream.write(81);
// outputStream.flush(); //条码高度
// outputStream.write(29);
// outputStream.write(107); //打印条码
// outputStream.write(73); // 条码类型 code128
// outputStream.write(15); // 条码位数
// outputStream.write(123);
// outputStream.write(66); //code128 的子类型,有128a,128b,128c
// outputStream.write(bar);
// outputStream.flush();
// outputStream.flush();
console.log('打印完成')
// device = null
// bluetoothSocket.close();
}
}
1 个回复
x***@163.com
解决不了 是不是也是报连接不上的错误?