Cannot find peripheral when scanning for specific service CBUUID

别说谁变了你拦得住时间么 提交于 2019-12-19 04:08:32

问题


When I use:

CBUUID * uuid = [CBUUID UUIDWithString:@"1800"]; // GAP
DEBUG_LOG(@"CBUUID: %@",uuid); // CBUUID: Generic Access Profile
_centralMan = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
[_centralMan scanForPeripheralsWithServices:[NSArray arrayWithObject:uuid]
                                    options:nil];

I cannot find my peripheral, but when I use:

_centralMan = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
[_centralMan scanForPeripheralsWithServices:nil
                                    options:nil];

…it shows up immediately.

When I connect, I am able to discover/read/write to all the services I am expecting. My understanding is that GAP makes this possible. I have also tried CBUUID's for other services that I know are running (specifically "1804", TX power service), to no avail; the scan never discovers anything.

Even though the services are running (I can communicate w/ them upon connecting), when I scan passing the service UUID array as nil and this delegate method is called...

-(void) centralManager:(CBCentralManager *)central 
 didDiscoverPeripheral:(CBPeripheral *)peripheral 
     advertisementData:(NSDictionary *)advertisementData 
                  RSSI:(NSNumber *)RSSI {
    DEBUG_LOG(@"Found peripheral w/ UUID: %@ RSSI: %@ \n AdvData: %@",peripheral.UUID,RSSI,advertisementData);
    DEBUG_LOG(@"Services found: %d",peripheral.services.count);
    for (CBService * service in peripheral.services) {
        DEBUG_LOG(@"Found service: %@ w/ UUID %@",service, service.UUID);
    }
}

…it always reports "Services found: 0". Should I expect the services array to be populated w/ services found during the scan? (I assume this is at least partially possible, based on scanForPeripheralsWithServices:[NSArray arrayWithObject:uuid] options:nil.)

Could this be a problem w/ my peripheral's service advertisements? Am I using the API incorrectly? Any guidance is much appreciated! :-)

PS: I'm pretty green w/ Bluetooth.

PPS: I control the source to the peripheral (it's a CC2540DK). So I'd be as likely to believe the problem is there as it is here.


回答1:


Turns out our peripheral code was not advertising the UUIDs a la this Bluetooth spec.

As soon as we dropped them into the advertising packet, iOS picked them up no sweat.



来源:https://stackoverflow.com/questions/11216248/cannot-find-peripheral-when-scanning-for-specific-service-cbuuid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!