Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding Bluetooth Mac address in Windows10 UWP without pairing

I'm trying to write an app that reads all MAC addresses around on Windows 10 IoT. These lines of code is returning all paired devices even if they aren't turn on.

var selector = BluetoothDevice.GetDeviceSelector();
var devices = await DeviceInformation.FindAllAsync(selector);
listBox.Items.Add(devices.Count);
foreach (var device in devices)
{
    listBox.Items.Add(device.Id);
}

And I also found this line of code

await DeviceInformation.FindAllAsync(RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort));

This returning null though. Is there any way to scan for all MAC addresses in a Windows 10 universal app?

like image 692
Konrad Straszewski Avatar asked Dec 27 '15 11:12

Konrad Straszewski


2 Answers

You are very close to finding the answer of your question. You might try getting a BluetoothDevice instance from the DeviceId property. You will then be able to get all the specific Bluetooth information including the Bluetooth address

var selector = BluetoothDevice.GetDeviceSelector();
var devices = await DeviceInformation.FindAllAsync(selector);
foreach (var device in devices)
{
    var bluetoothDevice = await BluetoothDevice.FromIdAsync(device.Id);
    if (bluetoothDevice != null)
    {
        Debug.WriteLine(bluetoothDevice.BluetoothAddress);
    }
    Debug.WriteLine(device.Id);
    foreach(var property in device.Properties)
    {
        Debug.WriteLine("   " + property.Key + " " + property.Value);
    }
}
like image 109
danvy Avatar answered Sep 24 '22 21:09

danvy


There is a new approach using BluetoothLEAdvertisementWatcher to scan for all Bluetooth LE device around. Here is a piece of code I use in my project:

var advertisementWatcher = new BluetoothLEAdvertisementWatcher()
{
    SignalStrengthFilter.InRangeThresholdInDBm = -100,
    SignalStrengthFilter.OutOfRangeThresholdInDBm = -102,
    SignalStrengthFilter.OutOfRangeTimeout = TimeSpan.FromMilliseconds(2000)
};

advertisementWatcher.Received += AdvertisementWatcher_Received;
advertisementWatcher.Stopped += AdvertisementWatcher_Stopped;

advertisementWatcher.Start();

and later

advertisementWatcher.Stop();

advertisementWatcher.Received -= AdvertisementWatcher_Received;
advertisementWatcher.Stopped -= AdvertisementWatcher_Stopped;
like image 32
foxanna Avatar answered Sep 24 '22 21:09

foxanna