2017-04-25 103 views
0

我是藍牙LE編程的新手。我已經構建了我正在爲Android開發的程序,現在我試圖將它移植到C#/ Windows IoT。無法獲得GATT特性C#無法投射BluetoothLEDevice到IBluetoothLEDevice3

我正試圖讀取在BLE廣告軟件包中發送的健康溫度計數據。每次我嘗試訪問該設備的關貿總協定的服務,我的代碼拋出一個異常:

private TypedEventHandler<BluetoothLEAdvertisementWatcher, BluetoothLEAdvertisementReceivedEventArgs> OnAdvertisementReceived = async (w, eventArgs) => 
    { 
     BluetoothLEDevice device = await BluetoothLEDevice.FromBluetoothAddressAsync(eventArgs.BluetoothAddress); 
     try 
     { 
      var SERVICEUUID = eventArgs.Advertisement.ServiceUuids.FirstOrDefault(); 
      var CHARACUUID = new Guid("00001809-0000-1000-8000-00805f9b34fb"); 

      //FIXME: Getting invalid cast exception at this point 
      var gatt = await device.GetGattServicesForUuidAsync(SERVICEUUID); 
      Debug.WriteLine($"{device.Name} Services: {gatt.Services.Count}, {gatt.Status}, {gatt.ProtocolError}"); 

      var characs = await gatt.Services.Single(s => s.Uuid == SERVICEUUID).GetCharacteristicsAsync(); 
      var charac = characs.Characteristics.Single(c => c.Uuid == CHARACUUID); 

      //TODO: Parse temperature value to float 
      var val = await charac.ReadValueAsync(); 
     } 
     catch (Exception e) 
     { 
      Debug.WriteLine("Error getting characteristics: " + e); 
     } 
    }; 

我得到的例外是:

System.InvalidCastException: Unable to cast object of type 'Windows.Devices.Bluetooth.BluetoothLEDevice' to type 'Windows.Devices.Bluetooth.IBluetoothLEDevice3' 
at System.StubHelpers.StubHelpers.GetCOMIPFromRCW_WinRT(Object objSrc, IntPtr pCPCMD, IntPtr& ppTarget)  
at Windows.Devices.Bluetooth.BluetoothLEDevice.GetGattServicesForUuidAsync(Guid serviceUuid) 

的BluetoothLEDevice顯然實現了IBluetoothLEDevice3接口,所以不瞭解它如何不能被鑄造。任何幫助爲什麼發生這種情況非常感謝。如果有問題,我的設備是Raspberry PI3。提前致謝!

更新:

當調試代碼,我看到BluetoothLEDevice的DeviceAccessInformation財產拋出異常和它被傳播到頂級:

DeviceAccessInformation = 'device.DeviceAccessInformation' 扔 「System.InvalidCastException」類型的例外

UPDATE2

當我嘗試直接訪問GATT服務這種方式,我得到一個不同的異常:

BluetoothLEDevice device = await BluetoothLEDevice.FromBluetoothAddressAsync(eventArgs.BluetoothAddress); 
      var gattService = await GattDeviceService.FromIdAsync(device.DeviceId); 

在這種情況下,唯一的例外是

System.IO.FileNotFoundException:找不到系統找不到指定的文件 。在在System.Runtime.CompilerServices.TaskAwaiter`1 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任務 任務)在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務 任務):(0x80070002從HRESULT異常)。 GetResult() 在StempHub.StartupTask。 <> c。 < < -ctor> b__5_0> d.MoveNext()

+0

什麼是你的Windows iot核心版本? –

+0

It's 10.0.14393.1066 –

回答