I am looking for a way to initiate the audio connection between the Android phone and my headset within my application. The idea is to simplify the connection process in such a way that the user doesn't have to go through the different settings menus anymore (Apps -> Settings -> Wireless & Networs -> Bluetooth settings).
Both devices are supposed to be already paired and the Bluetooth address of the headset to be known.
As far as I learned, the Bluetooth capabilities (available since version 2.0 of the Android SDK) are restricted to Bluetooth discovery and the connection of RFComm channels (host/client) between the Android phone and a Bluetooth device. Is there another way to request Bluetooth profiles on the Android system to initiate a connection to a known device from an app or is this impossible?
Have you looked at the BluetoothAdapter class? It seems like you can do everything you need via that class, unless I misunderstand the question.
As for initiating a connection to a specific device, won't the phone auto-pair with the known device once you enable bluetooth?
I'm using follow code to get IBluetooth service, and it has sevral function including connect headset.
IBinder b = ServiceManager.getService("bluetooth");
if (b != null) {
IBluetooth mBluetoothService = IBluetooth.Stub.asInterface(b);
}
But, these apis are hidden for app level, and they are different on different android sdk version.You must comply the code in android whole source enviroment, or use reflection to access them.
Related
I want to make my android device working as a bluetooth headset.
I search the Android APIs, but I just find some interface which
can make Android device working as a masters, not as a device(bluetooth headset).
I'm also ready to modify the source codes of Android OS, and rebuild the OS.
What I hope is, when Android devices connect to a mobile phone with bluetooth, the Android devices can work as a bluetooth headset.
I don't know whether there are interfaces can do this, or I should modify the Android OS?
Thank you!
You need to implement HFP profile in your android device, normally phone role is AG(Audio gateway) and headset roles is HF(Hands free) , think its as a server and client role. Connect RFCOMM channel to phone using UUID defined for HF , have SDP record updated,changes will be required to handle AT Commands as well.
These will be the initial changes, once done you will need to handle audio packets(SCO packets) for voice calls
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)
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.
My application needs to connect to a a2dp device over bluetooth and I want to "be able to query for the visible bluetooth devices, then, select a a2dp device and have it 'connect via a2dp' so that audio starts playing through the connected device" but my phone is running gingerbread (2.3.3).
I went through the basic bluetooth tutorial at http://developer.android.com/guide/topics/wireless/bluetooth.html and got to the part I need to connect to the bluetooth device and then I read the bottom of the page:
"Starting in Android 3.0, the Bluetooth API includes support for working with Bluetooth profiles." -> does this mean that I am S.O.L.? Is there any way to programmatically (why does stackoverflow mark programmatically as being misspelled?!) connect to a a2dp device using a pre-3.0 version of Android? Is my only option to direct the user to go into their settings/pull up the settings programmatically?? Because I'm able to do it through the settings, I guess I just assumed it would be possible via my application as well.
Help?
Some of the Bluetooth classes (profiles like BluetoothA2dp) are hidden in Gingerbread. It means that their declaration is annotated by #hide, and they are not included into the SDK (Android.jar). This is done intentionally, since these APIs are likely to be changed in newer Android versions. Generally it is not a good idea to use hidden APIs, since your App can stop working on newer Android versions, but if you are sure you want to, follow
http://devmaze.wordpress.com/2011/01/18/using-com-android-internal-part-1-introduction/
Once you get access to them do something like (just a hint):
BluetoothA2dp mBluetoothA2dp = new BluetoothA2dp(context);
BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().
// Loop through paired devices
for (BluetoothDevice device : mBluetoothAdapter.getBondedDevices()) {
if (device.getName().contains("whatyouwant")) {
mBluetoothA2dp.addSink(device);
}
}
So, after much more research, it seems that it is impossible to programmatically connect to a A2DP device on a pre-3.0 Android device. I am going mark this as the answer but, if someone finds otherwise, please correct me on this as I would really like to do it programmatically.
I want to initiate a pairing request (not connection at this point of time) to a non android device. This can be found working on Settings Application > Network > Bluetooth Settings > Click on a device after scan. A pop up appears on both the devices with a 6 digit pin.
As per Android's documentation this should have popped up in my application as well, if I do something like this
device.createRfcommSocketToServiceRecord(MY_UUID);
As per Android's API documentation
"Once a connection is made with a
remote device for the first time, a
pairing request is automatically
presented to the user."
And it is also mentioned that
"The current Android Bluetooth API's
require devices to be paired before an
RFCOMM connection can be established.
(Pairing is automatically performed
when you initiate an encrypted
connection with the Bluetooth APIs.)"
Even when I used the sample Bluetooth Chat application (only to test if it initiates a pairing on first connection) it didn't worked.
I also tried using a generic UDID like "00001101-0000-1000-8000-00805F9B34FB" but to no avail.
Couldn't find the source code of Settings Application (Android OS 2.1) to figure out myself. The sources available in Android GIT were for Android 2.3
Even if you cannot answer the question, only pointing out to Android Packages Settings App sources for Android 2.1 might do the trick for me.
It works for me with this UUID : "00001101-0000-1000-8000-00805F9B34FB".
The intent for pairing request is launched when you call socket.connect() if I remember correctly.