Bluetooth connection between Android and another phone over the Handsfree profile - android

I'm trying to use my Android phone as a handsfree kit (like the one for cars) in order to connect to another phone (any phone) and perform some handsfree functionality like (answer an incoming call, reject,.. etc) which can be done using the AT commands for handsfree profile.
For that, I'm using the well-known Bluetooth chat App, and reflection work around in order to establish a connection with any device:
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
tmp = (BluetoothSocket) m.invoke(device,1);
However, in order to achieve the handsfree functionality and understand the AT commands that I'm sending, the connected phone needs to be over the handsfree profile which uses the UUID: "0000111f-0000-1000-8000-00805F9B34FB"
Therefore, is there a way to achieve a connection to the handsfree profile?
Thanks!

You should only use this code when you have no other choice. The 1 in this code is the RFCOMM port. Each service has it's own RFCOMM port. This port is usually random between 1 and 31. You need to know which port the service (here handsfree profile) is using on the device that you want to connect to. You have to use the createRfcommSocketToServiceRecord method from the BluetoothDevice object to do this:
try { clientSocket = bluetoothDevice.createRfcommSocketToServiceRecord( serviceUUID ); }
catch (IOException e)
{
// handle error
}
This code is the correct way to use Bluetooth and should replace the one you're using.

Related

How do Bluetooth SDP and UUIDs work? (specifically for Android)

My understanding is that the SDP is a list of UUIDs that other devices can fetch.
According to this PDF from MIT, "A more general way to think of
SDP is as an information database." Does this mean I can add multiple values to SDP? Since Android has BluetoothDevice.fetchUuidsWithSdp(), how do I set the UUIDs of a device?
Also, what does each section of an UUID mean? UUIDs look like 00000000-0000-1000-8000-00805F9B34FB, but what information does this convey?
An UUID identifies a service that is available on a particular device. So if you call BluetoothDevice.fetchUUidsWithSdp() your BroadcastReceiver will receive the relevant Intent ACTION_UUID containing the device and the service UUID.
The bluetooth specification defines some common UUIDs.
If you don't want to connect to one of these well known services but intent to implement your own bluetooth application, then you have to just generate your own UUID (use uuidgen from a unix console or an online generator) that identifies your application/service.
You can create an UUID instance in java like this UUID uuid = UUID.fromString("785da8ea-1220-11e5-9493-1697f925ec7b");.
So if you create the server side for your bluetooth application on Android you typically do this
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
BluetoothServerSocket serverSocket = adapter.listenUsingRfcommWithServiceRecord("YourHumanReadableServiceName", uuid);
And this is where you "set" your UUID. The Android bluetooth API creates the SDP-entry consisting of YOUR application's UUID and name for you. Other devices can now retrieve this entry. Androids bluetooth stack will now associate a bluetooth channel to your BluetoothServerSocket. If you want to connect to this ServerSocket, the connecting side usually connects doing this:
// you will most likely already have this instance from a discovery or paired device list
BluetoothDevice serverDevice = adapter.getRemoteDevice(bluetoothMacAddress);
// connect to your ServerSocket using the uuid
BluetoothSocket socket = serverDevice.createRfcommSocketToServiceRecord(uuid);
socket.connect();
Android will again do the heavy lifting for you: It checks the SDP-Records on the remote device, looks up the bluetooth channel that corresponds to your service's UUID and connects using this information.
There is a common code snippet spooking around here on SO that advices you to use "reflection" to get to a hidden API looking similar to this code:
try {
// this is the way to go
socket = device.createRfcommSocketToServiceRecord(uuid);
socket.connect( );
} catch ( IOException exception ) {
// don't do that! You will bypass SDP and things will go sideways.
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
socket = (BluetoothSocket) m.invoke(device, 1);
socket.connect();
}
Most people try this and it "just works" in their dev environment but you should know what you do using this. You actively bypass the SDP lookup that retrieves the right bluetooth channel to be used with your service and you will end up connecting to channel 1. If you have more than one Service running on the device, things WILL go sideways in this cases and you will end up in debugging hell ;-)
I developed a small middleware called Blaubot to create small networks using bluetooth/wifi/nfc and experienced all sorts of problems on the devices I used to test with (12 models). It was often the case that the bluetooth stack was not fully functional anymore in cases where it got some load or after many connects/disconnects (which you usually will have, if you are developing your app). In these cases the device.createRfcommSocketToServiceRecord(uuid) would occasionally fail and only turning the bluetooth adapter off and on again helped to bring the bluetooth adapters back to life (in some cases only after a full power cycle). If this happens and you use the reflection method, you will probably not have much fun with bluetooth.
But if you know this and keep concurrent calls to the BluetoothAdapter within bounds, bluetooth connections and the adapters will be pretty stable.

How to connect ble device without scanning? Android 4.3

I try to develop a simple android app with a ble device.
I found many source code from the Intenet. However, both of them were start from scanning a list of available ble device.
As I have MAC address of the device,UUID of service and characteristic.
How can I connect to the known device and read one specific characteristic directly??
To connect a specific Bluetooth Device which has details like MAC address of the device, UUID of service and characteristic etc. you already know. To do this you need a BluetoothDevice object to make a call like this:
yourBluetoothDevice.connectGatt(getApplicationContext(), false, bleGattCallback);
So for this (yourBluetoothDevice) you have to start the scan at first to get same device object to connect by comparing it's MAC address. However, you got that same device object in onLeScan callback just stop scanning and make a connection with the same device.
Note: Making a connection should be on UIThread(Either using Handler, runOnUIThread or mainlooperThread) otherwise it will give issue in some devices for 'Fail to register callback'
Here yourBluetoothDevice is the device object reference with you want to make a connection.
bleGattCallback is the registered new BluetoothGattCallback() callback for connection status, discovered services, characteristics read and write etc.
Take a look at createInsecureRfcommSocketToServiceRecord. Something like this:
UUID uuid = UUID.fromString("<Your UUID>");
BluetoothSocket socket = yourBLEDevice.createInsecureRfcommSocketToServiceRecord(uuid);
Method m = yourBLEDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
socket = (BluetoothSocket) m.invoke(yourBLEDevice, 1);
If you already know the mac address, you can try something like below:
final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(mMacAddress);
final BluetoothGatt mGatt = device.connectGatt(getApplication(), false, gattCallback);

Is it possible to make PandaBoard as Bluetooth Headset

I have a requirement to make Pandaboard as Bluetooth Headset. Is it possible to achieve this?
I followed below URL to flash the source code into sdcard. Before flashing I modified system/etc/bluetooth/audio.conf file. In the audio.config file I enabled HFP=true.
URL: https://releases.linaro.org/13.04/android/panda/
After doing this I tried to connect to bluetooth device via following Android-java
UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
m_oBluetoothSocket = btDevice.createRfcommSocketToServiceRecord(MY_UUID);
m_oBluetoothSocket.connect();
I also tried connecting without UUID like below
Method m = btDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
m_oBluetoothSocket = (BluetoothSocket) m.invoke(btDevice, Integer.valueOf(1));
m_oBluetoothSocket.connect();
I was unable to connect to device I see invalid argument exception.
If I try to run the code on Android mobile I am able to connect and send AT command.
But in mobile I am not able to make it as handsfree, I have done a related post for it
https://stackoverflow.com/questions/19246344/is-it-possible-to-make-my-old-phone-as-handsfree-and-establish-connection-betwee
Comming Back to Pandaboard is my approach correct? How to make Pandaboard Bluetooth has Headset??
Please share thoughts on this....

Android BluetoothSocket.connect() throws IOExceptions "Connection Refused" and "Service discovery failed"

My goal is to write an app that allows me to control my Motorola Xoom with a Playstation 3 Bluetooth Remote Control.
The device is able to be discovered by the native bluetooth app & classified as being a joystick. However, I cannot pair via the native bluetooth app because the app requires a PIN & the device does not have a pin that I am aware of.
So far I am able to programmatically discover the device & create a socket, however all attempts to connect to the device fail.
In both cases:
UUID u = UUID.fromString("00001124-0000-1000-8000-00805f9b34fb");
This is supposed to be the UUID used by HID devices. I also used the method described on another site to verify the UUID is available on the device.
Method1 (many people seem to have issues with this):
BluetoothSocket socket = device.createRfcommSocketToServiceRecord(u);
socket.connect();
Result: IOException "Service discovery failed"
Method2 (the accepted workaround to Method1. I also tried ports 1-100):
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
BluetoothSocket socket = (BluetoothSocket) m.invoke(device, 1);
socket.connect();
Result: IOException "Connection refused"
I have tried this code using 3 devices (plus their UUIDs):
Playstation Remote
00001124-0000-1000-8000-00805f9b34fb
00001200-0000-1000-8000-00805f9b34fb
Nintento Wiimote
00001000-0000-1000-8000-00805f9b34fb
00001124-0000-1000-8000-00805f9b34fb
00001200-0000-1000-8000-00805f9b34fb
Microsoft Bluetooth Number Pad
(which sucessfully pairs, with a pin, via the natvie bluetooth app)
00001000-0000-1000-8000-00805f9b34fb
00001124-0000-1000-8000-00805f9b34fb
00001200-0000-1000-8000-00805f9b34fb
To be able to connect to the devices you have to connect over the HID profile, what you are trying is connecting over SPP (Serial Port Profile) to the UUID for HID etc, this will not work.
In addition these devices have some "custom" HID protocol descriptors that allow it to work with this pre-paired gaming consoles, you will need to get access to those to be able to control the Xoom with these controllers

Android Bluetooth Where can I get UUID?

I want to connect 3 devices via Bluetooth.
As for example I use BluetoothChat. So How I understand I should use different UUID for this devices. I have been trying to connect via such UUID=766c82f0-e1b4-11df-85ca-0800200c9a66, which I 've get it from Web UUID generator. But it doesn't work at all.
I have succesfully connected (to 1 device) if I used UUID=00001101-0000-1000-8000-00805F9B34FB
Where can I get UUID?
if you are using linux or mac, enter "uuidgen" this command without quotes in terminal, you will get an unique UUID, use that in your android project.
UUid is used to uniquely identify applications.Each application have a unique uuid .so use the same uuid for each device
In order to connect with your targetted devices, you need to know what are you connecting to. It'll be more helpful to list your device targets.
The UUID can be obtained from this link, http://www.bluecove.org/bluecove/apidocs/javax/bluetooth/UUID.html
Here you need to know what bluetooth profile is being used in each of your target device. You mentioned that "UUID=00001101-0000-1000-8000-00805F9B34FB" works.
This is due to your device is having a SPP Bluetooth profile. SPP stands for Serial Port Profile.
You could also lookup on Bluetoothdevice.getuuids
http://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#getUuids()
You must get the Bluetooth UUID to establish a connection to the device,
you can invoke the method getUuids() using reflection:
try {
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
Method getUuidsMethod = BluetoothAdapter.class.getDeclaredMethod("getUuids", null);
ParcelUuid[] uuids = (ParcelUuid[]) getUuidsMethod.invoke(adapter, null);
if(uuids != null) {
for (ParcelUuid uuid : uuids) {
Log.d(TAG, "UUID: " + uuid.getUuid().toString());
}
}else{
Log.d(TAG, "Uuids not found, be sure to enable Bluetooth!");
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
Don´t forget to add the permission:
<uses-permission android:name="android.permission.BLUETOOTH"/>
and you must enable bluetooth to get Uuids, for example:
UUID: 0000110f-0000-1000-8000-00805f9b12fb
UUID: 0000111d-0000-1000-8000-00805f9b12fb
UUID: 0000111a-0000-1000-8000-00805f9b12fb
Imagine, that u have a one or more services. Each service have its own UUID.
UUID=00001101-0000-1000-8000-00805F9B34FB is special one for SPP. Some devices (for example, Bluetooth serial board) will not work if u not set SPP UUID.
But for peer-to-peer connection between Android devices such as smartphones u may use your own generated UUID.
Devices must set same UUID to found each other and connect.
UUID format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx where x=[0,...,9]|[A,...,F].
Good idea is to set xxxxxxxx-xxxx-xxxx-xxxx- to Your generated unique id.
Second part xxxxxxxxxxxx may be set to Your server MAC address without ":".
On client side u may construct UUID from known generated unique id (embedded to Your app) and server MAC address without ":". You can get server MAC address during Bluetooth device discovery.
you have to do a service discovery with the device you are trying to connect with, Get the UUID that it returns (which will be corresponding to the service that is running on the device and accepting connections) and then connect to it.

Categories

Resources