Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
614 views
in Technique[技术] by (71.8m points)

bluetooth - How to send AT commands based on BT Hands-Free profile in android?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
".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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...