Barry Klecka
Barry Klecka

Reputation: 73

Programmatically connecting to a Bluetooth enabled barcode scanner in Android

I am working to create an Android App that connects to a bluetooth barcode scanner. I've been looking for code examples of how to do this but I can not find any dealing with connecting to a device. I see lots for connecting peer-to-peer with android devices but that doesn't seem to cover android to device.

This is the code I have so far. It fails on the call to connect with a Connection refused.

UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
socket = mDevice.createRfcommSocketToServiceRecord(uuid);
socket.connect();

06-11 15:29:10.113: W/System.err(20018): java.io.IOException: Connection refused
06-11 15:29:10.133: W/System.err(20018):    at  android.bluetooth.BluetoothSocket.connectNative(Native Method)
06-11 15:29:10.133: W/System.err(20018):    at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:212)

The device is paired with the android phone and I retrieve it from the phone using this.

Set<BluetoothDevice> pairedDevices = btAdapter.getBondedDevices();

Upvotes: 2

Views: 8349

Answers (5)

Albert Kim
Albert Kim

Reputation: 11

I had same problem and solved it. First, change your barcode scanner mode to SSP from HID. HID(Human Interface Mode) is just for hardware keyboard mode.

Next, turn off your bluetooth hardware from Input device. Settings > Bluetooth > bluetooth barcode canner setting > uncheck Input device

Use UUID UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")

That's it.

Upvotes: 1

user1981175
user1981175

Reputation: 16

createInsecureRfcommSocketToServiceRecord was a great hint.

I had problems connecting to a Bluegiga WT32-A Bluetooth module. My old code was based on Bluetooth Chat example (which worked fine with an BTM 222 Bluetooth module) and used createRfcommSocketToServiceRecord, with the result of the connection being refused:

java.io.IOException: Connection refused

Weirdly, trying to connect by createRfcommSocketToServiceRecord again and again, in short intervals, did sometimes work.

Another thing to note: The use of createInsecureRfcommSocketToServiceRecord requires API level 10.

Upvotes: 0

Barry Klecka
Barry Klecka

Reputation: 73

I did find a fix to this issue, though I am not sure as to why this one works and the other did not.

UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
socket = mDevice.**createInsecureRfcommSocketToServiceRecord(uuid);**
socket.connect();

Changing to use the Insecure call allows the connect to work and I can receive data back from socket read calls.

Upvotes: 2

Julian Higginson
Julian Higginson

Reputation: 547

Another possibility - posting as a 2nd answer because it's very different from my last.

You've only given info on something you've planned to do technically so far, not the actual purpose of the app you are trying to write.

If the purpose of your app isn't just an exercise in bluetooth connectivity or just about using a particular barcode reader, and getting the barcode value into the android device is just one step in a bigger purpose - have you considered using the device's camera for that job instead of a barcode reader?

I believe there are libraries that can handle this, or DIYing your own image processing code could be fun (I know there's existing apps that do it already, so it's possible some way or other)

Upvotes: 0

Julian Higginson
Julian Higginson

Reputation: 547

This ID will only work if the barcode scanner offers a standard bluetooth SPP interface. Are you sure this device even uses that UUID?

Go and look for any documentation for connecting this scanner to something - even just a PC. And go through it, even though it's not what you're trying to do right now. You might learn something about the system or get ideas about how it actually works.

For instance, the presence of a special driver install for PC to make it do anything will point to it not being an SPP profile. Alternatively, if there's a PC application for working with the scanner that connects to a "serial port" to work, then it's pretty much definitely an SPP profile.

In the case of it not being an SPP profile, having a working connection to something like a PC at least gives you a chance to sniff the connection and maybe also data transfer, to reverse engineer something for android.

Also, with a baseline check that the hardware works for one specific application, you can rule out hardware fault. (check your android device's bluetooth link while you're at it!)

Upvotes: 1

Related Questions