蓝牙 多次打印总是报错
Uncaught java.io.IOException: read failed, socket might closed or timeout, read ret: -1;at android.bluetooth.BluetoothSocket.connect at printer.html:1
也代码控制了 还是报错
window.ConnectPrinter = function(bleId) {
if (device == null) {
var plusMain = plus.android.runtimeMainActivity(),
BluetoothAdapter = plus.android.importClass("android.bluetooth.BluetoothAdapter"),
UUID = plus.android.importClass("java.util.UUID"),
uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"),
BAdapter = BluetoothAdapter.getDefaultAdapter(),
device = BAdapter.getRemoteDevice(bleId);
plus.android.importClass(device);
var bluetoothSocket = device.createInsecureRfcommSocketToServiceRecord(uuid);
plus.android.importClass(bluetoothSocket);
BAdapter.cancelDiscovery();
}
// alert("取消搜索");
if (!bluetoothSocket.isConnected()) {
// alert("链接")
bluetoothSocket.connect();
mui.toast('打印机已就绪,可正常打印!');
}
this.gotoPrint = function(byteStr, options) {
var outputStream = bluetoothSocket.getOutputStream();
plus.android.importClass(outputStream);
if (byteStr) {
var bytes = plus.android.invoke(byteStr, 'getBytes', 'gbk');
if (options) {
mui.each(options, function(index, item) {
outputStream.write(item)
});
}
outputStream.write(bytes);
outputStream.flush();
} else {
if (options) {
mui.each(options, function(index, item) {
outputStream.write(item)
outputStream.flush();
});
}
c***@163.com (作者)
基本上都会,没有用 device == null 判断 ,重复初始化都会报错
Uncaught java.io.IOException: read failed, socket might closed or timeout, read ret: -1;at android.bluetooth.BluetoothSocket.connect at printer.html:1
加了控制要好一点,有时候还是会
2019-05-10 12:01