I am trying to print to an HP DeskJet 450wbt printer from my T-Mobile Pulse Mini phone, using the Android Bluetooth API. The code is as shown below. The connection fails with "Service discovery failed". If I try the alternative method of creating a socket mentioned in a number of other threads, I get "Host is down" instead.
I think that the UUID for BPP is correct, but I am not sure. The printer is a paired device, and it is switched on. I cannot find a USB driver for my phone (a badged Huawei 8110), so I have not been able to debug on the device, or look at a log. I am stuck at this point, and I would be grateful for any advice.
Here is a synopsis of my code:
final String UUID_BPP = "00001122-0000-1000-8000-00805F9B34FB";
final String printerName = "dj450 S/N SG..."; // name of paired printer
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
return 2; // phone does not support Bluetooth
}
if (!bluetoothAdapter.isEnabled()) {
return 3; // Bluetooth has not been enabled
}
Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
// Loop through paired devices
BluetoothDevice printer = null;
for (BluetoothDevice device : pairedDevices) {
String s = device.getName();
if (s.equals(printerName)) {
printer = device;
break;
}
}
if (printer == null)
return 4; // Paired printer not found
// create socket
UUID BPP = UUID.fromString(UUID_BPP);
BluetoothSocket socket;
try {
socket = printer.createRfcommSocketToServiceRecord(BPP);
} catch (IOException e) {
return 5; // Unable to create socket
}
/*
try {
Method m = printer.getClass().getMethod("createRfcommSocket",
new Class[] { int.class });
socket = (BluetoothSocket) m.invoke(printer, 1);
} catch (Exception e1) {
return 5; // Unable to create socket
}
*/
try {
socket.connect();
} catch (IOException e) {
return 6; // Unable to connect socket
}