等待IObservable时发生System.ArgumentNullException

时间:2018-10-01 20:52:57

标签: asynchronous xamarin system.reactive

我试图等待可观察到的物体,我得到

  

System.ArgumentNullException

     

值不能为空。
  参数名称:来源

堆栈跟踪:

System.ArgumentNullException: Value cannot be null.
Parameter name: source
  at System.Reactive.Linq.Observable.GetAwaiter[TSource] (System.IObservable`1[T] source) [0x00003] in <370f6a6bb34048878534065376a195cb>:0
  at Lynkd.Models.Peripherals.RPHPeripheral+<readMacAddress>d__13.MoveNext () [0x00012] in /Users/ericbowman/Documents/Development/Mobile/LYNKD/Lynkd/Models/Peripherals/RPHPeripheral.cs:24
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.12.0.4/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:152
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00037] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.12.0.4/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.12.0.4/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.12.0.4/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128
  at System.Runtime.CompilerServices.TaskAwaiter.GetResult () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.12.0.4/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:113
  at Lynkd.Models.Peripherals.RPHGunbox+<InitializeAsync>d__1.MoveNext () [0x00020] in /Users/ericbowman/Documents/Development/Mobile/LYNKD/Lynkd/Models/Peripherals/RPHGunbox.cs:12
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.12.0.4/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:152
  at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__6_0 (System.Object state) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.12.0.4/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1018
  at UIKit.UIKitSynchronizationContext+<>c__DisplayClass1_0.<Post>b__0 () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.12.0.4/src/Xamarin.iOS/UIKit/UIKitSynchronizationContext.cs:24
  at Foundation.NSAsyncActionDispatcher.Apply () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.12.0.4/src/Xamarin.iOS/Foundation/NSAction.cs:125
  at at (wrapper managed-to-native) UIKit.UIApplication.UIApplicationMain(int,string[],intptr,intptr)
  at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) [0x00005] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.12.0.4/src/Xamarin.iOS/UIKit/UIApplication.cs:79
  at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0002c] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.12.0.4/src/Xamarin.iOS/UIKit/UIApplication.cs:63
  at Lynkd.iOS.Application.Main (System.String[] args) [0x00001] in /Users/ericbowman/Documents/Development/Mobile/LYNKD/Lynkd.iOS/Main.cs:17

这是我的出处:

protected async Task readMacAddress()
{
    await (Device.ReadCharacteristic(BleService.DeviceInformation, BleCharacteristic.MacAddress).Subscribe(
        onNext: Result =>
        {
            MacAddress = BitConverter.ToString(Result.Data).Replace("-", string.Empty);
        },
        onError: Error =>
        {
            throw new BleException("Cannot Retrive MAC Address, Error Reading MAC Address");
        },
        onCompleted: () =>
        {
            return;

    }) as IObservable<CharacteristicGattResult>);
}

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

我不知道API,但是我敢冒险说值的类型不是CharacteristicGattResult

可以是BluetoothGattCharacteristic`吗?

答案 1 :(得分:0)

事实证明,我对RX的使用太想了。您可以像这样简单地等待原始函数调用:

protected async Task readMacAddress()
{
    var macResult = await Device.ReadCharacteristic(BleService.DeviceInformation, BleCharacteristic.MacAddress);
    MacAddress = BitConverter.ToString(macResult.Data).Replace("-", string.Empty);
}

非常感谢Allan Ritchie指导我朝正确的方向发展。 (我正在使用的便捷Xamarin BLE库的作者)https://github.com/aritchie/bluetoothle