Company information
Fill up the form and we will contact you shortly.
ServoCode Sp. z o.o.
Jasionka 954E, 36-002 Jasionka, Poland
NIP: 8133719852
REGON: 364182909
KRS: 0000611643
My first approach to programming was at primary school. During Computer Science classes, we were navigating a little turtle in Logo. It was the time when I got fascinated by IT. Controlling the turtle was fun. Who knew that one day I will be creating mobile applications that are going to control real things like robots via Bluetooth LE? I definitely didn’t.
Now I’m a Mobile Developer who wants to teach you how to get things moving via Bluetooth with the help of Xamarin.Forms.
Alright, that’s enough of nostalgia, let’s get down to business. We use Bluetooth LE Protocole to the communication between devices. Each of them can have one of the following roles:
- GATT Client
- GATT Server
This construction is similar to the traditional Client – Server architecture.
Before we dig into the code, you must recognize some useful definitions:
Services - provided features and associated behaviors made to interact with the peripheral. Every service contains a collection of characteristics. Remember, first come services, then characteristics.
Characteristics - definition of the data divided into value and declaration. They occur in several modes: read, write, notify, indicate
Descriptor - an optional attribute that involves data on the specific value and ways of accessing it. It's located in characteristics.
UUID: Universally Unique ID that get transmitted over the air. This way, peripheral can pass the information to a central on services it provides.
Bluetooth LE: Client-Service scheme
Attention! You can’t leave this page without remembering the following stuff:
- You can connect Bluetooth LE peripheral to only one central device at a time,
- Client must request the Server to send data – it won’t do it on its own,
- Server initiates operations of Notifications and Indications are whenever a value in the database changes.
Mental notes made? Great, let's move on to another part!
For the communication of cross-platform app with Bluetooth LE, use ACR Reactive BluetoothLE Plugin library, you can get it here. Install the package I've mentioned above to every project. (Core, Android, iOS).
In Android, we add permissions to AndroidManifest.xml. Remember that in the case of Android 6+ and other later versions, you should ask the user for the permissions with the pop-up. For this purpose, use PermissionPlugin available here.
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<!--this is necessary for Android v6+ to get the device name and address-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
In the case of iOS, add following keys to the Info.plist file:
<key>UIBackgroundModes</key>
<array>
<!--for connecting to devices (client)-->
<string>bluetooth-central</string>
<!--for server configurations if needed-->
<string>bluetooth-peripheral</string>
</array>
<!--To add a description to the Bluetooth request message (on iOS 10 this is required!)-->
<key>NSBluetoothPeripheralUsageDescription</key>
<string>YOUR CUSTOM MESSAGE</string>
To find nearby devices, use the adapter from the plugin, which contains the following methods:
var _adapter = CrossBleAdapter.Current;
_adapter.Scan();
_adapter.ScanExtra();
_adapter.ScanInterval();
_adapter.ScanForUniqueDevices();
_adapter.ScanUntilDeviceFound();
_adapter.ScanForHeartRateSensors();
You can even attach an event that will inform you about finding a device:
_adapter.ScanForUniqueDevices().
Subscribe(device => DeviceDiscovered(device));
You can simply do this by calling a method on found device:
foundDevice.Connect();
Before connecting with a device, you can use events like:
foundDevice.WhenConnected().Subscribe(result => Connected(result));
foundDevice.WhenAnyCharacteristicDiscovered().Subscribe(result => CharacteristicsDiscovered(result));
foundDevice.WhenStatusChanged().Subscribe(status => StatusChanged(status));
They will help you with monitoring of the connection with the device and finding characteristics that are necessary for communication.
Disconnecting from the device occurs after you run the following code:
if (_connectedDevice.IsConnected())
_connectedDevice.CancelConnection();
READvar result = await characteristic.Read(); // use result.Data to see response
WRITEawait characteristic.Write(bytes);
NOTIFICATIONScharacteristic.EnableNotifications();
characteristic.WhenNotificationReceived().Subscribe( result => {
//result.Data to get at response
});
If you have any questions, feel free to ask 'em in the comments :)
SEE ALSO
Check out other awesome articles from Xamarin Month!
Configuration of Continuous Integration for Xamarin using GitLab
Dependency Injection in C# - how to make it?
Have an idea ? Let’s talk
Fill up the form and we will contact you shortly.
ServoCode Sp. z o.o.
Jasionka 954E, 36-002 Jasionka, Poland
NIP: 8133719852
REGON: 364182909
KRS: 0000611643
Your message has been sent!
Your message has been sent!