9

I am trying to establish Bluetooth connection between an Android device with other mobile phone over Handsfree profile. I am using following code -

private static final UUID MY_UUID = UUID.fromString("0000111F-0000-1000-8000-00805F9B34FB"); // UUID for Hands free profile   

// Some code...

// Get Bluetooth Adapter.
m_oBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

// Some code...

// For paired BT device, getting a connection established.
if(null != m_oBluetoothDevice)
{
    if(BluetoothDevice.BOND_BONDED == m_oBluetoothDevice.getBondState())
    {
        try
        {
            m_oBluetoothSocket = m_oBluetoothDevice.createRfcommSocketToServiceRecord(MY_UUID);     
            m_oBluetoothSocket.connect();

            Log.i(TAG, "Socket Connected");

        }
        catch(Exception e)
        {
            if(null != m_oBluetoothSocket)
            {
                Log.i(TAG, "Closing socket");
                try 
                {
                    m_oBluetoothSocket.close();
                }
                catch (Exception e1) 
                {
                    Log.i(TAG, "Error while closing socket : " + e1.getMessage());
                }
            }
        }               
    }
}

I can create RFCOMMSocket using this code.

Now I want to send AT commands based on Bluetooth Hands-Free profile. e.g. If other mobile phone receives a phone call, my Android device can reject this call by sending AT command- "+CHUP". I am not sure whether this is possible or not.

At this point, I am stuck. I have read Bluetooth APIs where I found -

     BluetoothHeadset.ACTION_VENDOR_SPECIFIC_HEADSET_EVENT

Can we use this Intent for sending AT commands? Is this a proper way to send AT command based on Bluetooth Hands-Free profile? Please someone help me out and give me proper direction.

Any input from you all will be great help for me.

Thanks in advance.

KavitaDev
  • 475
  • 3
  • 7
  • 15
  • Please somebody help me out. Still, I am not able to solve this problem. – KavitaDev May 20 '13 at 09:21
  • I am trying out the same thing and have some questions like is this method viable to receive call from other phone or do you get only notification? Also it would help me out a great deal if i could get some source code,thanks. – rajithShetty Feb 25 '21 at 14:40

2 Answers2

11

You need to create InputStream and OutputStream so you can talk to the phone:

mmInStream = m_oBluetoothSocket.getInputStream();
mmOutStream = m_oBluetoothSocket.getOutputStream();

To setup the HFP connection you start to send:

mmOutStream.write("AT+BRSF=20\r".getBytes());

Where 20 is code for what you support of HFP.

And to read from the phone:

buffer = new byte[200];
mmInStream.read(buffer);
command = new String(buffer).trim();

So now you can talk beetwen the devices and you can read more about the Handsfree profile on https://www.bluetooth.org/docman/handlers/downloaddoc.ashx?doc_id=238193

eliasj
  • 316
  • 3
  • 7
  • Hi eliasj, I have followed your code and it helps me a lot. I have established connection between my Android phone and other mobile phone. I have sent few AT commands following the way you have specified in answer. Now, I am trying to hang up incoming call using my application by sending AT command. Still I am struggling for this. Though your answer provided me a proper direction, I will be grateful to you if you can help me regarding this. Thank you very much. – KavitaDev Jun 05 '13 at 08:48
  • @KavitaDev To hang up you send `mmOutStream.write("AT+CHUP\r".getBytes());`. It is all in the specs of the protocol (the link in the end of my awnser). – eliasj Jun 07 '13 at 07:53
  • Yes eliasj, I followed the pdf you have specified in answer and finally I did all steps successfully which are required for handling active call. Thanks a lot. – KavitaDev Jun 07 '13 at 09:40
  • @eliasj : can u please tell how can I use `BluetoothHeadset's object` for sending `HFP` command receiving command in phone – Kaushik Apr 16 '14 at 06:44
  • @kaushik: If I remember right is `BluetoothHeadset` is used to talk to Bluetooth headset device. This is code for being the headset deceive. – eliasj May 08 '14 at 07:09
  • @eliasj: Please answer my question here : http://stackoverflow.com/questions/24058957/how-to-check-at-one-phone-whether-the-other-phone-is-getting-an-incoming-call – SoulRayder Jun 05 '14 at 11:46
  • @KavitaDev: While making a call, how did you manage to get the audio on the headset? Any APIs? – SoulRayder Jun 18 '14 at 05:42
  • Hey Kavita, Can you please help me out to do same. Actually I am able to paired and all but to use AT commands its showing some fatal error. Can you please let me know the **AIDL** you have used and also the **permissions**. – Rajat kumar Jul 17 '14 at 13:00
1

Adding reference to AT commnads

http://forum.xda-developers.com/showthread.php?t=1471241

http://www.zeeman.de/wp-content/uploads/2007/09/ubinetics-at-command-set.pdf

Jay Kominek
  • 8,674
  • 1
  • 34
  • 51
Deepak Ramesh
  • 403
  • 1
  • 5
  • 13