我正在尝试通过蓝牙打印到热敏打印机。
我能够在Nexus 7设备(第一代和第二代)上成功打印。但是,当我将完全相同的代码复制粘贴到其他应用程序上并将其部署在Asus Tablet上时,突然出现一个IOException
告诉我套接字可能已关闭。
这是我的代码:
public void onPrintReceipt(){
Toast.makeText(getApplicationContext(), "Printing", Toast.LENGTH_LONG).show();
try{
Set<BluetoothDevice> bdevices = bluetoothAdapter.getBondedDevices();
blueToothDevice = bluetoothAdapter.getRemoteDevice("00:01:90:EE:B2:52");
simpleComm(1);
}
catch(Exception ex){
Log.e("", "simpleComm() Catch Statement Entered");
}
}
protected void simpleComm(Integer port){
//InputStream tmpIn = null;
byte[] buffer = new byte[3]; //{65,65,53,53,49,52,65,66,67,68};
buffer[0] = (byte) 0x08;
buffer[1] = (byte) 0x99;
buffer[2] = (byte) 0x04;
OutputStream tmpOut;// = null;
bluetoothAdapter.cancelDiscovery();
Log.e(this.toString(), "Port = " + port);
try {
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
Method m = blueToothDevice.getClass().getMethod("createRfcommSocket", new Class[] { int.class });
socket = (BluetoothSocket) m.invoke(blueToothDevice, port);
// assert (socket != null) : "Socket is Null";
if(socket.isConnected()){
socket.close();
}
socket.connect();
try {
Log.e(this.toString(), "************ CONNECTION SUCCEES! *************");
try{
//tmpIn=socket.getInputStream();
tmpOut = socket.getOutputStream();
//mmInStream = tmpIn;
mmOutStream = tmpOut;
out = new BufferedWriter(new OutputStreamWriter(mmOutStream));
}
catch(Exception ex){
Log.e(this.toString(), "Exception " + ex.getMessage());
}
//TODO print sequence starts here
//....snip snip a LOT of code
}
finally{
mmOutStream.flush();
mmOutStream.close();
socket.close();
}
}
catch (IOException ex){
Log.e(this.toString(), "IOException: " + ex.getMessage());
}
catch (NoSuchMethodException ex){
Log.e(this.toString(), "NoSuchMethodException: " + ex.getMessage());
}
catch (IllegalAccessException ex){
Log.e(this.toString(), "IllegalAccessException: " + ex.getMessage());
}
catch (InvocationTargetException ex){
Log.e(this.toString(), "InvocationTargetException: " + ex.getMessage());
}
}
这是try-catch块中的错误:
IOException: read failed, socket might closed or timeout, read ret: -1
现在我很困惑为什么当我所做的只是将代码部署到另一台设备上时突然出现错误。
我该如何进行?
最佳答案
我看到您使用反射来创建RFCOMM连接。这非常危险,我最近在这方面回答了一个问题:How do Bluetooth SDP and UUIDs work? (specifically for Android)
Tl; dr:您正在绕过SDP查找机制,该机制将UUID映射到您连接到的设备上的相应蓝牙通道。您的代码始终连接到蓝牙通道1。这在最初/某些情况下可能有效-但也可能是您的问题。这取决于接收设备。
您正在创建一个UUID。我猜(希望)您有打印机文档中的内容吗?但是,如果您检查代码,就会发现您没有使用它来连接打印机-您绝对应该这样做(有关整个故事,请参阅链接的答案)。使用createRfcommSocketToServiceRecord(uuid)打开您的套接字。
由于您使用的是Nexus 7:我使用了两个Nexus 7,一个Nexus 4和其他10个设备来测试我编写的bluetooth middleware for android。尤其是Nexus设备非常敏感,当许多并发呼叫给蓝牙造成压力时,导致一个完全没用的蓝牙适配器,直到我重新启动它们为止。另外,我想记得当我使用这个骇人的反射代码片段填补了bt-adapter的频道,直到没有人导致蓝牙完全失效时,我才发现一个错误(我找不到与相关官员的链接android错误报告,但其中有一个适用于Nexus设备)。
关于java - 蓝牙打印会在不同设备上从Socket引发IOException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30857058/