C# 在Windows10 UWP中查找蓝牙Mac地址而不进行配对
我正在尝试编写一个应用程序,可以读取Windows10IoT上的所有MAC地址。这些代码行返回所有已配对的设备,即使它们没有打开C# 在Windows10 UWP中查找蓝牙Mac地址而不进行配对,c#,bluetooth,windows-10,C#,Bluetooth,Windows 10,我正在尝试编写一个应用程序,可以读取Windows10IoT上的所有MAC地址。这些代码行返回所有已配对的设备,即使它们没有打开 var selector = BluetoothDevice.GetDeviceSelector(); var devices = await DeviceInformation.FindAllAsync(selector); listBox.Items.Add(devices.Count); foreach (var device in devices) {
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);
}
我还发现了这行代码
await DeviceInformation.FindAllAsync(RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort));
但这返回空值。是否有任何方法可以扫描Windows 10 universal应用程序中的所有MAC地址?您很快就能找到问题的答案。您可以尝试从DeviceId属性获取
BluetoothDevice
实例。然后,您将能够获得所有特定的蓝牙信息,包括蓝牙地址
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);
}
}
有一种新的方法使用扫描周围的所有蓝牙LE设备。 以下是我在项目中使用的一段代码:
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();
后来
advertisementWatcher.Stop();
advertisementWatcher.Received -= AdvertisementWatcher_Received;
advertisementWatcher.Stopped -= AdvertisementWatcher_Stopped;
该应用程序仍然只创建已配对的设备。有没有办法在没有配对的情况下找到MAC地址?不,你实际上不能这样做。好的,那么我如何才能找到配对的设备,但只能找到那些在设备范围内的设备。因为这行代码建立了所有曾经配对过的设备。
private void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementReceivedEventArgs eventArgs)
{
//MessAgeChanged(MsgType.NotifyTxt, "FR_NAME:"+ eventArgs.Advertisement.LocalName + "BT_ADDR: " + eventArgs.BluetoothAddress);
string sDevicename = setup.Default.BLEName.Text;
BluetoothLEDevice.FromBluetoothAddressAsync(eventArgs.BluetoothAddress).Completed = async (asyncInfo, asyncStatus) =>
{
if (asyncStatus == AsyncStatus.Completed)
{
if (asyncInfo.GetResults() == null)
{
if (!FailMsg)
{
MessAgeChanged(MsgType.NotifyTxt, "None");
}
}
else
{
BluetoothLEDevice currentDevice = asyncInfo.GetResults();
Boolean contain = false;
foreach (BluetoothLEDevice device in DeviceList.ToArray())/
{
if (device.DeviceId == currentDevice.DeviceId)
{
contain = true;
}
}
if (!contain)
{
byte[] _Bytes1 = BitConverter.GetBytes(currentDevice.BluetoothAddress);
Array.Reverse(_Bytes1);
// The received signal strength indicator (RSSI)
double rssi = eventArgs.RawSignalStrengthInDBm;
DeviceList.Add(currentDevice);
MessAgeChanged(MsgType.NotifyTxt, currentDevice.Name + " " + BitConverter.ToString(_Bytes1, 2, 6).Replace('-', ':').ToLower() + " " + rssi);
DeviceWatcherChanged(MsgType.BleDevice, currentDevice);
}
}
}
};
}