4

zebraRW420プリンターを使用してラベルを印刷するための簡単なAndroidアプリケーションの開発を開始することを意味します。彼らはAndroid用のSDKを持っていますが、どこから始めればよいのかわかりません。

私は単一の画面でアプリを想像します。それはラベルまたは発注書、印刷したい部数、そして印刷ボタンを要求します。誰かが私が始めるのを手伝ってくれますか?どんな助けでもありがたいです。

プリンターを検索するにはどうすればよいですか?SDKが検索してくれますか?または、AndroidのBluetoothAdapterクラスを使用する必要があります...ラベル形式については、自分で作成する必要がありますか、それともSDKの既存のファイルを使用できますか?

複数部印刷するようにプリンターを設定できますか?

4

2 に答える 2

8

SDKは、Bluetooth検出クラスを提供します。ドキュメントと例を見てください。BluetoothDiscovererクラスをチェックしてください。これは、すべてのBluetoothデバイスを検索し、ハンドラーで通知する方法を提供します。ディスカバリーを実行する方法の例を含むSDKに付属する開発者デモがあります。

    try {
        BluetoothDiscoverer.findPrinters(this, new DiscoveryHandler() {

            public void foundPrinter(DiscoveredPrinter printer) {
                String macAddress = printer.address;
                //I found a printer! I can use the properties of a Discovered printer (address) to make a Bluetooth Connection
            }

            public void discoveryFinished() {
                //Discovery is done
            }

            public void discoveryError(String message) {
                //Error during discovery
            }
        });
    } catch (ConnectionException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
于 2012-09-28T13:59:28.607 に答える
0

1)次のようなスレッドから印刷する必要があります。

new Thread (new Runnable () {
  public void run () {
    enablePrintButton (false);
    Looper.prepare ();
    doConnectionTest ();
    Looper.loop ();
    Looper.myLooper ().quit ();
  }
}).start ();

2)次に:

private void doConnectionTest () {
    printer = connect ();

    if (printer != null) {
      //print
    } else {
      disconnect ();
    }
  }

3)印刷方法:

private void sendTestLabel () {
    try {
      ZebraPrinterLinkOs linkOsPrinter = ZebraPrinterFactory.createLinkOsPrinter (printer);

      PrinterStatus printerStatus =
          (linkOsPrinter != null) ? linkOsPrinter.getCurrentStatus () : printer.getCurrentStatus ();

      if (printerStatus.isReadyToPrint) {
        byte[] configLabel = getConfigLabel ();
        connection.write (configLabel);
        setStatus ("Sending Data", Color.BLUE);
      } else if (printerStatus.isHeadOpen) {
        setStatus ("Printer Head Open", Color.RED);
      } else if (printerStatus.isPaused) {
        setStatus ("Printer is Paused", Color.RED);
      } else if (printerStatus.isPaperOut) {
        setStatus ("Printer Media Out", Color.RED);
      }
      DemoSleeper.sleep (1500);
      if (connection instanceof BluetoothConnection) {
        String friendlyName = ((BluetoothConnection) connection).getFriendlyName ();
        setStatus (friendlyName, Color.MAGENTA);
        DemoSleeper.sleep (500);
      }
    } catch (ConnectionException e) {
      setStatus (e.getMessage (), Color.RED);
    } finally {
      disconnect ();
    }
  }

4)ラベルの設定方法:

private byte[] getConfigLabel () {
     byte[] configLabel = null;

     try {
       PrinterLanguage printerLanguage = printer.getPrinterControlLanguage ();
       SGD.SET ("device.languages", "zpl", connection);

       if (printerLanguage == PrinterLanguage.ZPL) {
         configLabel = "^XA^FO17,16^GB379,371,8^FS^FT65,255^A0N,135,134^FDTEST^FS^XZ".getBytes();
       } else if (printerLanguage == PrinterLanguage.CPCL) {

         String msg = setPopratnica ();

         msg = format (msg);

         configLabel = msg.getBytes ("windows-1250");
       }
     } catch (Exception e) {
     }
     return configLabel;
   }

5)接続方法:

public ZebraPrinter connect () {
     setStatus ("Connecting...", Color.YELLOW);
     connection = null;
     connection = new BluetoothConnection (getBluetoothAddressKey ());
    try {
       connection.open ();
       setStatus ("Connected", Color.GREEN);
         } catch (ConnectionException e) {
           setStatus ("Comm Error! Disconnecting", Color.RED);
           DemoSleeper.sleep (1000);
           disconnect ();
         }

         ZebraPrinter printer = null;

         if (connection.isConnected ()) {
           try {
             printer = ZebraPrinterFactory.getInstance (connection);
             setStatus ("Determining Printer Language", Color.YELLOW);
             String pl = SGD.GET ("device.languages", connection);
             setStatus ("Printer Language " + pl, Color.BLUE);
          } catch (ConnectionException e) {
             setStatus ("Unknown Printer Language", Color.RED);
             printer = null;
             DemoSleeper.sleep (1000);
             disconnect ();
           } catch (ZebraPrinterLanguageUnknownException e) {
             setStatus ("Unknown Printer Language", Color.RED);
             printer = null;
             DemoSleeper.sleep (1000);
             disconnect ();
           }
         }
         return printer;
}

6)切断方法:

public void disconnect () {
    try {
      setStatus ("Disconnecting", Color.RED);
      if (connection != null) {
        connection.close ();
      }
      setStatus ("Not Connected", Color.RED);
    } catch (ConnectionException e) {
      setStatus ("COMM Error! Disconnected", Color.RED);
    } finally {
      enablePrintButton (true);
    }
  }

さらに、寝台車のクラスが必要です:

public class DemoSleeper {
    private DemoSleeper () {
    }

    public static void sleep(int ms) {
        try {
            Thread.sleep(ms);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
于 2017-03-02T14:31:43.370 に答える