【发布时间】:2015-12-11 02:49:21
【问题描述】:
我的蓝牙连接有问题,因为有时打印后无法正常工作!,我需要重新启动打印 Zebra im320 并重新连接和配对,这不稳定..
public void pairPrinter() {
final UUID SerialPortServiceClass_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
final BluetoothAdapter BA = BluetoothAdapter.getDefaultAdapter();
final String PrinterBsid = "AC:3F:A4:18:B2:B0";
Thread t = new Thread(new Runnable() {
@Override
public void run() {
OutputStream sOut;
BluetoothSocket socket;
BA.cancelDiscovery();
BluetoothDevice BD = BA.getRemoteDevice(PrinterBsid);
try {
socket = BD.createInsecureRfcommSocketToServiceRecord(SerialPortServiceClass_UUID);
BD.createInsecureRfcommSocketToServiceRecord(SerialPortServiceClass_UUID);
if (!socket.isConnected()) {
// Thread.sleep(2000);
socket.connect();
Thread.sleep(2000); // <-- WAIT FOR SOCKET
}
sOut = socket.getOutputStream();
String cpclData = "! 0 200 200 210 1\r\n"
+ "TEXT 4 0 30 40 This is a CPCL test.\r\n"
+ "FORM\r\n";
sOut.write(cpclData.getBytes());
sOut.flush();
sOut.close();
socket.close();
BA.cancelDiscovery();
} catch (IOException e) {
Log.e("", "IOException");
e.printStackTrace();
return;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t.start();
}
这是我的日志
E/BluetoothEventLoop.cpp﹕onDiscoverServicesResult: D-Bus 错误: org.bluez.Error.Failed (Connection timed out)
D/BluetoothService﹕清理失败的 UUID 通道查找:AC:3F:A4:18:B2:B0 00001101-0000-1000-8000-00805f9b34fb
E/: IOException
W/System.err: java.io.IOException: 服务发现失败
W/System.err: atandroid.bluetooth.BluetoothSocket$SdpHelper.doSdp(BluetoothSocket.java:465)
W/System.err:atandroid.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:230)
W/System.err:atcom.example.msanchez.printzebra.MainActivity$1.run(MainActivity.java:73)
W/System.err﹕在 java.lang.Thread.run(Thread.java:856)
【问题讨论】:
标签: java android android-studio bluetooth