Android調用藍牙打印機


首先需要一個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();
    }
  }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM