我的Android应用程序需要连接到蓝牙打印机(Zebra Bt打印机)来打印票据,这台打印机 doesn't require authentication (it's set Authentication:OFF) ,但我无法创建与它的连接,因为应用程序要求通过输入一个引脚进行配对,我尝试过默认值,如0000和1234但其中任何一个都有效 .
这是我用来与打印机 Build 连接的代码:
Set<BluetoothDevice> devices = bluetoothAdapter.getBondedDevices();
for (BluetoothDevice device : devices) {
if (device.getAddress().equals(PRINTER_DEVICE_MAC_ADDRESS)) {
bluetoothDevice = bluetoothAdapter.getRemoteDevice(PRINTER_DEVICE_MAC_ADDRESS);
if (bluetoothDevice != null) {
try {
clientSocket = bluetoothDevice.createRfcommSocketToServiceRecord(uuid);
clientSocket.connect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
当它执行该行时:
clientSocket.connect();
在哪里出现用于输入引脚以与设备 Build 连接的对话框,但我不知道要输入什么代码或如何避免此对话框 .
- 我在Android 4.1.2中使用Galaxy Tab 3 - 在Api 14中开发
1 回答
Zebra提供Android SDK以与其打印机配合使用 . 具体来说,有一个BluetoothConnectionInsecure类,允许您连接到您的打印机,而无需提供配对信息:http://www.zebra.com/us/en/products-services/software/link-os/link-os-sdk.html . JavaDoc中包含完整示例 .
如果您不能使用SDK,那么BitBank建议使用CreateInsecureRfCommSocket()是一个不错的选择 . 以下是一些有关其用法的文章:
http://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#createInsecureRfcommSocketToServiceRecord(java.util.UUID)
How to create Insecure RFCOMM Socket in Android?