我正在开发一个使用 CoreBluetooth 与外部 BLE 附件 (cc256x) 交互的 iOS 应用程序。我必须将 iOS 通知从 iOS 设备发送到 BLE 设备。根据 Apple 文档,我正在使用 ANCS(Apple 通知中心服务)。
我从 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 就会自动发送通知。
我试过这个链接中提到的
但无法在 BLE 外围设备中看到通知。
有什么遗漏吗?请帮助我,因为我无法找到解决方案。