Adrian (@secelead) and I pushed the first release candidate of our bluetooth low energy plugin for xamarin and MvvmCross to NuGet, today. The code is available on github. It is based on Monkey robotics, but heavily refactored.
It currently supports Android and iOS and the following features are implemented.
Bluetooth State
You can check if the device supports bluetooth LE, check whether it is turned on or off and subscribe to any changes on this state.
Discover
Finding devices is the first step. You can discover
all advertised devices
devices with advertised services
devices that fullfil a custom filter expression
You can provide any Func<IDevice, bool>
you want. You can access the simple properties of a device (like its name) or use the advertisement records to filter devices on a more complex base.
a single device
In some use cases you might be only interested in a particular device (e.g. last connected device). In these cases starting the discovery, handling events, etc. is annoying. We implemented this for you ;)
Connect / Disconnect
For connecting the perviously discovered device, you have to use the adapter. We are currently discussing to move the connect and disconnect functions to IDevice
.
Services
Getting the services of a device is easy.
Characteristics
Getting the characteristics of a service is as easy as getting a service of a device.
After you have found your characteristic, you can read, write or subscribe to notification.
characteristic.StartUpdates()
starts listening for notifications and updates the Value
of characteristic
when the notification has been received successfully. You can stop listening with characteristic.StopUpdates()
.
Reading Rssi
Initially, you get the Rssi value when you discover the device. If you want to update it, you can use UpdateRssiAsync
. Due some limitations, this works only while being connected to the device.
Give it a try
If you want to try it you have two options. You can simply clone the repository and build and execute the sample applications, or integrate it directly into your own app via NuGet .
Sample app
We provide a sample Xamarin.Forms app, that is a basic bluetooth LE scanner. With this app, it’s possible to
- check the bluetooth LE status
- discover devices
- connect/disconnect
- discover the services
- discover the characteristics
- see characteristic details
- read/write and register for notifications of a characteristic
Have a look at the code and use it as starting point to learn about the plugin and play around with it.
Our goals for v 1.0
Adrian started this project last year as MvvmCross plugin. He fixed alot of bugs and made it stable. It has been in v 0.9.x for quite a while and we wanted to bring it to v 1.0. In recent weeks, we refactored it with the goals
- provide a “vanilla” Xamarin plugin
- provide a sample app
- streamline the API (make it asnyc and use cancelable)
- get rid of old code
What next?
We are aiming for a soon stable release. We invite everybody to give some feedback and suggestions. Feel free, to create an issue on github or contact us directly on twitter or slack. The open issues can be found on github.
The main todos are
- better build process
- more documentation
- get rid of fat single gatt callback (split it)
- unit tests
- extend
IBluetooth
- request permission
- enable bluetooth
- basic info like IsBleAvailable, MaxSupportedVersion, …
Background Photo by Jona Nalder / CC BY
Found a typo? Send me a pull request!