Question

I am developing a Xamarin.iOS Bluetooth enabled app and i have some hurdles which i need to overcome. I am trying to make a connection from C# iOS app to peripheral device; I can connect successfully to the device but after discovering services and characteristics of the device, I need to catch particular service so that i can further use it for different purposes.

Now, the issue is how can i catch particular service from list of services in the cb peripheral object. Each service is identified by CBUUID object and i already know the CBUUID object that the device is returning, but i am not sure how can i compare them so that i know i catch the appropriate service.

Was it helpful?

Solution

If you are targeting iOS 7.1 (or later) then you can compare the System.String Uuid property of CFUUID. That's the easiest way. E.g.

if (cbuuid1.Uuid == cbuuid2.Uuid)
    Console.WriteLine ("Equal");
else
    Console.WriteLine ("Different");

Otherwise (iOS 7.0 and earlier) you'll need to compare the NSData Data properties of CFUUID. First compare their length and, if equal, compare each byte inside them. E.g.

bool equal = false;
using (var d1 = cbuuid1.Data)
using (var d2 = cbuuid2.Data) {
    if (d1.Lenght == d2.Length) {
        for (int i=0; i < d1.Lenght; i++) {
            if (d1 [i] != d2 [i]) {
                equal = false;
                break;
            }
        }
    }
}
Console.WriteLine (equal ? "Equal" : "Different");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top