带有外部BLE设备的iOS核心蓝牙ANCS

时间:2015-11-04 17:45:03

标签: ios objective-c core-bluetooth ancs

我正在使用一个iOS应用程序,它使用CoreBluetooth与外部BLE附件(cc256x)进行交互。我必须从iOS设备向BLE设备发送iOS通知。根据Apple文档,我使用的是ANCS(Apple Notification Center Service)。

https://developer.apple.com/library/ios/documentation/CoreBluetooth/Reference/AppleNotificationCenterServiceSpecification/Introduction/Introduction.html

我在iOS App上执行的步骤是:

iOS设备充当外围设备并广告ANCS,如下所示。

-(void)addService
{
    CBUUID *notificationSourceUUID = [CBUUID UUIDWithString:@"9FBF120D-6301-42D9-8C58-25E699A21DBD"];

    CBMutableCharacteristic *notificationSourceCharacteristic = [[CBMutableCharacteristic alloc] initWithType:notificationSourceUUID properties:CBCharacteristicPropertyNotify value:nil permissions:CBAttributePermissionsReadable];

    CBUUID *controlPointUUID = [CBUUID UUIDWithString:@"69D1D8F3-45E1-49A8-9821-9BBDFDAAD9D9"];

    CBMutableCharacteristic *controlPoint = [[CBMutableCharacteristic alloc] initWithType:controlPointUUID properties:CBCharacteristicPropertyWrite value:nil permissions:CBAttributePermissionsWriteable];

    CBUUID *dataSourceUUID = [CBUUID UUIDWithString:@"22EAC6E9-24D6-4BB5-BE44-B36ACE7C7BFB"];

    CBMutableCharacteristic *dataSource = [[CBMutableCharacteristic alloc] initWithType:dataSourceUUID properties:CBCharacteristicPropertyNotify value:nil permissions:CBAttributePermissionsReadable];

    CBUUID *newAlertCategoryUUID = [CBUUID UUIDWithString:@"0x2A47"];

    CBMutableCharacteristic *newAlertCategoryChar = [[CBMutableCharacteristic alloc] initWithType:newAlertCategoryUUID properties:CBCharacteristicPropertyRead value:nil permissions:CBAttributePermissionsReadable];

    CBUUID *supportedUnreadAlertCategoryUUID = [CBUUID UUIDWithString:@"0x2A48"];

    CBMutableCharacteristic *supportedUnreadAlertCategoryChar = [[CBMutableCharacteristic alloc] initWithType:supportedUnreadAlertCategoryUUID properties:CBCharacteristicPropertyRead value:nil permissions:CBAttributePermissionsReadable];

    CBUUID *newAlertUUID = [CBUUID UUIDWithString:@"0x2A46"];

    newAlertChar = [[CBMutableCharacteristic alloc] initWithType:newAlertUUID properties:CBCharacteristicPropertyNotify value:nil permissions:CBAttributePermissionsReadable];

    CBUUID *unreadAlertUUID = [CBUUID UUIDWithString:@"0x2A45"];

    unreadAlertChar = [[CBMutableCharacteristic alloc] initWithType:unreadAlertUUID properties:CBCharacteristicPropertyNotify value:nil permissions:CBAttributePermissionsReadable];

    CBUUID *alertNotificationControlPointUUID    = [CBUUID UUIDWithString:@"0x2A44"];

    self.alertNotificationControlPointChar = [[CBMutableCharacteristic alloc] initWithType:alertNotificationControlPointUUID properties:CBCharacteristicPropertyWrite value:nil permissions:CBAttributePermissionsWriteable];

    ANCSUUID = [CBUUID UUIDWithString:@"0x1811"];

    CBMutableService *ANCS = [[CBMutableService alloc] initWithType:ANCSUUID primary:YES];

    ANCS.characteristics = @[newAlertCategoryChar,supportedUnreadAlertCategoryChar,newAlertChar,unreadAlertChar,self.alertNotificationControlPointChar,notificationSourceCharacteristic,controlPoint,dataSource];

    [self.peripheralManager addService:ANCS];
}


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

    [self addService];

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

    [self.peripheralManager startAdvertising:advertisementData];
}

服务是广告,外部基于cc256x的设备作为中心,能够扫描并连接到外围设备。

当cc256x设备尝试订阅新警报/未读警报的通知时,他们会收到以下错误:

  

错误 - ANS_Decode_Un_Read_Alert_Status_Notification返回-1004。

该错误表明远程应用发送了错误/已损坏的“未读警报状态通知行为”通知。

据我对iOS应用程序的理解,我们只需宣传ANCS,一旦BLE设备订阅了ANCS特性,iOS就会自动发送通知。

我尝试过这个链接中提到的

iOS 7 ANCS: Discovering the primary ANCS Service UUID

但无法在BLE外设中看到通知。

有什么遗失的吗?请帮助我,因为我无法找到解决方案。

0 个答案:

没有答案