我的安卓蓝牙连接有点问题。我正在使用蓝牙在 Zebra MZ320 打印机上进行打印。到目前为止,我的任何手机都没有任何问题(在三星 Galaxy Mini、三星 Galaxy Ace、三星 Galaxy Nexus S、三星 Galaxy S2、三星 Galaxy Nexus S、三星 Galaxy Note、HTC Desire Z 上打印没有任何问题, HTC Desire S、HTC Desire、HTC Legend、HTC Flyer、三星 Galaxy Tab),但最近我得到了无法打印的三星 Galaxy XCover。
经过一天的研究,我做了这个程序:
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button bTest = (Button)findViewById(R.id.button1);
bTest.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (new File("/sdcard/logDat.txt").exists()) {
new File("/sdcard/logDat.txt").delete();
}
Toast.makeText(TestPrinterActivity.this, "abcd1234", Toast.LENGTH_SHORT).show();
String macAddr = "00:22:58:07:BD:A5";
//sendCpclOverBluetooth(macAddr);
BluetoothSocket bSocket = null;
OutputStream outStream = null;
InputStream inStream = null;
try {
log("try... \tMAC address: "+macAddr);
BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(macAddr);
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] { Integer.TYPE });
//Method m = device.getClass().getMethod("createInsecureRfcommSocket", new Class[] { Integer.TYPE });
bSocket = (BluetoothSocket)m.invoke(device, new Object[] { Integer.valueOf(1) });
//bSocket = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(macAddr).createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
//bSocket = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(macAddr).createInsecureRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
log("\tconnect()");
bSocket.connect();
log("\tgetOutputStream");
outStream = bSocket.getOutputStream();
log("\tgetInputStream");
inStream = bSocket.getInputStream();
String cpclData = "! U1 SETLP ARI08ITA.CPF 0 20\r\n test123\r\n";
// Send the data to printer as a byte array.
log("\twriting: "+cpclData);
outStream.write(cpclData.getBytes());
log("\tflush");
outStream.flush();
Sleeper.sleep(1000);
log("\tfinish1");
} catch (Exception e) {
log("\t\tERROR: "+e.getMessage());
e.printStackTrace();
Toast.makeText(TestPrinterActivity.this, "ERROR: "+e.getMessage(), Toast.LENGTH_SHORT).show();
} finally {
try {
log("\tbSocket.close");
bSocket.close();
log("\toutStream.close");
outStream.close();
log("\tinStream.close");
inStream.close();
} catch (Exception e) {
log("\t\tERROR while closing: "+e.getMessage());
e.printStackTrace();
Toast.makeText(TestPrinterActivity.this, "ERROR while closing: "+e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
log("endTry");
}
});
}
private void log(String str) {
if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED ) == false)
return;
try {
BufferedWriter buffWr = new BufferedWriter(new FileWriter("/sdcard/logDat.txt", true));
buffWr.write(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + ": "+ str);
buffWr.newLine();
buffWr.close();
} catch(Exception e) {
Toast.makeText(TestPrinterActivity.this, "ERROR writing to log file: "+e, Toast.LENGTH_SHORT).show();
}
}
以下是正确和错误的输出。每个输出包含两个场景(打印机打开和关闭)。
似乎在三星 Galaxy XCover 蓝牙上总是成功连接到打印机,即使它处于脱机状态......
任何建议做什么/尝试将不胜感激。
---------------------------------------------------------------------
When printer is turned on:
---------------------------------------------------------------------
2012-07-25 14:53:36: try... MAC address: 00:22:58:07:BD:A5
2012-07-25 14:53:36: connect()
2012-07-25 14:53:43: getOutputStream
2012-07-25 14:53:43: getInputStream
2012-07-25 14:53:43: writing: ! U1 SETLP ARI08ITA.CPF 0 20
test123
2012-07-25 14:53:43: flush
2012-07-25 14:53:44: finish1
2012-07-25 14:53:44: bSocket.close
2012-07-25 14:53:44: outStream.close
2012-07-25 14:53:44: inStream.close
2012-07-25 14:53:44: endTry
---------------------------------------------------------------------
When printer is off
---------------------------------------------------------------------
2012-07-25 14:53:07: try... MAC address: 00:22:58:07:BD:A5
2012-07-25 14:53:07: connect()
2012-07-25 14:53:12: ERROR: Host is down
2012-07-25 14:53:12: bSocket.close
2012-07-25 14:53:12: outStream.close
2012-07-25 14:53:12: ERROR while closing: null
2012-07-25 14:53:12: endTry
Output from Samsung Galaxy XCover (2.3.6):
---------------------------------------------------------------------
When printer is turned on:
---------------------------------------------------------------------
2012-07-25 15:00:48: try... MAC address: 00:22:58:07:BD:A5
2012-07-25 15:00:48: connect()
2012-07-25 15:00:48: getOutputStream
2012-07-25 15:00:48: getInputStream
2012-07-25 15:00:48: writing: ! U1 SETLP ARI08ITA.CPF 0 20
test123
2012-07-25 15:00:48: flush
2012-07-25 15:00:49: finish1
2012-07-25 15:00:49: bSocket.close
2012-07-25 15:00:49: outStream.close
2012-07-25 15:00:49: inStream.close
2012-07-25 15:00:49: endTry
---------------------------------------------------------------------
When printer is off
---------------------------------------------------------------------
2012-07-25 15:01:25: try... MAC address: 00:22:58:07:BD:A5
2012-07-25 15:01:25: connect()
2012-07-25 15:01:25: getOutputStream
2012-07-25 15:01:25: getInputStream
2012-07-25 15:01:25: writing: ! U1 SETLP ARI08ITA.CPF 0 20
test123
2012-07-25 15:01:25: flush
2012-07-25 15:01:26: finish1
2012-07-25 15:01:26: bSocket.close
2012-07-25 15:01:26: outStream.close
2012-07-25 15:01:26: inStream.close
2012-07-25 15:01:26: endTry