2015-12-27 24 views
5

Tôi đang cố gắng viết một ứng dụng đọc tất cả các địa chỉ MAC xung quanh trên Windows 10 IoT. Các dòng mã này sẽ trả lại tất cả các thiết bị được ghép nối ngay cả khi chúng không được bật.Tìm địa chỉ Bluetooth trên máy Mac trong Windows10 UWP mà không cần ghép nối

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); 
} 

Và tôi cũng tìm thấy dòng mã này

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

này trở rỗng mặc dù. Có cách nào để quét tất cả các địa chỉ MAC trong một ứng dụng phổ dụng Windows 10 không?

Trả lời

1

Bạn đang rất gần với việc tìm câu trả lời cho câu hỏi của mình. Bạn có thể thử nhận một phiên bản BluetoothDevice từ thuộc tính DeviceId. Sau đó bạn sẽ có thể nhận được tất cả các thông tin Bluetooth cụ thể bao gồm địa chỉ Bluetooth

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); 
    } 
} 
+0

App vẫn sáng lập chỉ những thiết bị đã ghép nối. Có cách nào để tìm địa chỉ MAC mà không ghép nối? –

+0

Không, bạn thực sự không thể làm điều đó. – danvy

+0

ok, vậy làm cách nào tôi có thể tìm thấy thiết bị được ghép nối nhưng chỉ những thiết bị nằm trong phạm vi thiết bị. Trở thành dòng mã này thành lập tất cả các thiết bị đã từng được ghép nối. –

2

Có một cách tiếp cận mới sử dụng BluetoothLEAdvertisementWatcher để quét tất cả các thiết bị Bluetooth LE xung quanh. Dưới đây là một đoạn mã tôi sử dụng trong dự án của tôi:

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(); 

và sau

advertisementWatcher.Stop(); 

advertisementWatcher.Received -= AdvertisementWatcher_Received; 
advertisementWatcher.Stopped -= AdvertisementWatcher_Stopped; 
Các vấn đề liên quan