iOS 7 ANCS: Discovering the primary ANCS Service U

2019-01-31 02:23发布

问题:

Under iOS7, is the primary ANCS Service meant to be constantly advertised, or does it need to be enabled in obfuscated settings / implemented using a custom CBPeripheralManager (using the Apple-specified Service and Characteristic UUIDs) for a potential Notification Consumer to successfully discover it and subscribe?

The Apple documentation (both the CoreBluetooth Programming Guide, and the ANCS Specification) are surprisingly bereft of any information on this. They seem to hint at requiring a custom implementation, but this is just conjecture on our part.

Given the primary ANCS Service UUID: 7905F431-B5CE-4E99-A40F-4B1E122D00D0, performing a scan yields no hits. Scanning the entire BLE spectrum, as expected, yields hits for other BLE devices, but not a single ANCS device.

EDIT 1:

Defining a custom CBPeripheralManager and manually adding the Apple-specified ANCS Service with its associated Characteristics fails, with the NSError: Error Domain=CBErrorDomain Code=8 "The specified UUID is not allowed for this operation."

Consequently, it appears that the Service UUID is reserved by Apple (as it should be), and we cannot enable it in this manner.

Any insight is greatly appreciated; we've reached out to Apple about this, and will update when we hear from them.

The code below reproduces the NSError mentioned above:

// define the ANCS Characteristics
CBUUID *notificationSourceUUID = [CBUUID UUIDWithString:@"9FBF120D-6301-42D9-8C58-25E699A21DBD"];
CBMutableCharacteristic *notificationSource = [[CBMutableCharacteristic alloc] initWithType:notificationSourceUUID properties:CBCharacteristicPropertyNotifyEncryptionRequired value:nil permissions:CBAttributePermissionsReadEncryptionRequired];
CBUUID *controlPointUUID = [CBUUID UUIDWithString:@"69D1D8F3-45E1-49A8-9821-9BBDFDAAD9D9"];
CBMutableCharacteristic *controlPoint = [[CBMutableCharacteristic alloc] initWithType:controlPointUUID properties:CBCharacteristicPropertyWrite value:nil permissions:CBAttributePermissionsWriteEncryptionRequired];
CBUUID *dataSourceUUID = [CBUUID UUIDWithString:@"22EAC6E9-24D6-4BB5-BE44-B36ACE7C7BFB"];
CBMutableCharacteristic *dataSource = [[CBMutableCharacteristic alloc] initWithType:dataSourceUUID properties:CBCharacteristicPropertyNotifyEncryptionRequired value:nil permissions:CBAttributePermissionsReadEncryptionRequired];

// define the ANCS Service
CBUUID *ANCSUUID = [CBUUID UUIDWithString:@"7905F431-B5CE-4E99-A40F-4B1E122D00D0"];
CBMutableService *ANCS = [[CBMutableService alloc] initWithType:ANCSUUID primary:YES];
ANCS.characteristics = @[notificationSource, controlPoint, dataSource];

// define the Advertisement data
NSMutableDictionary *advertisementData = [NSMutableDictionary dictionary];
[advertisementData setValue:@"CUSTOM_ANCS" forKey:CBAdvertisementDataLocalNameKey];
[advertisementData setValue:@"7905F431-B5CE-4E99-A40F-4B1E122D00D0" forKey:CBAdvertisementDataServiceUUIDsKey];

// publish the ANCS service
[self.peripheralManager addService:ANCS];

回答1:

As a belated answer to this question, now that Mavericks is out, here is what we've come up with.

Our initial efforts to implement the ANCS specification between two iOS devices, one as Peripheral one as Central, were unsuccessful. Apple responded to us after some time (hat tip to their evangelists) and told us this was impossible.

With the addition of the CBPeripheralManager class and CBPeripheralManagerDelegate protocol to the CoreBluetooth.framework embedded in the IOBluetooth.framework on OSX Mavericks (deep breath), we can now use the BLE radio on an OSX device to implement and advertise ANCS.

Thus, this snippet belongs to a CBPeripheralManager on OSX:

- (void) advertiseANCS
{
    NSLog(@"%s", __FUNCTION__);

    // define the ANCS Characteristics
    CBUUID *notificationSourceUUID = [CBUUID UUIDWithString:@"9FBF120D-6301-42D9-8C58-25E699A21DBD"];
    CBMutableCharacteristic *notificationSource = [[CBMutableCharacteristic alloc] initWithType:notificationSourceUUID properties:CBCharacteristicPropertyNotifyEncryptionRequired value:nil permissions:CBAttributePermissionsReadEncryptionRequired];
    CBUUID *controlPointUUID = [CBUUID UUIDWithString:@"69D1D8F3-45E1-49A8-9821-9BBDFDAAD9D9"];
    CBMutableCharacteristic *controlPoint = [[CBMutableCharacteristic alloc] initWithType:controlPointUUID properties:CBCharacteristicPropertyWrite value:nil permissions:CBAttributePermissionsWriteEncryptionRequired];
    CBUUID *dataSourceUUID = [CBUUID UUIDWithString:@"22EAC6E9-24D6-4BB5-BE44-B36ACE7C7BFB"];
    CBMutableCharacteristic *dataSource = [[CBMutableCharacteristic alloc] initWithType:dataSourceUUID properties:CBCharacteristicPropertyNotifyEncryptionRequired value:nil permissions:CBAttributePermissionsReadEncryptionRequired];

    // define the ANCS Service
    CBUUID *ANCSUUID = [CBUUID UUIDWithString:@"7905F431-B5CE-4E99-A40F-4B1E122D00D0"];
    CBMutableService *ANCS = [[CBMutableService alloc] initWithType:ANCSUUID primary:YES];
    ANCS.characteristics = @[notificationSource, controlPoint, dataSource];

    // define the Advertisement data
    NSMutableDictionary *advertisementData = [NSMutableDictionary dictionary];
    [advertisementData setValue:@"ANCS" forKey:CBAdvertisementDataLocalNameKey];
    [advertisementData setValue:@[ANCSUUID] forKey:CBAdvertisementDataServiceUUIDsKey];

    // publish the ANCS service
    [self.peripheralManager addService:ANCS];
    [self.peripheralManager startAdvertising:advertisementData];
}

Whereas this snippet belongs on a CBCentralManager on an iOS device:

- (void) discoverANCS
{
    NSLog(@"%s", __FUNCTION__);

    NSMutableArray *services = [NSMutableArray array];
    [services addObject:@"7905F431-B5CE-4E99-A40F-4B1E122D00D0"];

    NSMutableDictionary *options = [NSMutableDictionary dictionary];
    [options setValue:[NSNumber numberWithBool:NO] forKey:CBCentralManagerScanOptionAllowDuplicatesKey];

    [self.centralManager scanForPeripheralsWithServices:services options:options];
}

The iOS device can now see and connect to the OSX radio, which implements the ANCS specification as detailed in the Apple documentation.

<CBCentralManager: 0x14e23280> <CBPeripheral: 0x14d27b40 identifier = 7231B80F-874E-DB5F-2AF9-7F376911E2B7, Name = "ANCS", state = disconnected> {
    kCBAdvDataChannel = 39;
    kCBAdvDataIsConnectable = 1;
    kCBAdvDataLocalName = ANCS;
} -60

Happy hunting



回答2:

Well the reason is because you are setting the UUID for the advertisement Data Dictionary as a String and not as a CBUUID, also I think that key takes an array of CBUUIDs.

therefore this should make it work:

NSDictionary *advertisementData = @{
                                        CBAdvertisementDataServiceUUIDsKey:@[[CBUUID UUIDWithString:@"7905F431-B5CE-4E99-A40F-4B1E122D00D0"]],
                                        CBAdvertisementDataLocalNameKey:@"ANCS",
                                        };

EDIT: Oh yeah my bad! I forgot to mention that if you are trying to discover this ANCS service from another iOS Device you wont be able to see it, not under iOS 7. Somehow the OS is reserving that service to itself and wont show up on your didDiscoverServices callback even though you might be seeing it on your advertisement data. It will however, work if you have an external device, like a non-iOS device, or a pebble-like device. This is how you expose the ANCS functionality but the rest of the implementation is up to consumer of the service.



回答3:

According to this blog post:

http://blog.punchthrough.com/post/63658238857/the-apple-notification-center-service-or-wtf-is

You can advertise with 'service solicitation' to pair and access the ANCS without writing any code on the iPhone!

I have not tried it, but will soon.



回答4:

For anyone currently Googling a similar question: ANCS via CoreBluetooth appears to no longer work in iOS 9. Specifically,

This functionality was removed from OS X and iOS.

Neither platform can be used to consume the ANCS service anymore using CoreBluetooth.

Given by https://forums.developer.apple.com/thread/24336



回答5:

The ANCS is not advertised on iOS, I used a following way to achieve a long time connection with ANCS:

My peripheral device uses a dummy service, which is advertised. An iOS application is used to discover a device with this service and to create a connection. You can write your own application, or use one of free available (like LightBlue for example).

Once a connection is established, the peripheral device enumerates all services present on connected iOS device. Beside of others, there are those three mentioned in ANCS documentation.

If your register notifications for them, you will get ANCS data.

If you bond devices (iOS and peripheral), ANCS will automatically care for connection (re)establishment any time, if it found bonded device being advertised.



回答6:

I use ANCS without any code on the iPhone using "pebble-like" prototype hardware. Using the methods documented above on this question. The video is for a kind of joke show and meant as a joke as is the concept of "area-wide notifications" :-) and not at all technical. But might be informative somewhat.

http://www.youtube.com/watch?v=O-YWMl7IS-g

I don't make money from my youtube BTW

I have not been successful with iOS--iOS attempts.