rexmont
Posts: 11
Joined: Mon Jan 12, 2009 5:14 pm

Simple Send & Receive Question

Mon Jan 12, 2009 5:14 pm

Hello all,

I have an electronic device which can communicate with PC via bluetooth. I put a button on FindAndConnect sample, a "Write and Read Response" button and this button's function is below. I discover devices, discover services. When I press that button, data written successfully but when I try to read, it hangs. Could you please guide me to the right way? My request is in hexadecimal format.

Thank you.

private void button1_Click(object sender, EventArgs e)
{
if (currentService != null)
{
if (currentStream != null)
{
currentStream.Close();
currentStream = null;
}
currentService = null;
}

// Attempt to connect to the service
//

// Get service object from list box
currentService = (RemoteService)serviceList.SelectedItem;
lStatus.Text = "Asking for Serial No";
char[] gonder = {'\x3C','\x6A','\x3E','\x00'};
byte[] gonderArr = new byte[gonder.Length];
for (int j = 0; j < gonder.Length; j++)
gonderArr[j] = (byte)gonder[j];

try
{
currentService.Stream.Write(gonderArr, 0, gonderArr.Length);
closeConnection.Enabled = true;
serviceList.Enabled = false;
lStatus.Text = "Data written...";

System.Text.ASCIIEncoding enc = new ASCIIEncoding();
byte[] byteRead = new byte[255];

currentStream = currentService.Stream;
int LengthOfBuffer = currentStream.Read(byteRead, 0, byteRead.Length);

String response = enc.GetString(byteRead, 0, LengthOfBuffer);
lStatus.Text = "Response: " + response;


}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
currentService = null;
}
}

Jonas
Posts: 2319
Joined: Mon Jan 09, 2006 2:20 pm
Location: Sweden

RE: Simple Send & Receive Question

Mon Jan 12, 2009 6:54 pm

It hangs because you are making a synchronous Read operation on the Stream.
If the device doesn't "say" anything, there is nothing to read and the application will appear to be hung but it is in fact just waiting.

Regards,
Jonas

rexmont
Posts: 11
Joined: Mon Jan 12, 2009 5:14 pm

RE: Simple Send & Receive Question

Mon Jan 12, 2009 8:52 pm

Thanks for the reply,

Return to “BlueTools”