首页 > 解决方案 > What is the easiest way to get the latest value from an Observable in C#

问题描述

The question is very basic but I'm struggling none the less since this is not my main area of work. I'm trying to debug a Bluetooth connection and want to extract the current RSSI value of the connection and log that in a log.

I've googled the problem but find that most solutions go over my head or seem overly complex since I just want the latest value.

I'm using a Bluetooth plugin which has an interface for a device called IDevice. That interface has a ReadRssi method that should somehow be able to yield an int and I would need the latest int from it.

IObservable<int> ReadRssi();

After some googling the closest I've gotten is:

module.Device.ReadRssi().Subscribe(v => 
{
    currentRSSI = v;
    Logger.LogInfo(LogOperationType.Convert, $"Current RSSI {currentRSSI}");
});

where the Device is a part of a module class.

The above compiles and the app runs just fine but no values are output to the log. It might be a scope issue or I might be misusing something but my goal as mentioned is to just get the last value from the observable in an unobtrusive manner. An ugly hack is good enough in this case and all help is greatly appreciated.

标签: c#system.reactive

解决方案


推荐阅读