Question

I am creating a Xamarin.Ios Bluetooth app and my question is following:

Is there any way to discover characteristics of all services in the peripheral at once. I mean to say if i have three services (a,b,c) and i call peripheral.discoverCharacteristic(a); three time a,b,c and then receive the characteristic call back in discover characteristic of all discovered characteristics of all services. I know it can do the call back everytime when i call peripheral.discovercharacteristic() but i need to have discover all characteristic from all service in place before i do anything. This is my use requirement. Please help !

Was it helpful?

Solution

Not possible directly.

You will have to use one DiscoverCharacteristic call for every single service, then process the results using custom app logic.

OTHER TIPS

Okay, In xamarin it only works with discovering for characteristic for a service ,not like a batch discovery. I will need to discover characteristics for a given service one by one and process it and discover again for next service as Etan explained in the first answer. It works for me and will think of changing my use case requirements accordingly. Thanks Everyone for sharing your thoughts.

When I've discovered the services of a CBPeripheral, I call discoverCharacteristics once with all the characteristics that I'm looking for;

for (CBService *service in peripheral.services)
{
    DDLogInfo(@"Kicking discovery of characteristics for servive %@", service);
    [peripheral discoverCharacteristics:@[[CBUUID UUIDWithString:CHARACTERISTIC_A],
                                          [CBUUID UUIDWithString:CHARACTERISTIC_B],
                                          [CBUUID UUIDWithString:CHARACTERISTIC_C],
                                          [CBUUID UUIDWithString:CHARACTERISTIC_D]]
                             forService:service];
}

Then, in the didDiscoverCharacteristicsForService:error:, I loop through the discovered results and store references to the characteristics that I'll need later on, and that's that.

for (CBCharacteristic *characteristic in service.characteristics)
{
    if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:CHARACTERISTIC_A]])
        self.currentCharacteristicA = characteristic;
    else if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:CHARACTERISTIC_B]])
        self.currentCharacteristicB = characteristic;
    else if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:CHARACTERISTIC_C]])
        self.currentCharacteristicC = characteristic;
    else if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:CHARACTERISTIC_D]])
        self.currentCharacteristicD = characteristic;
}

And, obviously, cleanup the self.currentCharacteristic* nicely when disconnected.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top