Faisal
Faisal

Reputation: 33

Finding paired Bluetooth device if its in range using 32Feet

I'm trying to make a simple application that will show the nearby Bluetooth devices (this one works fine)

Also I'm trying to find if the specific Bluetooth device (I have its MAC & Its already paired in windows) is in range. It's an android phone and by default its set to Invisible to nearby devices, But that wouldn't be a problem since it's already paired in windows, right?

Am using this code:

BluetoothClient BTClient = new BluetoothClient();
BluetoothDeviceInfo[] BTDeviceInfo =  BTClient.DiscoverDevices();

which is working find for finding "Visible Devices", It will also show me Paired device weather its In-Range or Not!

How i can check if that paired devices is in-range? Without going to setting and make it "Visible" will be much better

Upvotes: 2

Views: 1435

Answers (1)

Ahmed Soliman
Ahmed Soliman

Reputation: 53

It might be too late, but you can use BTClient.DiscoverDevicesInRange(); and look for your device in that list.

Another method would be to try reading the services in the device using its bluetooth address, you will get an exception if it isn't in range.

private static Boolean IsInRange(BluetoothDeviceInfo device)
    {
        Guid fakeUuid = new Guid("{F13F471D-47CB-41d6-9609-BAD0690BF891}");
        try
        {
            ServiceRecord[] records = device.GetServiceRecords(fakeUuid);
            return true;
        }
        catch(SocketException ex)
        {
            if (ex.ErrorCode == 10040) return true;
            return false;
        }
    }

Upvotes: 0

Related Questions