Search code examples
c#asynchronousbluetoothbluetooth-device-discovery

32feet.net howto discover nearby bluetooth devices async in c#


I am trying to use the 32feet.NET bluetooth library in a C# application to detect nearby devices. The purpose of my little application is to let the PC know who is in the room using the bluetooth feature of people's mobile phones.

The best way to do something like this would be to let the devices I want to "track" connect once, then continuously check if they can be detected via bluetooth.

Now my questions:

  1. Do I need to pair or authenticate a device with my application? How to do this in C# with 32feet.NET?

  2. How to continuously check for devices in range and compare them to the stored devices?

I know that all this is probably in the library documentation, but it is really hard to read for me and most of the examples seem to be in VB which I don't know and find hard to translate to C# (especially when it comes to AsyncCallbacks and the like).

I would be really glad if someone could give me a push in the right direction!


Solution

  • This is not an answer, but I wasn't able to put this much code on comment section. Change these lines of code:

    //continue listening for other broadcasting devices
    listener.BeginAcceptBluetoothClient(this.BluetoothListenerAcceptClientCallback, listener);
    
    // create a connection to the device that's just been found
    BluetoothClient client = listener.EndAcceptBluetoothClient();
    

    to

    // create a connection to the device that's just been found
    BluetoothClient client = listener.EndAcceptBluetoothClient();
    
    // continue listening for other broadcasting devices
    listener.BeginAcceptBluetoothClient(this.BluetoothListenerAcceptClientCallback, listener);
    

    Basically, change the sequence of code..
    As for every call to BeginXXXX method must have next EndXXXX. And all above code, you are trying to BeginAcceptBluetoothClient over already began "BeginAcceptBluetoothClient".

    Hope you understand.