Code never runs past AcceptBluetoothClient

Topics: Bluetooth - Microsoft
Feb 9, 2014 at 10:55 PM
I'm a beginner in both bluetooth and 32feet.NET's library programming. I am building an Android app that will connect to my .NET server to send messages. I am using the following starter code to try to gather messages sent to the server:
private Guid service = new Guid("{860c8850-91c7-11e3-baa8-0800200c9a66}");

private BluetoothClient client;
private BluetoothListener listener;

public BluetoothManager()
{
        BluetoothRadio.PrimaryRadio.Mode = RadioMode.Connectable;

        listener = new BluetoothListener(service);
        listener.Start();

        client = listener.AcceptBluetoothClient(); // Never passes this line
        Console.Writeline("Bluetooth client accepted - Never prints");

        Stream peerStream = client.GetStream();
        var wtr = new StreamReader(peerStream);
        var line = wtr.ReadLine();
        MessageBox.Show(line);
}
As stated in the comments and WriteLine, the code never passes the AcceptBluetoothClient call. I've tried other peoples' suggestions on this forum who seem to have had the same problem (I believe changing the guid fixed it for them) but haven't had luck, as changing the guid makes no difference. It doesn't throw an exception or any errors, it just stops at that line.

Does AcceptBluetoothClient block until it receives a connection from a client? It might just be that my server is waiting for a connection properly, but doesn't receive the message properly from the client side. Adding to this, if I want to have multiple connections from multiple clients, do I need to create a new BluetoothClient for each object, or how would that work?

Thank-you for your help
Developer
Feb 10, 2014 at 8:56 AM
Yes AcceptBluetoothClient block until it receives a connection from a client. You need to use the same UUID/Guid on both sides of the connection. Does the Android client connect or fail?

It returns a new BluetoothClient for each client that connects. If multiple clients are connecting at the same time you'll need to use threads or asynchronous code to service the multiple clients and new connection simultaneously.
Feb 10, 2014 at 11:24 PM
I was able to connect to the bluetooth client (with code very similar to the above) via a python script, indicating the problem seems to be on the Android's side. The blocking bit makes much more sense now, and I appreciate your insight on the multiple clients part.

On the Android side, a very stripped down piece of code that illustrates how we're trying to connect is:
BluetoothDevice mDevice = mBluetoothAdapter.getRemoteDevice(MAC_ADDRESS);

// Attempt to connect to the device by getting a socket
// Get a BluetoothSocket to connect with the given BluetoothDevice
// MY_UUID is the app's UUID string, also used by the server code
BluetoothSocket socket = mDevice.createRfcommSocketToServiceRecord(UUID);

// Connect the device through the socket. This will block until it succeeds or throws an exception
mSocket.connect();
But the connection never goes through, the server does not see it. Asking for help on the Android side may be out of the scope of this forum, so we'll keep working on it and see if we can get it working (but any pointers you know of wouldn't be turned down ;) ).

Thank-you for your help, Alan, it's very much appreciated!
Sep 17, 2015 at 7:28 AM
Edited Sep 30, 2015 at 8:52 AM
Hi,

this discussion is older than one year, nevertheless I hope someone can help me.
I am using the same methods and i'm facing exactly the same problem like desscribed above.
I'm using a Desktop PC with Windows 7 and the Windows drivers for a USB bluetooth dongle and writing the server Code in C# in Visual Studio 2013.
My device has the Android Version 4.2.2.
The BluetoothListener in C# throws two Exceptions: System.NotSupportedException and System.Net.Sockets.SocketException
Could you solve your problem? Or has anyone else a suggestion how to solve it?

EDIT:
now I know that it's a driver problem... my dongles (sidecom&speedlink both V4.0) seem not to support the generic Windows Drivers installed on my PC. Are they maybe too new?
I'd be very glad about any answers
Christoph