Today I will begin with a series of posts based on my work with Xamarin and BlueTooth (LE) Low Energy devices. I’ve already written a couple of posts on how to work with Windows 10 Universal Apps and BlueTooth LE, in those examples I shared my experiences connecting a Windows 10 device to a heart rate sensor and display the HR value in an App. Today’s example is simpler: show all BLE Devices that can be detected by an Android device.
The first thing we will do is to create an Xamarin Android app.
Note: If you don’t know Xamarin, in the references section I have shared a link with resources to get started with Android.
The next step is to add a button and a multiline text in the main view. The app flow is easy: press the button, and then the BLE Devices found will be listed.
The next step is to identify the button and the text box. Also we need to creating a new instance of BlueToothLeScanner to list the BlueTooth Low Energy devices and subscribe to the ScanTimeOutElapsed() and DeviceDiscovered() events.
Important: From what I’ve seen, there are 2 modes of working with BlueTooth Devices. For normal devices, you must use the Adapter class, which is obtained from the BlueTooth system service, lines 30 to 32. In the case of BlueTooth LE, this is not a system service, simply have to create a new instance of BlueToothLeScanner and work with this object, lines 34 to 36.
Then in the DeviceDiscovered() event, we will show the new device in the box of text, line 45-29.
The same will do when the process of scanning-error, lines 39 to 43.
Finally, at the click of the button will begin the process of searching for BLE Devices.
The result of the app in operation is as follows
I’m not sure why the app raises so many times the same device. While that is not a problem, I have a couple of nights to take it forward 😉
You can download the code from here https://github.com/elbruno/Blog/tree/master/Xamarin/AndroidApp4
Saludos @ Madrid
– El Bruno, posts on BlueTooth LE https://elbruno.com/category/BLE/
– Polar Heart Rate Sensor H7, http://www.polar.com/us-en/products/accessories/H7_heart_rate_sensor
– Xamarin, Hello Android http://developer.xamarin.com/guides/android/getting_started/hello,android/
– Xamarin Android.Bluetooth.LE.BluetoothLeScanner http://developer.xamarin.com/api/type/Android.Bluetooth.LE.BluetoothLeScanner/
[Tags Visual Studio 2015, Xamarin, Bluetooth, BLE, Android, English Post]