Alex Rivera | Logout

Use a handheld bluetooth printer with android

Asked 2011-11-09T14:28:48.427
37

I have a bluetooth Handheld printer that I am able to communicate to using a SPP connection from my Mac(using Coolterm). When I'm trying to do the same from Android (using platform 7) I am running into multiple issues:

  • The printer doesn't seem to support/need PIN Authentication. When connecting from OSX, I just selected the option that said "Do not use a pin" and it got paired. In Android, when I use device.createRfcommSocketToServiceRecord(), it always ends up asking me for a PIN/Key(which I don't have/need). I solved this using the reflection trick:

    Method m = device.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class});
    BluetoothSocket connection = (BluetoothSocket) m.invoke(device, 1);
    

    I am unsure whether this actually worked, but the blinking LED on the printer stops blinking, which makes me believe it did.

  • Once I have the socket, I try to write byte data to the stream using:

    byte[] buffer = new byte[3];
    buffer[0] = (byte) 0x8A;
    buffer[1] = (byte) 0xC1;
    buffer[2] = (byte) 0x04;
    outStream.write(buffer);
    int response = inStream.read();
    mySocket.close();
    

    Sending the same three-byte sequence from Coolterm on OSX printed a test page from the printer. However, this seems to make the thread hang on Android(the read).

Is there something I am missing out here?

EDIT: This seems to work only when I set channel to 1. So that means I am on to something here.

Edit
Report

1 Answer

-6
answered 2011-12-08T06:04:44.577

Your Answer