I'm trying to use the accelerometer with Monotouch but a got an issue with MotionManager. Updates stop to be sent after 2-3 sec.

Here is the code. (Everything is created in the constructor of my main UIView).

CMMotionManager motionManager = new CMMotionManager();
motionManager.AccelerometerUpdateInterval = 0.5;
motionManager
    .StartAccelerometerUpdates(NSOperationQueue.CurrentQueue, 
        delegate(CMAccelerometerData data, NSError error) 
{
    myLabel.Text = data.Acceleration.X.ToString("0.0000");        
});

With UIAccelerometer, it's working:

UIAccelerometer acc = UIAccelerometer.SharedAccelerometer;
acc.UpdateInterval = 0.5;
acc.Acceleration += delegate(object sender, UIAccelerometerEventArgs e) 
{
    myLabel.Text = e.Acceleration.X.ToString("0.0000"); 
};

Any idea?

有帮助吗?

解决方案

There's not enough source code to see if you keep a reference to your CMMotionManager instance. Otherwise the GC could dispose of it, which would stop any update.

In comparison you are using the shared UIAccelerometer (which won't ever be GC'ed) while you're creating your own CMMotionManager (which could be).

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top