2017-06-01 98 views
7

我正在使用DeviceWatcher爲UWP應用中的配對藍牙設備獲取DeviceInformation。我設置了DeviceWatcher像這樣在UWP中獲取已知藍牙設備的COM端口名稱

var requestedProperties = new string[] { "System.Devices.Aep.DeviceAddress", "System.Devices.Aep.IsConnected" }; 
var deviceWatcher = DeviceInformation.CreateWatcher("(System.Devices.Aep.ProtocolId:=\"{e0cbf06c-cd8b-4647-bb8a-263b43f0f974}\")", requestedProperties, DeviceInformationKind.AssociationEndpoint); // ClassGuid = {e0cbf06c-cd8b-4647-bb8a-263b43f0f974} includes all Bluetooth devices 
deviceWatcher.Added += DeviceWatcher_Added; 
deviceWatcher.Updated += DeviceWatcher_Updated; 
deviceWatcher.Start(); 

當DeviceWatcher_Added事件處理程序被調用我檢查,看看是否該設備是一個我是通過檢查它的名字和它提供的服務RfcommServiceId.SerialPort.Uuid感興趣的

一旦我有藍牙設備的DeviceInformation,我有興趣知道如何獲取COM端口?我可以在設備管理器中看到它,它被列爲「通過藍牙鏈接的標準串口(COM8)」,但我看不到如何以編程方式在UWP中獲得「COM8」。

我試圖使DeviceInformation成SerialDevice,因此我可以再拿到SerialDevice.PortName(C.F. this答案),但我對SerialDevice.FromIdAsync(deviceInfo.Id)調用失敗了System.Exception的:數據無效。

(NB一些誘人的答案,像thisthis,使用Windows管理Intrumentation WMI功能,但這些都無法在UWP)

+0

你如何確保deviceInfo.Id是你的設備的ID(COM8)?你能否顯示完整的代碼使這個異常:'System.Exception:數據無效'? –

+0

完整的代碼是[這裏](https://github.com/dumbledad/BluetoothCOMGleaner) – dumbledad

+0

鑑於你已經知道'deviceInfo.Name',想知道你是否可以從這裏獲得'Id'? '(等待DeviceInformation.FindAllAsync(SerialDevice.GetDeviceSelector()))。Single(di => di.Name == deviceInfo.Name).Id' –

回答

3

another questionRita建議要Serial UART sample這讓我看到了一種做這個。我不會將此標記爲一段時間的答案,因爲它似乎太過間接而不能成爲規範的方式。

雖然我的DeviceInformation爲我的UWP應用中配對的藍牙設備,但我也需要SerialDevice的列表,以便我可以匹配它們。這是最終的代碼。

public async Task<string> ComPort(DeviceInformation deviceInfo) 
{ 
    var serialDevices = new Dictionary<string, SerialDevice>(); 
    var serialSelector = SerialDevice.GetDeviceSelector(); 
    var serialDeviceInformations = (await DeviceInformation.FindAllAsync(serialSelector)).ToList(); 
    var hostNames = NetworkInformation.GetHostNames().Select(hostName => hostName.DisplayName.ToUpper()).ToList(); // So we can ignore inbuilt ports 
    foreach (var serialDeviceInformation in serialDeviceInformations) 
    { 
     if (hostNames.FirstOrDefault(hostName => hostName.StartsWith(serialDeviceInformation.Name.ToUpper())) == null) 
     { 
      try 
      { 
       var serialDevice = await SerialDevice.FromIdAsync(serialDeviceInformation.Id); 
       if (serialDevice != null) 
       { 
        serialDevices.Add(deviceInfo.Id, serialDevice); 
       } 
      } 
      catch (Exception ex) 
      { 
       System.Diagnostics.Debug.WriteLine(ex.ToString()); 
      } 
     } 
    } 
    // Example Bluetooth DeviceInfo.Id: "Bluetooth#Bluetooth9c:b6:d0:d6:d7:56-00:07:80:cb:56:6d" 
    // from device with Association Endpoint Address: "00:07:80:cb:56:6d" 
    var lengthOfTrailingAssociationEndpointAddresss = (2 * 6) + 5; 
    var bluetoothDeviceAddress = deviceInfo.Id.Substring(deviceInfo.Id.Length - lengthOfTrailingAssociationEndpointAddresss, lengthOfTrailingAssociationEndpointAddresss).Replace(":", "").ToUpper(); 
    var matchingKey = serialDevices.Keys.FirstOrDefault(id => id.Contains(bluetoothDeviceAddress)); 
    if (matchingKey != null) 
    { 
     return serialDevices[matchingKey].PortName; 
    } 
    return ""; 
}