Coder Social home page Coder Social logo

crossgeeks / devicesensorsplugin Goto Github PK

View Code? Open in Web Editor NEW
12.0 6.0 6.0 30 KB

Device Sensors Notification Plugin for Xamarin, Tizen and Windows

License: MIT License

C# 100.00%
device sensors accelerometer xamarin gyroscope magnetometer barometer pedometer ios plugin

devicesensorsplugin's Introduction

DeviceSensors Plugin for Xamarin iOS, Android, UWP and Tizen

Simple cross platform plugin to access device sensors.

Features

  • Access to accelerometer
  • Access to gyroscope
  • Access to magnetometer
  • Access to barometer
  • Access to pedometer

Setup

Platform Support

Platform Version
Xamarin.iOS iOS 7+
Xamarin.Android API 13+
Windows 10 UWP 10+
Tizen 4.0+

API Usage

Call CrossDeviceSensors.Current from any project or PCL to gain access to APIs.

iOS Setup

Add to your Info.plist NSMotionUsageDescription key:

<key>NSMotionUsageDescription</key>
<string>This app needs to be able to access your motion use</string>

Getting Started

Device Sensor interface

public interface IDeviceSensor<T>
    {
        /// <summary>
        /// Check if sensor supported
        /// </summary>
        bool IsSupported { get; }
        /// <summary>
        /// Check if sensor is active
        /// </summary>
        bool IsActive { get; }
        /// <summary>
        /// Get latest sensor reading
        /// </summary>
        T LastReading {get;}
        /// <summary>
        /// Sets/get sensor report interval
        /// </summary>
        int ReadingInterval { get; set; }
        /// <summary>
        /// Starts sensor reading
        /// </summary>
        void StartReading(int readingInterval = -1);
        /// <summary>
        /// Stops sensor reading
        /// </summary>
        void StopReading();

        /// <summary>
        /// Sensor reading changes event
        /// </summary>
        event EventHandler<DeviceSensorReadingEventArgs<T>> OnReadingChanged;
    }

Available sensors

    public interface IDeviceSensors
    {
       IDeviceSensor<VectorReading> Accelerometer {get; }
       IDeviceSensor<VectorReading> Gyroscope { get; }
       IDeviceSensor<VectorReading> Magnetometer { get; }
       IDeviceSensor<double> Barometer { get; }
       IDeviceSensor<int> Pedometer { get; }
    }

Usage sample:

Accelerometer

    if(CrossDeviceSensors.Current.Accelerometer.IsSupported)
    {
         CrossDeviceSensors.Current.Accelerometer.OnReadingChanged += (s,a)=>{

         };
         CrossDeviceSensors.Current.Accelerometer.StartReading();
    }
   

Gyroscope

    if(CrossDeviceSensors.Current.Gyroscope.IsSupported)
    {
         CrossDeviceSensors.Current.Gyroscope.OnReadingChanged += (s,a)=>{

         };
         CrossDeviceSensors.Current.Gyroscope.StartReading();
    }
   

Magnetometer

    if(CrossDeviceSensors.Current.Magnetometer.IsSupported)
    {
         CrossDeviceSensors.Current.Magnetometer.OnReadingChanged += (s,a)=>{

         };
         CrossDeviceSensors.Current.Magnetometer.StartReading();
    }
   

Contributors

devicesensorsplugin's People

Contributors

rdelrosario avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

devicesensorsplugin's Issues

iOS Accelerometer StartReading silent crash (Potential bug)

Hey,

I've been using this plugin in my Xamarin app and it works fine on Android. I was having problems with iOS where the StartListening call would just crash silently. I couldn't get your project to restore properly in my solution so I just took your code and added it in to test what the problem was.

I am calling the StartReading function on the Accelerometer from a System.Timers.Timer.Elapsed callback, meaning there's no NSOperationQueue.CurrentQueue since this value only reflects the operation queue that launched the opration. This causes a silent crash and doesn't provide any feedback from your plugin.

I would make the change and make a PR, but as mentioned above the project is not restoring in my solution (which contains .netstandard2 libs) and I don't want to submit PR not tested by me.

I fixed the problem by just using NSOperation.MainQueue instead of CurrentQueue and the reading works again, just as a workaround for now.

Suggestions:

  • Use NSOperation.MainQueue in Platforms/iOS/AccelerometerDeviceSensor.cs line 51
  • Additionally: Use the NSError returned (if any) to throw an exception in OnAccelerometerChanged
  • Additionally: Surround _motionManager.StartAccelerometerUpdates(NSOperationQueue.CurrentQueue, OnAccelerometerChanged); in a try catch and throw a custom exception to indicate "Not called from the UIThread" or something in those regards or you can make sure the call is being performed on the UI thread to ensure that NSOperationQueue.CurrentQueue is not null.

If I missed anything and it's my code causing the issue you can just close this.

I hope this helps

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.