首先需要一个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(); } }
时间: 2024-10-19 13:15:09