Home » Android » Bluetooth connection between Android and another phone over the Handsfree profile

Bluetooth connection between Android and another phone over the Handsfree profile

Posted by: admin May 14, 2020 Leave a comment

Questions:

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!

How to&Answers:

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.