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

FindAndConnect Sample

Tue Jan 13, 2009 4:58 pm

Hi again,

That's where I stuck now;

On FindAndConnect sample:

1 - I removed service listbox's event handler. Now when I click service listbox there is no action at all.

2 - I have added a "Write" button. Here's button's event function:

if (currentService != null)
{
currentStream.Close();

currentService = null;
currentStream = null;
}

// Attempt to connect to the service
//

// Get service object from list box
currentService = (RemoteService)serviceList.SelectedItem;

char[] gonder = {'\x3C','\x6A','\x3E'};// gönder!!!
byte[] gonderArr = new byte[gonder.Length];
for (int j = 0; j < gonder.Length; j++)
gonderArr[j] = (byte)gonder[j];

try
{
// Connect to service by reading its Stream
currentStream = currentService.Stream;

currentStream.BeginWrite(gonderArr, 0, gonderArr.Length, new AsyncCallback(writeCallback), currentStream);

//closeConnection.Enabled = true;
//serviceList.Enabled = false;
lStatus.Text = "Data written...";

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

3 - I have added a "writeCallBack" for beginWrite. Here is the function. Note the sleep and beginRead below the endWrite:

private void writeCallback(IAsyncResult result)
{

// result is always a BlueToolsAsyncResult object
BlueToolsAsyncResult blueAsyncResult = (BlueToolsAsyncResult)result;

// We passed on buffer as custom object, you can pass on any object here. We passed the stream object
ServiceStream stream = (ServiceStream)blueAsyncResult.AsyncState;

// The buffer used for reading can be found in the result object.

try
{
// EndRead() must always be called!
// If stream has been closed due to an error, we'll have an excpetion here
stream.EndWrite(result);
System.Threading.Thread.Sleep(300);
stream.BeginRead(m_buffer, 0, m_buffer.Length, new AsyncCallback(readCallback), stream);

}
catch (ObjectDisposedException ex)
{
// Thrown if stream has been closed.
closeConnection.Enabled = false;
serviceList.Enabled = true;

lStatus.Text = ex.Message;
}
}

4 - I leave the readCallback alone. Same as the FindAndConnect sample.

Now, I click "Discover", select the device and the service. Then I click "Write". "Data written" is seen on the label. But there is no response at all. If there was, the label would show the response. Then when I click the "Write" button again, it says "stream not open. object Name: 'ServiceStream'". When I click it for the last time, "Data written" again but there is no response.

Thanks in advance,

Regards,
Uygar.

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

RE: FindAndConnect Sample

Tue Jan 13, 2009 5:09 pm

Do you expect some response then? What service are you connecting to? You say that you have removed the service listbox's event handler. So how are you connecting to a remote service?

Regards,
Jonas

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

RE: FindAndConnect Sample

Tue Jan 13, 2009 5:17 pm

Hi Jonas,

I have the following line in my "Write" button event handler;

currentStream = currentService.Stream;

I think this connects to device by reading its stream, right?

Thank you.

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

RE: FindAndConnect Sample

Tue Jan 13, 2009 5:29 pm

Jonas, actually I want to know is if there is an logical error in my code.

Thank you.

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

RE: FindAndConnect Sample

Tue Jan 13, 2009 5:47 pm

I don't know, run it and try it?

Regards,
Jonas

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

RE: FindAndConnect Sample

Tue Jan 13, 2009 5:50 pm

Thank you Jonas,

I figured it out now. This sample works as a charm! For my device, first you should turn on Bluetooth mode by sending some bytes. I did not know that. And then write again and get the information from the device.

Have a nice day,

yuva
Posts: 8
Joined: Tue Jul 22, 2008 9:55 am
Location: India

RE: FindAndConnect Sample

Mon Mar 09, 2009 3:39 pm

hi rexmont,

I am facing the same problem . please can u send me the code which is working for you?

Return to “BlueTools”