Xamarin Integration

I am attempting to use the meta wear with Xamarin forms PCL.

The Nuget package won't work because it is specific to UWP and throws error. 'You are trying to install this package into a project that targets '.NETPortable,Version=v4.5,Profile=Profile111', but the package does not contain any assembly references or content files that are compatible with that framework'.

Has anyone got Xamarin working with meta wear?

I am going to try and get the C++ library compiled, then I attempt to port the UWP wrappers.

I am trying to build the metawear c++ API into a project.
How do you do that in Visual Studio 2015? I tried using the steps on github, but they were very brief and wasn't sure exactly what I was doing.

Also when I tried to use nmake in the VS Command Prompt, it threw a fatal syntax error.

Config.mk(4) : fatal error U1034: syntax error : separator missing





  • What issues are you running into when creating a VS2015 project?
  • To be honest, I haven't built a C++ project from an external source before and am not sure of the exact process in Visual Studio.

    I added a C++ Win32 project to my solution. Application type DLL? Unchecked Precompiled header.

    Properties -> Config Properties -> VC++ directories, Included Metawear src folder from API downloaded from github.

    Then Properties -> Config Properties -> C/C++ -> Preprocessor -> Added METAWEAR_DLL_EXPORTS to Preprocesor Definitions

    Then built solution.

    I dont know if I am missing something, I can't seem to see any of the metawear .h files etc in my C++ project.
  • You need to select the files themselves to add them to the project.

    I don't know what kind of DLL Xamarin expects; you should double check that Win32 is the correct DLL type for calling C code in a Xamarin project.
  • Ok, I will see how I fare.

    Any reason why the nmake would fail, that I mentioned earlier?

  • Because that is not an nmake file.
  • Hi Eric,

    I have a better idea of what type of files I need for it to work.

    For Android, I need to build the C++ source code into a Dynamic Library (.so) and for iOS: Static Library (.a) for each type of ABI that each of the phones use.

    I am trying to build the C++ into the dynamic library (.so) first, and I am getting stacks of errors when it is trying to compile. A lot of errors are along the lines of "cannot open source file "metawear/core/logging.h".

    Are you able to tell me if what I am trying to achieve is possible?

    For example, are you able to build the C++ into a .so file for armeabi-v7a?
  • edited June 2016
    The error you quoted is a result of not properly configuring the include paths.  If you are building with gcc, you need to configure Gradle to have the same compiler flags as outlined in the Makefile.

    I have not used the Android NDK beyond compiling the example code so I can't give you a definitive answer either way.  However, I assume what you are doing is possible since the NDK is calling gcc or cl behind the scenes so you would just need to be using the appropriate compiler version and have the correct compiler flags.

    Alternatively, why not just use the bindings to call into the Android and iOS libraries?
  • edited June 2016
    I tried adding those compiler flags and am still getting errors.

    in metawearboard.cpp 243: "cannot use typeid with -fno-rtti" for ARM

    "metawear/core/datasignal.h' file not found in datasignal.cpp" for ARM64, x86

    On your last remark, it says on the Android Github readme "The MetaWear API aar comes packaged with the library's source code." 

    I couldn't find the .aar file. I tried building the aar myself and when I tried to create the binding library, I got these errors.

    "DefaultMetaWearBoard.Bma255AccelerometerImpl' does not implement interface member 'IAccelerometer.RouteData()'. 'DefaultMetaWearBoard.Bma255AccelerometerImpl.RouteData()' cannot implement 'IAccelerometer.RouteData()' because it does not have the matching return type of 'IAccelerometerSourceSelector'."

    The same error for each type of board it seems (11 errors)
  • In order of the issues you listed:

    1) Remove the '-fno-rtti' flag.
    2) Make sure all gcc build variants have the same compiler flags.  It looks like you only have the flags set for the x64 build.
    3) There is no 'IAccelerometer' interface in the Android API.  Is Xamarin auto generating these class names?
  • I was still getting errors with the C++, I think it may be to do with the compiler flags.

    Xamarin automatically creates C# wrappers from the AAR library.

    I found something in the Xamarin documentation that helped me solve the interface issues.

    On the bright side, I managed to get the Android binding library to build finally. I will let you know how I go :)
  • edited June 2016
    Hi Eric,

    I am working on getting the FreeFall example working.

    I have got a connection going and I am at the point where I am subscribing the route manager.

    When I call the routeManager,subscribe(ACCEL_DATA, new RouteManager,MessageHandler()).

    It gets the IntPtr Handle of 0x100dd6 and throws a NullPointerException when trying to access BMI160Accelerometer.NoMotionConfigEditor from configureNoMotionDetection

    I have followed your video tutorial precisely, I am not sure why it is trying to configure no motion detection.


  • MessageHandler is an interface.  Please see the Free Fall source code after part C regarding setting up a stream.
  • Yes I have set up the override for process, but the code throws an exception before process gets called.
  • Also, I just tried running your FreeFall detector project straight out of GitHub.

    I got a NullPointerException when I tried to press Start.

    Attempt to invoke interface method 'void com.mbient.metawear.module.Accelerometer.enableAxisSampling()'

    accelModule is null

    Maybe this is relevant to my problem?
  • It was an issue with my porting. When implementing a Java interface in C#, you have to derive from Java.Lang.Object.

    I have accelerometer data!
  • edited June 2016
    Nice, good to see everything is sorted out.
  • Hi @Damian,
    I see you are able to use MetaWear in a Xamarin.Forms project. 
    Did you end up using the .jar binding or the C++ .so?
    I'm trying the binding route but I'm having issues. I explain it all at https://forums.xamarin.com/discussion/80614/binding-derived-interfaces-with-overridden-methods
    Can you please share some info on how you did it?
    Thanks in advance.
  • Hi @aalmada,

    I wasn't able to get it working perfectly.

    I could see the values if I put a watch, but I couldn't access the individual axes from the Cartesian, and didn't have time to work on it any further at the time.

    I ended up doing it in a hacky sort of workaround until I had more time to figure out the problem.

    var value = message.GetData(cartesian).ToString();
                    var regexPattern = @[(){}\\s,]+;
                    var axes = Regex.Split(value, regexPattern);

    The axes variable returns an array with the 3 values in it, which I parsed to doubles.

    If you figure out a better way let me know.


  • Hi @Damian,
    I have an alternative solution but I'm still working on a project where I can test it.
    The source is available at https://github.com/aalmada/MetaWear.Droid
    The interesting bits are in the Metadata.xml file and in the Additions folder.
    Feel free to submit issues and pull requests.
  • @Damian
    I'm still strugling with this issue. I now get it to compile but I can't bind to the MetaWearBleService. I'm doing the following:
    var context = Android.App.Application.Context;
    var intent = new Intent(context, typeof(MetaWearBleService));
    var bound = context.BindService(intent, connection, Bind.AutoCreate)
    BindService() always returns false...

This discussion has been closed.