7

I am trying to provide a "click to dial" solution for someone to a bluetooth device such as a mobile phone. I have been trying to do so using the 32feet.net bluetooth api.

I haven't really done anything with bluetooth (since the days of at commands via a bluetooth serial port) but I have paired the device in question, which supports the handsfree service with the pc. I have the following code to attempt to connect and send a dial command.

String deviceAddr = "11:11:11:11:11:11";
BluetoothAddress addr = BluetoothAddress.Parse(deviceAddr);
BluetoothEndPoint rep = new BluetoothEndPoint(addr, BluetoothService.Handsfree);
BluetoothClient cli = new BluetoothClient();
cli.Connect(rep);
Stream peerStream = cli.GetStream();

String dialCmd = "ATD 0000000000\r\n";
Byte[] dcB = System.Text.Encoding.ASCII.GetBytes(dialCmd);
peerStream.Write(dcB, 0, dcB.Length);

// Begin Edit ------------------------------------------------------------
Byte[] sResponse = new Byte[100];
peerStream.Read(sResponse, 0, 99);
TextBox1.Text = System.Text.Encoding.ASCII.GetString(sResponse);
// End Edit --------------------------------------------------------------

peerStream.Close();
cli.Close();
MessageBox.Show("Done");

Since it seems to run through these lines of code, taking an appropriate time to connect at the relevant spot or crashing out if the device address is wrong and it can't connect. Obviously the AT command is not the right thing to be sending it.

Can anyone enlighten me as to what I might need to send to a bluetooth device via the handsfree profile to get it to dial?

Begin Edit -------------------------------------------

I decided to read from the stream and see if there was a response of any sort after sending the AT command through. Since I am just testing to see if I can make it work I am just dumping the response into a textbox.

The response I read from the stream is :

ERROR

There doesn't seem to be an error codes or anything.

End Edit ---------------------------------------------

Edit --------------------------------------------------

Sent command : AT+CMER\r

Result : OK

then

Sent command : AT+CIND=?\r

Result : +CIND: ("service",(0-1)),("call",(0-1)),("callsetup",(0-3)),("battchg",(0-5)),("signal",(0-5)),("roam",(0-1)),("callheld",(0-2))

then

Send command : ATD 0000000000\r

Result: OK D: ("service",(0-1)),("call",(0-1)),("callsetup",(0-3)),("battchg",(0-5)),("signal",(0-5)),("roam",(0-1)),("callheld",(0-2))

Still it doesn't actually dial :(

End Edit ----------------------------------------------

Solution ----------------------------------------------

The following code now works to dial via my iPhone. It's really rough at the moment, as I have just been testing to see if I could make it work. It's enough to get started for anyone else wanting to do a similar thing.

String deviceAddr = "00:00:00:00:00:00"; 
        BluetoothAddress addr = BluetoothAddress.Parse(deviceAddr);
        BluetoothEndPoint rep = new BluetoothEndPoint(addr, BluetoothService.Handsfree);

        BluetoothClient cli = new BluetoothClient();
        cli.Connect(rep);
        Stream peerStream = cli.GetStream();

        String dialCmd1 = "AT+CMER\r";
        String dialCmd2 = "AT+CIND=?\r";
        String dialCmd3 = "AT+BRSF=\r";
        String dialCmd4 = "ATD 0000000000;\r";

        Byte[] dcB = System.Text.Encoding.ASCII.GetBytes(dialCmd1);
        peerStream.Write(dcB, 0, dcB.Length);

        Byte[] sRes = new Byte[200];
        peerStream.Read(sRes, 0, 199);
        textBox1.Text = textBox1.Text + "\n\r----------\n\r" + System.Text.Encoding.ASCII.GetString(sRes);

        dcB = System.Text.Encoding.ASCII.GetBytes(dialCmd2);
        peerStream.Write(dcB, 0, dcB.Length);

        peerStream.Read(sRes, 0, 199);
        textBox1.Text = textBox1.Text + "\n\r----------\n\r" + System.Text.Encoding.ASCII.GetString(sRes);

        dcB = System.Text.Encoding.ASCII.GetBytes(dialCmd3);
        peerStream.Write(dcB, 0, dcB.Length);

        peerStream.Read(sRes, 0, 199);
        textBox1.Text = textBox1.Text + "\n\r----------\n\r" + System.Text.Encoding.ASCII.GetString(sRes);

        dcB = System.Text.Encoding.ASCII.GetBytes(dialCmd4);
        peerStream.Write(dcB, 0, dcB.Length);

        peerStream.Read(sRes, 0, 199);
        textBox1.Text = textBox1.Text + "\n\r----------\n\r" + System.Text.Encoding.ASCII.GetString(sRes);

        peerStream.Close();
        cli.Close();
Tim
  • 964
  • 2
  • 15
  • 30
  • Yup. When I connect to my wm6 phone the RFCOMM connection to HF closes immediately after it opens, and when I try it against an iPhone I see the ERROR result to the ATD command. – alanjmcf May 12 '11 at 17:13
  • Is it possible to send dial commands over any other bluetooth profiles if the HF profile is in use and a serial port profile is not available. Such as the AVRCP, A2DP or PBAP? I know they are for other purposes but is it possible? – Tim May 19 '11 at 22:38
  • If more than 39 characters are entered, or a syntax error is found anywhere in the command line, the modem returns an ERROR result code, and the command input is ignored. So a syntax error then? whats wrong with my ATD command? – Tim May 24 '11 at 06:09
  • 1
    Just to note, on the various stacks... MSFT has a driver-level SCO API but one must handle each packet AFAICS. Widcomm has a 'use audio' API. BlueSoleil's API has support for HFP/HSP both AG and HS, so has a function called Btsdk_HFAP_Dial for instance. So if you can change the stack then try BlueSoleil first, then Widcomm. – alanjmcf May 25 '11 at 17:44
  • 1
    Good stuff! Probably irrelevant now, or maybe for some phones: I was just coming to note that often a phone has _one_ registered headset and maybe the phone needs to be configured to use the PC (and thus one will need to run a BluetoothListener for Headset on the PC). – alanjmcf May 31 '11 at 20:03

2 Answers2

3

Try to find what is the response for AT\r (or) ATH\r. If the response is "OK\r\n", try your dial command with no space after ATD and number.

Umesh CHILAKA
  • 1,466
  • 14
  • 25
  • Thanks. The AT\r returns AT+OK... ATH\r returns ERROR as does ATDxxxxxxxxxx\r – Tim May 24 '11 at 03:50
  • try ATDxxxxxxxxxx;\r semicolon at end is for voice call for test try to send a SMS using AT commands – Umesh CHILAKA May 24 '11 at 07:22
  • I put the semicolon back at the end of the dial string like I had in the beginning, plus the addition of the other commands and it is now dialing as expected. Thanks. :) – Tim May 24 '11 at 23:33
1

As maintainer of 32feet.NET I'd love to find the answer to this. Hopefully someone with knowledge of HSP/HFP will explain why this doesn't work. My only general guess is that since we're not accepting/creating the SCO channel for the audio channel the phone refuses the connection.

Once thing in your case however... AT commands are terminated by a CR (0Dh); or does the semicolon act the same??

alanjmcf
  • 3,430
  • 1
  • 17
  • 14
  • Thanks. I have since tried adding the carriage return to the end of the AT command but still have had no luck. – Tim May 12 '11 at 01:16
  • Also added code to read back from the stream resulting in a response of : ERROR – Tim May 12 '11 at 01:45