1

I have two phones which are paired and connected via bluetooth. How to programmatically check at one phone's end whether the other phone is getting an incoming call? Should I use a particular profile for this, that is, PBAP or HFP? If yes, how I am to do this?

Once I detect this, if I want to receive the incoming call via the connected phone, how should I implement that?

SoulRayder
  • 5,072
  • 6
  • 47
  • 93
  • 1
    Please give me the reason for the downvote. If any further clarification required, I am ready to give it. – SoulRayder Jun 06 '14 at 04:26
  • @SoulRayder- I am trying to do the same thing by using AG as ANDROID phone (SAMSUNG S DUOS) and on other side HF as CSR 8670 Bluetooth Audio Kit. Can you please help me out , how did you transfer the AT COMMANDS and what changes I have to do to make it worthy. – Rajat kumar Jul 18 '14 at 05:08
  • 1
    This link might be helpful to you for transferring at commands http://stackoverflow.com/questions/16610811/how-to-send-at-commands-based-on-bt-hands-free-profile-in-android?lq=1.. but since it is a car kit, you have to find a similar way for transferring commands since the above link is for the android platform. – SoulRayder Jul 19 '14 at 05:43
  • 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

3

Download Hands Free Profile pdf. It is available easily. It provides you all details about how HFP works and AT commands supported by HFP for communication. No APIs available for this.

As eliasj said, you need to implement HFP and when two phones get connected, you can communicate between them via AT commands.

Suppose you have 1st phone which is Android device and 2nd phone Android or any device and they both are connected over HFP.

I don't have complete code but I can suggest you some AT commands -
1. Using AT+CIND? command you can read indicator status of other phone.
2. To enable reporting for Indicator status change, you need to use AT+CMER=3,0,0,1 command.
3. Once you get valid response from 'AT+CMER' command, you can use AlarmManager that will start a service which continuously reads the input stream of Bluetooth Socket.
4. Because of step 2., if the 2nd phone is having any incoming call, the input stream of Bluetooth Socket will contains RING as an alert.
I have used service implementing a Runnable. Here is a sample code for step 4.-

public void run()
{
    try
    {
        // Get input and output streams from Bluetooth socket.
        m_oInputStream = m_oBluetoothSocket.getInputStream();
        m_oOutputStream = m_oBluetoothSocket.getOutputStream();

        // Read input stream for +CIEV response is given or not.
        byte[] buffer = new byte[200];
        int nNumberOfBytesRead = m_oInputStream.read(buffer);

        String strResponse = new String(buffer).trim();

        if(true == strResponse.contains("RING"))
        {
            // Contains RING Alert. Answer the call.                
            // Start Activity for handling Incoming Call.
            Intent oIncomingCallActivityIntent = new Intent(getApplicationContext(), IncomingCallActivity.class);
            oIncomingCallActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            getApplicationContext().startActivity(oIncomingCallActivityIntent);

            // Stop service.
            stopSelf(); 
        }
    }
    catch(Exception e)
    {
        // Log the error.
    }
}

You need to implement acivity that handles incoming call. It will accept or reject call. To accept incoming call AT+ATA command is used. You will receive "OK" as a response from 2nd phone.

I hope this will help you.

KavitaDev
  • 475
  • 3
  • 7
  • 15
  • *Once you get valid response from 'AT+CMER' command*: What do you mean for valid response? Am I supposed to poll for reponse "OK"? – SoulRayder Jun 19 '14 at 10:50
  • When I tried step 1, I got a response +CIND: and six numbers separated by commas. Now what do these mean? In the pdf, this explanation is not given. Please help. – SoulRayder Jun 19 '14 at 11:02
  • I figured it out :). Thanks for your help. But what should I do if I want this to be monitored continuously, that is even after I get one incoming call notification, I should be able to get more such notifications even after that. How to go about that? – SoulRayder Jun 19 '14 at 11:46
  • I have used AlarmManager which is starting my service at a perticular time interval. Within service I have used above code that reads InputStream and checks for "RING" alert. I am not sure whether this is good way to approach as it will definitely result into battery drain. – KavitaDev Jun 19 '14 at 12:20
  • I have done what you have mentioned here within an asynctask. But as soon as I got one incoming call, it now shows notification and finishes. But I don't want it to finish, it should continuously run and give me notifications when I get incoming calls, atleast when I am in the same activity. What would be your suggestion for the best way to go about this? – SoulRayder Jun 20 '14 at 04:05
  • Does notification means Android notifications? If not, let me know what you have used? I have used separate activity that handles any incoming call. I mean either Accept or Reject call. – KavitaDev Jun 20 '14 at 11:51
  • I have added a custom notification in the form of an alert dialog, which is made and shown everytime my code detects an incoming call being made – SoulRayder Jun 23 '14 at 04:04
0

You should implement HFP (the hands-free side). When I looked at this problem over a year ago it was not possible to send the audio between to phones (Android) but it could have change now.

Look at the Q/A in How to send AT commands based on BT Hands-Free profile in android? (hit on how to connect) and in the HFP spec https://www.bluetooth.org/docman/handlers/downloaddoc.ashx?doc_id=238193 (for how the profile works (incoming call on page 36))

Community
  • 1
  • 1
eliasj
  • 316
  • 3
  • 7