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();
createInsecureRfcommSocketToServiceRecordwas 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:Weirdly, trying to connect by
createRfcommSocketToServiceRecordagain and again, in short intervals, did sometimes work.Another thing to note: The use of
createInsecureRfcommSocketToServiceRecordrequires API level 10.