我在同一时间编写了一个广告和扫描应用程序。它运作良好,但我有一个问题。经过一段时间mz外围停止广告。
我试图重新启动,但我的手机和应用程序仍无法正常工作。
以下是外围设备实施:
@implementation PeripheralManager
+ (PeripheralManager*)sharedInstance
{
static PeripheralManager *_sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedInstance = [[PeripheralManager alloc] init];
});
return _sharedInstance;
}
-(id)initWithUUID:(NSString *)UUID{
self = [super init];
if(self) {
self.UUID = UUID;
self.peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil];
}
return self;
}
- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral {
if (peripheral.state != CBPeripheralManagerStatePoweredOn) {
return;
}
if (peripheral.state == CBPeripheralManagerStatePoweredOn) {
self.transferCharacteristic = [[CBMutableCharacteristic alloc] initWithType:[CBUUID UUIDWithString:TRANSFER_CHARACTERISTIC_UUID] properties:CBCharacteristicPropertyWrite value:nil permissions:CBAttributePermissionsWriteable];
CBMutableService *transferService = [[CBMutableService alloc] initWithType:[CBUUID UUIDWithString:self.UUID] primary:YES];
transferService.characteristics = @[_transferCharacteristic];
[_peripheralManager addService:transferService];
}
}
-(void)startAdvertisment{
[_peripheralManager startAdvertising:@{ CBAdvertisementDataServiceUUIDsKey : @[[CBUUID UUIDWithString:self.UUID]], CBAdvertisementDataLocalNameKey: PERIPHERAL_NAME}];
}