Particle SDK for Xamarin

Just wanted to throw this out there for everyone. I took the objective-c iOS implementation and translated it to C# using HttpClient backed with ModernHttpClient. There is a working Xamarin.Forms sample included in the project to show how you can use it. The SDK is 99% complete, I haven’t taken the time to figure out two methods on the ParticleCloud class: SignUpWithCustomerAsync and RequestPasswordResetForCustomerAsync. In the future I will extend the Xamarin.Forms sample to include a Windows 10 app. I haven’t tested Windows, but I’m sure it all works fine since it is housed in a Portable Class Library.

I tested the SDK on both Xamarin.iOS and Xamarin.Android, but I can obviously only test it out so much. That’s where you come in! If you use it and something doesn’t work as expected, open up an issue and I’ll take a look. I tried to design the SDK in a way that would be lightweight, 100% asynchronous and utilizes IDisposable as much as I could.

Also, I just finished Events this weekend. Errors on the subscribed events will not invoke OnStart or OnError unless you manually wire it up. I did this for performance reasons. Unsubscribed events will return the cached data and dispose of everything else. I also left the option open to be able to manually construct EventSource, wire it up however you need, start it and then add it to the ParticleCloud SubscibedEvents if you want to.

It is important to also note that the Event Subscription setup will throw a HttpClient on a new thread. I was able to subscribe to two events fine, but I’m sure at some point you will feel the performance drag. So don’t subscribe to 10 events. If you need that, fork my implementation and make your own TaskFactory to better manage those events however you need.

Stop talking and give us the link!!!


If you have any questions about cross-platform mobile apps working with Particle, send me a private message and we can talk. I will also be the only person maintaining this repo, but I’m always happy to look at pull requests or any suggestions people have. I love Particle and what they have done. I started this project because I wanted to build a motorcycle tracker with the Electron. Now maybe I’ll have some time to finish that with this completed SDK!

5 Likes

@DrWatson I have looked at Xamarin a while back and it looked like a great platform for building iOS and Android apps. It looks like you have did a great job on getting Particle and its could functions and data available via Xamarin’s add building software which is great news :smiley:

So do you work for Xamarin? Or is this a side project for you?

Can you build out a working app using only Xamarin Forums software? Or is more required?

I have not used their software before but now that you have the SDK 99% complete then with some guidance it should be something I and others can figure out how to use to create professional looking cross platform mobile apps.

How long have you been using Xamarin?

@RWB Thanks! I do actually work for Xamarin, but this is a side project. It definitely took some time getting everything working. I remember how happy I was once I got flashing binary files to the Photon/Electron working :sunny:

Xamarin.Forms is a library that allows you to share your UI code, but it is also aimed at sharing a major portion of your code between platforms. For example, a “Button” in Xamarin.Forms will abstract to the platform specific control: UIButton for iOS, Button for Android and Button for Windows. The software you get through Xamarin gives you access to the C# native libraries they created: Xamarin.iOS and Xamarin.Android (Xamarin.Mac also). If you want to compile for those two frameworks, you would need one Xamarin.iOS license and one Xamarin.Android license. Xamarin.Forms can be used with any Xamarin license and only requires one active license to use it. Of course if you build for another target platform, I think you get a license prompt if you don’t have an active license for that platform.

If you’re interested in learning more about Xamarin.Forms, Xamarin published a book by Charles Prezold that is really good to learn from and it’s free also. You can also do cross-platform mobile with the traditional native C# Xamarin.iOS and Xamarin.Android. This gives you the most flexibility, but you have to create the UI on each platform. Both development techniques have a place in mobile development, just kind of depends on your specific use case.

I’ve been using Xamarin for about 2 years now. I started with Xamarin.Forms and have been with it since almost the beginning of it’s creation. I’m always happy to answer questions about mobile applications and Particle stuff. I went to IoT Devcon in Santa Clara last year and I realized how little there is available for IoT developers wanting to do something with mobile. You’re worrying about the IoT piece, which is already very complicated and probably the competitive advantage you have.

Once I found Particles REST API, I knew how easy it could all be with C# and Portable Class Libraries (PCL). I know the SDK says Xamarin, but it should work in any C# project that supports PCL. If something happens where ModernHttpClient doesn’t have a library for your target framework, fork the library and refactor the “new NativeMessageHandler()” that is passed into the HttpClient instantiations. It should work after that. So with this library, you can use Particle on a ASP.Net website, Windows 10 application, Xamarin application, Google Glass, Apple TV, Apple Watch…(the list goes on) AAAANNNNNNNDDDDDD you could also host it in Azure.

I just see a perfect world where a large block of Particle devices (Electron or Photon) are purchased and a middle layer is built on Azure between Particle and the Mobile client. This will give you the best security possible. Particle knows nothing of your customers and the mobile client knows nothing of the actual IoT device. Beautiful right? No portals to be hacked (assuming we can trust in Particle’s security they have baked in, and yes I do trust that). Everything runs through one controlled access point, Azure. I think that’s pretty cool. You could also cycle the token (for Particle API) on Azure every hour or so to add another layer.

I could go on and on. I love this stuff. Tinkering has become an addiction and I hope to build some awesome things with these technologies. I know a lot about mobile, not just Xamarin and I’m trying to learn more and more about IoT to put it all together. Happy to knowledge share or start some little group up to talk.

1 Like

Sounds great and I’m glad your here to talk about this.

I agree that there is no real easy way to get the data produced via the Particle devices into a iOS or Android App for us guys who are not professional programmers by trade.

I want a way to create a nice clean iOS and Android app that can display data generated by equipment we sell that use Photon & Electron devices inside of them to push their current status onto the web + into a database.

From my research I ended up trying IBM Bluemix to host Node Red, then ran some code that pulled in Particle publish events and then put that data into a Mongo Database. I heard that Mongo DB was a good database solution.

I figure if I just send all the data form the systems we sell that are online then I can pull from that database via Web HTML pages we create, and then for the mobile it would ideally be iOS & Android pulling data from the database also.

After looking at different options for iOS & Android apps I ended up just thinking that it’s probably just best to build a custom responsive website design that pulled system data in for that specific customer and then have that website look good on the laptop, iOS, and Android Browser. This way I can just eliminate the need for iOS and Android apps.

I think people love when their device has a custom app though. It’s confusing because it seems like building a simple mobile app pretty dam complicated for a beginner without some good examples to follow.

What are your thoughts about this?

I feel ya there. I try and build more realistic samples that show off how to use Xamarin.Forms. I have a Expense Reporting application that I created and it displays more about how to create the app in Xamarin.Forms. I kind of assume the database portion isn’t worth complicating the sample because there are so many choices. Maybe that will help a little. If you’re interested, I could PM you a ton more items.

Mongo DB is good. I’ve talked with many Xamarin developer teams that use it. I think it really comes down to whether you want SQL or NoSQL. Whichever makes sense for your team, that’s a win.

Native App will always beat a website anything. Hybrid applications are bad because you’re constantly running everything through the Javascript Interpreter built into the OS. You get access to some of the native controls, but not access to a lot available on the native device for iOS. That makes sense because iOS locks down security and having an open Javascript portal that can access everything is never safe.

You can definitely still build a beautiful hybrid application and there are many successful ones out there. I think it really depends more on what your use case is and what you are trying to do. For example the LinkedIn application is a hybrid application that is updated every 4 hours through continuous integration and automated builds. But with decisions like that come compromises. Ever notice how you really only have to login once to the LinkedIn app and then you’re always logged in? That is because the token used in their OAuth process has a long expiration. Not that big of a deal, but in IoT world, scary to me. So use case is important when evaluating the tech.

Generally mobile websites won’t give them feedback response you would like to have when working with IoT devices. You need the native performance. For example, with the SDK I created, the second I send a request off it fires on my Photon. I’m actually super impressed that is happens so fast. Event subscriptions are fired immediately when I publish from the Particle CLI. This is because ModernHttpClient is mapping to the native web handler. You’re website is doing everything behind the scenes using Javascript, which will always be slower than a NSUrlSession (iOS) or OkHttp (Android).

I started at Xamarin with 0 C# experience and no mobile experience. Their training for me was Xamarin University and I got certified as a mobile developer; then I was building apps in no time. It still took some practice, but I highly recommend a program like that. There are a lot of various programs available online. Pluralsight has a ton of videos available on multiple technologies. It definitely isn’t simple creating a mobile application, but it’s getting easier. Event doing native Obj-C or Swift for iOS isn’t that bad, the designer in XCode is pretty sweet. Android Studio is also really good.

Biggest difference in worlds between IoT dev and mobile dev environments, mobile dev environments have ridiculously awesome IDEs. I know I’m biased, but I love Xamarin Studio. It makes coding so easy and I can whip up wire frames using Xamarin.Forms in no time. I would definitely take that into consideration when evaluating. I’ve seen hybrid development and it doesn’t feel the same. If anyone has IoT IDE recommendations, I would love that :smile:

Do you think you could do a screen share video showing how easy it can be to use Xamrin Forms to create a iOS & Android app using a Xamrin template to display a variable that is live updated as its Particle.plublished() ?

I see that Xamarin University does have tons of videos and there are others that are free online to learn how to layout the app how you want. But learning exactly what needs to be done to pull the Particle data into the Xamarin app your creating would be something they are not going to have a video on.

I think guys here could use the Xamarin Forum training videos to learn how to make a simple app using the templates but were going to need help on how to exactly use the SDK code that you just provided us all.

I think there is a real need for a easy way to create simple mobile apps that pull in data that is generated by the Particle platform. Right now there is nothing for us that I know of that helps us do this without needing to hire somebody to do it.

What ever you can do I’m sure would be greatly appreciated by others on here that are working off a shoestring budget building hardware out of the garage. :smile:

I’m sure there are ways for you to make some money in return for this type of work also.

Since you know more than us it would be helpful if from your point of view if you could let us know which data base solution you think is best to use for storing the data were creating keeping cost and security in mind. And then give us a template to use for pushing our data to that particular database.

It looks like you have the SDK for then taking advantage of that data and using it in the iOS & Android apps which is what we all would really like to do I think. Expecially the guys making hardware that they want to sell eventually.

Your insight into a web based app display via the phones browser vs a native app is appreciated also.

I hear you on all this. You inspired me to make the plunge today. I created a blog today and put two posts up to start it off. The most recent post is about this SDK. Tomorrow I will take the time to create a video on how you can start with Xamarin Studio, new project and integrate this SDK just like you asked. It might take a few days to get everything edited together. I still need to make some standard graphics to brand it up a little.

http://xambrew.net/

Check it out, I’ll start posting to it regularly as I figure things out. If you ever want to know something or want to see something in my blog, you can email me at xambrew@gmail.com.

I will work on putting some stuff together on this and post it on the community. I’ll remember to mention you in the post so you can see it. Cost will be a little more difficult for me to gauge exactly, but I know a lot about what is available at free tiers for people on a budget like us. Stay tuned.

3 Likes

Sweet!

I read both your blog post and I’m excited you are taking this plunge.

I will attempt to follow along with you and build a iOS and Android app using the info and advice you provide.

First I will need directions on the easiest way to push Particle.publish data directly to a Database. I need a setup that can handle the same data being published from thousands of devices eventually.

Once that data is being sent to a database then I will be able to build a simple iOS and Android app to display that data on a nice clean simple app using Xamarin Form software as the template for the apps.

From what I had seen in the past from Xamarin it looked like it was similar to creating web pages which I have done quite a few times in the distant past, but pulling in data from a database or directly from a Particle.publish what something that I had no idea how to accomplish.

It’s nice to see you had zero knowledge of C# before you got started with Xamarin because that means you know exactly where alot of us are standing right now.

I just took the plunge and bought an Arduino a few years ago from Adafruit and just started playing around in hopes of learning enough to eventually be able to add new features to products we build. After a few years of playing around and playing with code I’m now able to make some pretty cool stuff that I never before thought was even possible. Now I’m able to turn what was once a dream into a working reality and that’s exciting.

The mobile phone industry has changed the face of society. People and kids are glued to their devices to a point it’s kinda scary :smile:

I think Xamarin’s mobile app platform + Particles Photon & Electron devices can be a match made in heaven when it comes to getting this IOT data into a easy to use format.

Before you know it every device you buy will connect to the internet and every device will have a App to monitor and control it.

Keep up the good work and can’t wait to see how this all plays out.

I’m sure Particle would be willing to help out some how if you can make something that is good for their community and their products.

This is incredible, thank you! Great help for our community.
Pinned the topic.

2 Likes

Thank you, this is awesome!

Unfortunately the app happens to throw an unhanded exception ,when trying to run it in my emulator.
When i have logged in and it is trying to get the devices (i assume), i get:

“System.ArgumentNullException: Value cannot be null.
Parameter name: collection”

I’m pretty new to Xamarin myself, and I can’t step inside the Xamarin code, but it points me to SyncContext.cs witch VS does not know of.

Information in attached pic is all i got to work with…

I was going to make it an issue on GitHub, but i could just be my configuration…

I’m i got VS 2015 Enterprise with update 3, Xamarin.Android 7.0 and the Xamarin.Forms.2.3.2.127 packet installed.

Thank you in advance!

Hi @simoher, I’m looking into this right now. I will post again this afternoon with an update, but hope to have a new nuget package distributed with updated samples. I’ve incorporated an additional InternetButton sample I created for a conference.

I’ve been working on another branch to take out modernhttpclient now that the native handlers is baked into Xamarin. I also changed the HttpClient instance to be a static singleton instead of using statements. This provides thread safety by using a public static instance to route all of the Particle calls.

You can feel free to check out that branch if you would like, I have verified it to work in Xamarin Studio. I’ll be testing the branch in Visual Studio today and then packing up the nuget package for distribution after testing criteria passes. I should probably document that and post it as well.

Thanks for trying this out, I love using these technologies together. It really works amazingly simple and the SDK I wrote supports flashing binary files. Imagine the fun you can have dynamically loading binaries to a Photon with multiple sensors and capabilities??? :particle: :doge:

Edited to add link to branch

@simoher sorry for the delay, I just got finished testing everything in Visual Studio and pushed out a new nugget package just now with everything updated. The samples work in VS now, I don’t believe it had anything to do with your environment. I found that everything worked perfectly in Xamarin Studio, but nothing worked in Visual Studio. I traced everything back to an issue that was raised awhile back about csproj compatibility between the two IDEs. Unfortunately for us, everything was broke from Visua Studio’s eyes. I restructured the entire solution to make more sense and to have better compatibility in the future. The SDK also has a build folder now that does auto-nuget package generation when built in release.

I hope this helps, please let me know if you have any questions!

2 Likes

Delay? You are quick! Thank you for looking in to this!
Yeah i have a few ides i wanna try out with this! :doge::doge::doge:

I pulled the newest branch in to a new Repo, but it looks like the the projects cant find any of the referred packages, even those which is not NuGet packages, probably not something with my env.

From the output view:

1>------ Build started: Project: MyDevices, Configuration: Debug Any CPU ------

1>C:\Program Files (x86)\MSBuild\14.0\bin\Microsoft.Common.CurrentVersion.targets(1611,5): warning : The referenced project ‘…\Particle\Particle.csproj’ does not exist.
1>C:\Program Files (x86)\MSBuild\14.0\bin\Microsoft.Common.CurrentVersion.targets(1820,5): warning MSB3245: Could not resolve this reference. Could not locate the assembly “Akavache, Version=4.1.2.0, Culture=neutral, processorArchitecture=MSIL”. Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors.
1>C:\Program Files (x86)\MSBuild\14.0\bin\Microsoft.Common.CurrentVersion.targets(1820,5): warning MSB3245: Could not resolve this reference. Could not locate the assembly “Akavache.Sqlite3, Version=4.1.2.0, Culture=neutral, processorArchitecture=MSIL”. Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors.

I synced you changes into the old project, now the packages is referred correctly, but i still run into the same problem as before, when trying to get the Devices:

System.ArgumentNullException: Value cannot be null.
Parameter name: collection

It’s like the devices isn’t loaded, and the GetDevicesAsync returns null and crashes the app.

I did a quick console app, login was successful but GetDevicesAsync returned null?

I’ve also begun to see this kind of error allot lately, not sure if they are related at all:

Severity	Code	Description	Project	File	Line	Suppression State
Warning		Found conflicts between different versions of the same dependent assembly. In Visual Studio, double-click this warning (or select it and press Enter) to fix the conflicts; otherwise, add the following binding redirects to the "runtime" node in the application configuration file: <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="System.Runtime" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" /><bindingRedirect oldVersion="0.0.0.0-4.1.0.0" newVersion="4.1.0.0" /></dependentAssembly></assemblyBinding><assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="System.IO" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" /><bindingRedirect oldVersion="0.0.0.0-4.1.0.0" newVersion="4.1.0.0" /></dependentAssembly></assemblyBinding><assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="System.Globalization" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" /><bindingRedirect oldVersion="0.0.0.0-4.0.10.0" newVersion="4.0.10.0" /></dependentAssembly></assemblyBinding><assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="System.Xml.ReaderWriter" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" /><bindingRedirect oldVersion="0.0.0.0-4.0.10.0" newVersion="4.0.10.0" /></dependentAssembly></assemblyBinding><assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="System.ObjectModel" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" /><bindingRedirect oldVersion="0.0.0.0-4.0.10.0" newVersion="4.0.10.0" /></dependentAssembly></assemblyBinding><assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="System.Linq" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" /><bindingRedirect oldVersion="0.0.0.0-4.1.0.0" newVersion="4.1.0.0" /></dependentAssembly></assemblyBinding><assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="System.Runtime.Extensions" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" /><bindingRedirect oldVersion="0.0.0.0-4.1.0.0" newVersion="4.1.0.0" /></dependentAssembly></assemblyBinding><assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="System.Collections" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" /><bindingRedirect oldVersion="0.0.0.0-4.0.10.0" newVersion="4.0.10.0" /></dependentAssembly></assemblyBinding><assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="System.Threading" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" /><bindingRedirect oldVersion="0.0.0.0-4.0.10.0" newVersion="4.0.10.0" /></dependentAssembly></assemblyBinding><assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="System.Diagnostics.Debug" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" /><bindingRedirect oldVersion="0.0.0.0-4.0.10.0" newVersion="4.0.10.0" /></dependentAssembly></assemblyBinding><assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="System.Reflection" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" /><bindingRedirect oldVersion="0.0.0.0-4.1.0.0" newVersion="4.1.0.0" /></dependentAssembly></assemblyBinding><assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="System.Linq.Expressions" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" /><bindingRedirect oldVersion="0.0.0.0-4.1.0.0" newVersion="4.1.0.0" /></dependentAssembly></assemblyBinding><assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="System.Net.Http" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" /><bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" /></dependentAssembly></assemblyBinding><assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="System.Text.RegularExpressions" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" /><bindingRedirect oldVersion="0.0.0.0-4.1.0.0" newVersion="4.1.0.0" /></dependentAssembly></assemblyBinding><assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="System.Threading.Tasks" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" /><bindingRedirect oldVersion="0.0.0.0-4.0.10.0" newVersion="4.0.10.0" /></dependentAssembly></assemblyBinding><assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="System.Dynamic.Runtime" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" /><bindingRedirect oldVersion="0.0.0.0-4.0.10.0" newVersion="4.0.10.0" /></dependentAssembly></assemblyBinding>	MyDevices.Droid

Okay @DrWatson, i figured it out… One of my devices was for some reason named null (not named at all) and thereby making the app crash… Glad i figured it out ! Looked at the RestAPI response and figured out something wasn’t quiet right, with the name of one of my devices :sweat: Now let the fun begin

Hey @DrWatson congrats!
I added your work to our community-enabled Particle clients web page, look here:
https://docs.particle.io/reference/community/

Keep up the great work and thanks for this great contribution.

1 Like

Hi @simoher
I’m also looking at this Xamarin Project. I see from a screen shot above that you also encountered the errors I initially found: The type or namespace name ‘…’ could not be found…

How I got there: I downloaded Visual Studio 2017 Community Edition and included everything that looked useful. Using the GitHub code, I opened up the solution for the PCL (which built OK). But the sample solutions did not build, (with the above errors). The output view showed heaps of are you missing a using directive or an assembly reference type errors for many things: IAps, Platform, AppQuery, AndroidApp etc…

How did you managed to resolve this? Ideally I 'd like to get the starting point of running a sample up in an emulator but I’m floundering a bit. I don’t suppose you could provide a few pointers to get to that point?

Cheers,

Al.