开发者

Bluetoothlistener help

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 wi开发者_如何学Pythonth an incoming Bluetooth connection
    Thread t = new Thread(new ThreadStart(listening));
    t.Start();
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜