Bluetooth LE plugin for Xamarin released

The first release candidate of our BLE plugin is now available.

Posted by Sven-Michael Stübe on May 13, 2016

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.

var state = ble.State;
ble.StateChanged += (s, e) => 
{
    Debug.WriteLine($"The bluetooth state changed to {e.NewState}");
};

Discover

Finding devices is the first step. You can discover

all advertised devices

adapter.StartScanningForDevicesAsync();

devices with advertised services

adapter.StartScanningForDevicesAsync(new[] { Guid.Parse("ffe0ecd2-3d16-4f8d-90de-e89e7fc396a5") })

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.

adapter.StartScanningForDevicesAsync(dev => dev.Name.Contains("Sven"));

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 ;)

var device = adapter.DiscoverDeviceAsync(dev => dev.Name.Equals("Adrian")));

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.

await adapter.ConnectToDeviceAync(device);
await adapter.DisconnectDeviceAsync(device);

Services

Getting the services of a device is easy.

var services = await device.GetServicesAsync();
// or

var service = await device.GetServiceAsync(Guid.Parse("ffe0ecd2-3d16-4f8d-90de-e89e7fc396a5"));

Characteristics

Getting the characteristics of a service is as easy as getting a service of a device.

var characteristics = await service.GetCharacteristicsAsync();
// or

var characteristic = await service.GetCharacteristicAsync(Guid.Parse("ffe0ecd2-3d16-4f8d-1234-000000000000"));

After you have found your characteristic, you can read, write or subscribe to notification.

var data = await characteristic.ReadAsync();
data[0] = 0x13;
await characteristic.WriteAsync(data);

characteristic.ValueUpdated += (s, e) =>
{
    Debug.WriteLine("New value: {0}", e.Characteristic.Value);
};
characteristic.StartUpdates();

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.

if (await device.UpdateRssiAsync())
{
    Debug.WriteLine("The new rssi value is: {0}", device.Rssi);
}

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 NuGet NuGet MvvMCross.

Install-Package Plugin.BLE -Pre
// or
Install-Package MvvmCross.Plugin.BLE -Pre

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!