tags:

views:

121

answers:

1

I'm not very sure but for some reasons I can't use a BlueToothListener that receives string information.

I tried following this example, and it doesn't seems to work.

Code Snippet

private Guid Rffcomm = BluetoothService.RFCommProtocol; 
private void btnConnect_Click(object sender, EventArgs e)
{
    if (comboBox1.SelectedValue != null)
    {
        try
        {
            BluetoothClient btSender = null;
            btSender = new BluetoothClient();
            btSender.Connect(new BluetoothEndPoint(selectedAddr, Rffcomm));
            MessageBox.Show("1");
            StreamWriter swSender = new StreamWriter(btSender.GetStream());
            swSender.WriteLine("Test 1");
            //swSender.Flush();
            // swSender.Close(); // At first it worked, but after some tries, IOsocket exception keeps popping out.
        }
        catch (Exception se)
        {
            MessageBox.Show(se.Message);
        }
    }
}

void listening()
{
    try
    {
        bool run = true; 
        BluetoothListener btl = new BluetoothListener(Rffcomm);
        btl.Start();
        while(run)
        {
            BluetoothClient btReceiver = btl.AcceptBluetoothClient();
            StreamReader srReceiver = new StreamReader(btReceiver.GetStream());
            String writeInfo = srReceiver.ReadLine();
            test = writeInfo;
            srReceiver.Close();
        }
        MessageBox.Show(test);
    }
    catch(Exception ex) { MessageBox.Show(ex.Message); }
}

private void button1_Click(object sender, EventArgs e)
{
    // Start a new thread to deal with an incoming Bluetooth connection
    Thread t = new Thread(new ThreadStart(listening));
    t.Start();
}
A: 

So as I wrote to you in another forum: :-)

Yeh, using BluetoothService.RFCommProtocol will have the client connect to a random RfComm-based service

Note "random"!!!

You need to not use "RFCommProtocol" as your Service Class Id, but do as the user guide says:

Bluetooth applications/services are identified and registered by UUID (Universally Unique Id), a 128-bit value that is represented by the System.Guid class in .NET. If one is creating a new service then a new UUID should be created at design time and entered into the two applications’ source code, a new value can be created either by calling Guid.NewValue or using the guidgen.exe Windows SDK program — in Visual Studio access it with menu item Tools, “Create GUID”.

And use that Guid as the Service Class Id value in both server (in its constructor) and client (Connect method). Do that and see what happens.

alanjmcf