YhXcorLFNR xmlns:o="urn:schemas-microsoft-com:office:office" xmlns="http://www.w3.org/TR/REC-html40">

连接原理及示例代码

   AndroidPrinterSDK 提供通过 BluetoothWIFIUSB 三种方式和打印机进行连接的方法。 具体调用的方法详见 AndroidPrinterDemo com.android.print.demo 包下的 BluetoothOperation.javaWifiOperation.javaUsbOperation.java 三个类中。

Bluetooth 为例 :

PrinterInstance mPrinter=new BluetoothPort().btConnnect(mContext, mac, adapter, mHandler);

1 mContext 为当前上下文对象,类型为Context

2 adapter 为蓝牙适配器

BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();

3 mac 为目标设备的蓝牙地址,可通过扫描附近蓝牙的方式获得。

adapter.startDiscovery();

扫描前需注册 BroadcastReceiver ,详见 BluetoothDeviceList mReceiver 的注册与使用,当扫描到周围设备时,device.getAddress() 即可获得。

4 mHandler 为消息接收器。需自定义。详见 MainActivity.java 中的 mHandler。 设定此 handler 的作用时,当连接成功时,SDK 会发送固定消息给此 handler,以便识别连接状态和更新UI

 

Connection Theory and Sample Code

AndroidPrinterSDK supplies three ways ?C Bluetooth, WIFI, USB to connect with the printer. The detailed methods can be referred to three classes ?C “BluetoothOperation.java”, “WifiOperation.java”, UsbOperation.java” in “com.android.print.demo” of “AndroidPrinterDemo”.

Take Bluetooth as the example:

PrinterInstance mPrinter=new BluetoothPort().btConnnect(mContext, mac, adapter, mHandler);

 

1 mContext is the object of current context. Type is “Context”

 

2 adapter is Bluetooth adapter

BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();

 

3 mac is the Bluetooth address of target device. It can be gained by scanning the nearby Bluetooth.

adapter.startDiscovery();

BroadcastReceiver needs to be registered before scanning. Pls refer to the details of registration and using of “myReceiver” in “BluetoothOpreation.java”. When the surrounding device is scanned, device.getAddress() can be gained.

4 mHandler is message receiver. It needs to be user-defined. The details can be checked in “mHandler” of “MainActivity.java”. After the function of this “handler: is set, when the connection is successfully, SDK will send the fixed message to this “handler to identify the connection status and update UI.