我通常通过 Android 设置菜单配对设备,然后尝试以编程方式将其连接到打印机。我搜索配对的设备,尝试连接,然后它再次提示我输入密码。我试图避免每次建立连接时都必须输入密码。
以下是相关代码:
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
String defaultPrinter = UserSession.getDefaultPrinter( context );
BluetoothDevice preferredDevice = null;
if ( defaultPrinter == null ) {
//start activity regarding selecting a default printer
}
else {
for (BluetoothDevice device : pairedDevices)
if ( device.getAddress().equals( defaultPrinter ) ) {
preferredDevice = device;
break;
}
}
UUID id = UUID.fromString( BLUE_TOOTH_UUID );
try {
selectedDeviceSocket = preferredDevice.createRfcommSocketToServiceRecord( id );
selectedDeviceSocket.connect();
isConnected = true;
return selectedDeviceSocket.getOutputStream();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
只有当我重新输入正确的引脚时,才会发生连接。