私はBluetooth 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);
}
};
私が手に例外がある:私は、デバイスのGATTサービスにアクセスしようとするたびに、私のコードは、例外がスローされます
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インタフェースを実装するので、私はしないでくださいどのようにキャストすることができないかを理解する。なぜこのようなことが起こったかについての助けは大いに感謝します。私のデバイスはラズベリーのPI3です。前もって感謝します!
更新:
コードをデバッグするとき、私はBluetoothLEDeviceのDeviceAccessInformationプロパティは例外をスローし、それがトップレベルに伝播されることを参照してください。
DeviceAccessInformation = 'device.DeviceAccessInformation' を投げました「System.InvalidCastException」タイプの例外
アップデート2
私が直接このようにGATTサービスにアクセスしようとすると、私は別の例外を取得:
BluetoothLEDevice device = await BluetoothLEDevice.FromBluetoothAddressAsync(eventArgs.BluetoothAddress);
var gattService = await GattDeviceService.FromIdAsync(device.DeviceId);
この場合、例外は
System.IO.FileNotFoundExceptionです:システムファイル が見つかりません。 (HRESULTからの例外:0x80070002が) System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(タスク タスク)でSystem.Runtime.CompilerServices.TaskAwaiter`1で System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(タスク タスク)で。 GetResult() at StempHub.StartupTask。 <> c。 < < -ctor> b__5_0> d.MoveNext()APIを新たにv10.0.14965で導入されたが、このバージョンでは実装されていませんし、それは15003で実装されている
Windowsのiotコアバージョンとは何ですか? –
It's 10.0.14393.1066 –