首先需要一個jar包,bluesdk,請自行百度。
具體排版樣式跟網絡打印機打印排版樣式實現一樣,這里不多敘述,只貼一個實現方法代碼。藍牙打印機使用前需要先跟手機配對,可以保存在本地,記錄下地址,這里可以看到我每次調用這個方法 讓線程休息了1s,原因是通過測試發現,當數據量特別大的時候,反復調用這個方法會出現丟失數據的現象,所以,每次執行完畢,都讓他休息1s鍾。
public void printData(String data) { if (Settings.BluetoothDeviceAddress.equals("")) { MyToast.MyLogo(History.this, "打印錯誤,請檢查打印機或重試"); return; } System.out.println("Settings.BluetoothDeviceAddress:" + Settings.BluetoothDeviceAddress); try { Settings.BluetoothManager.setServerAddress(Settings.BluetoothDeviceAddress); if (!Settings.BluetoothManager.IsConnected()) Settings.BluetoothManager.ConnectServer(); if (!Settings.BluetoothManager.IsConnected()) { MyToast.MyLogo(History.this, "打印錯誤,請檢查打印機或重試"); } else { Settings.BluetoothManager.PrintData(data); } } catch (Exception e) { MyToast.MyLogo(History.this, "打印錯誤,請檢查打印機或重試"); e.printStackTrace(); try { Settings.BluetoothManager.shutdownServer(); Settings.BluetoothManager.ConnectServer(); Settings.BluetoothManager.PrintData(data); } catch (IOException e1) { MyToast.MyLogo(History.this, "打印錯誤,請檢查打印機或重試"); e1.printStackTrace(); return; } } try { // 每次打印后休息1s Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }