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();
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.
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!)
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.
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.
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)
Related
Can an Android device pretend and connect as a Bluetooth controller?
Can Android devices connect as a device to, say, a PC and act as a gamepad or similar device?
Is it allowed and doable in code without root access?
I tried this code: Connect Bluetooth devices. But it can not get it to pair.
Short answer, yes.
I recommend looking at Google's example project on Bluetooth communication BlueToothChat initially. Based on what you've written, I think you're missing several important steps.
You need all of the following components / steps:
General Overview
Set up Bluetooth
Set Permissions
Scan for other Bluetooth devices
Query the local Bluetooth adapter for paired Bluetooth devices
Connect to a remote device as client or server
Transfer data over Bluetooth
If you have access to the device your attempting work with as a controller, then you can create your own communication setup. Define a set of parameters to write and read on both sides that emulate the functionality you're looking for.
If you do not have access to creating your own read/write setup, or you want to use standard interaction methods for the industry, then the best bet is the Bluetooth HID Device profile mentioned by #Morrison Chang.
The HID profile basically establish an industry standard of required device features, read/write formats, combinations of features equaling a controller type (ie: "this is a keyboard"), and data mappings for what read/write numbers mean on either side of the client / server connection.
From the linked comment thread, the Kontroller project on Github has source available for your peruse.
There's also a reasonable Intro to Bluetooth HID that covers some of the topics on what Bluetooth HID is actually doing.
Finally, also recommend looking at Google's own BluetoothHidDevice page.
I need to estabilish a brief communication between a BLE master and a BLE peripherical device.
Until I have to use the mobile phone I have no problems, since I select the bluetooth device from Android, I can connect to it and then I can receive data simply writing them to HM-10.
BUT, this approach is battery expensive, since the BLE scanning is more battery expensive than the BLE advertising (right?). So I want that the mobile becames an advertisor and the HM-10 takes the ROLE of Slave.
BUT, I still don't understand if it is possible to connect to other devices: I tried AT+CONN and it always fails (AT+CONF). Is there any way to send brief data from the HM-10 to the advertisor, even without connecting?
Even my firmware looks slightly different from the readme! For example,AT+LN is present in the documentation but it appears to not exist. Does a better version of the firmware for this device exist, or, are sources available somewhere? I installed the HM-Soft V705 (lastest).
Thanks
UPDATE 1:
#ChrisStratton Ok, I have some intresting news: I was already following your idea (phone advertise sometimes, while HM-10 always scan and tries to connect to the service if it sees it, then HM-10 send some data, phone answer). I managed to make the phone visible from HM-10 (don't ask me how!). I am using the BLE Tool Android app to test everything. Now, the central problem now is that HM-10 won't connect to the android service even if the advertisor is connectable (see attached images).
I am sospicious that the AT+CO (that I discovered is a kind a replacement of AT+LN, but I am not sure) can help, but i don't know how.
I'm asking because I've been facing that issue for weeks.
I need to develop an Android application that can be able to perform an Bluetooth Connection and send data to an Bluetooth Hardware.
Ok, so here's the point:
is it possible to perform a RFCOMM connection to an 'unknown' device?
I mean, is it possible to do this without having any information about the hardware code?
Because i'm only able to modify the AndroidDevice (cellphone) code, for SENDING purposes.
I want to send a byte and make sure it was received, but there's only a sending code.
Could we have some more details? Is the other device accessible by you, as in can the other device know information about the Android device? And what do you mean by a "sending code"?
The Android device can pick up any active Bluetooth device in its range, and know the name and address of those devices. So, if the Android device doesn't know anything about the Bluetooth device it wants to connect to, you can always sort through the list of devices in range, and get its hardware code through that.
See the documentation about Bluetooth, specifically the Finding Devices section:
http://developer.android.com/guide/topics/connectivity/bluetooth.html
I've done a fair amount of Bluetooth work with Android devices and Arduinos, and the Bluetooth library might be a bit of a pain to work with, but it is powerful.
With bluetooth the two devices have a UUID. If you a writing a program that runs on both devices you have them listen for that UUID. When they connect as a client or a server you have a dataInputStream and a dataOutputStream. Then you can push and pull bytes out of those.
I am trying to scan for bluetooth devices using a processing based android app. I would like to use processing to communicate to my microcontroller board via bluetooth as shown in the example below:
http://webdelcire.com/wordpress/archives/1045
I was successful in launching this app on my android tablet. However, I am not able to list the bluetooth device on my tablet. The only bluetooth device that my tablet recognizes is the bluetooth adapter on my computer. Why am I not able to list all the bluetooth devices? My computer is able to recognize the microcontroller's bluetooth device.
Probably your embedded target board identifies itself erroneously as a Bluetooth Device.
The computer's Bluetooth adapter accepts this error, while Android does not.
If you can change the SDP records (Bluetooth search discovery protocol) of the embedded target board, maybe Android will see the device (try making it look as a headset or hands free device!).
The quick way is to change the 'magic numbers' defining the SDP class, service id, etc. The numbers are inside this document. Then see if Android sees your board.
You can also read the Bluetooth Core Spec if you have the time.
See this tutorial for more on the formal aspects, and "Bluetooth for programmers" for a lighter introduction.
How can I connect and disconnect to/from a headset bluetooth device programmatically?
The application should work for Android 2.1+
In other words: I have a headeset. I can pair, connect, disconnect to it using Settings/Wireless and Networks/Blutooth Settings.
But how can I do all those things (pair, connect, disconnect) from my program?
Please chech this link.
The android bluetooth example (already listed) has a bunch of issues (not the least of which is you need 2 android devices to get it to function).
Take a look at the example at http://luugiathuy.com/2011/02/android-java-bluetooth/ where he is using bt on the android device to hit a server (to do some robotics work).
Be aware of UUID issues (the way that BT decides what connection it can make is defined in the UUID), and check out http://www.avetana-gmbh.de/avetana-gmbh/produkte/doc/javax/bluetooth/UUID.html
Keep in mind that the 16 bits represented in the UUID (ie, 0x1101 for Serial Port) is misleading in that its really the least significant part and needs to be coded 0x00001101.