iOS 7 Core Bluetooth Peripheral running in background

前端 未结 2 1198
甜味超标
甜味超标 2021-01-31 21:49

What I want is for my iOS device to be advertising a Bluetooth LE service all the time, even when the app isn\'t running, so that I can have another iOS device scan for it and f

2条回答
  •  無奈伤痛
    2021-01-31 22:09

    The implementation can be founded here: https://developer.apple.com/library/content/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/PerformingCommonPeripheralRoleTasks/PerformingCommonPeripheralRoleTasks.html#//apple_ref/doc/uid/TP40013257-CH4-SW5

    Its very simply actually.

    
    
    @property (strong, nonatomic) CBPeripheralManager *peripheralManager;
    

    ...

    - (NSDictionary*) advertiseNotBeacon {
        CBUUID *myCustomServiceUUID = [CBUUID UUIDWithString:@"MY_UUID"];
    
        CBMutableCharacteristic *myCharacteristic = [[CBMutableCharacteristic alloc] initWithType:myCustomServiceUUID
                                                                                       properties:CBCharacteristicPropertyRead | CBCharacteristicPropertyNotify
                                                                                            value:nil permissions:CBAttributePermissionsReadable];
    
        CBMutableService *myService = [[CBMutableService alloc] initWithType:myCustomServiceUUID primary:YES];
        myService.characteristics = @[myCharacteristic];
    
        self.peripheralManager.delegate = self;
        [self.peripheralManager addService:myService];
    
        return @{CBAdvertisementDataServiceUUIDsKey : @[myService.UUID],
                 CBAdvertisementDataLocalNameKey: @"MY_NAME"
                 };
    }
    

    ...

    [self.peripheralManager startAdvertising:[self advertiseNotBeacon]];
    

    Doing this, you will start advertising a peripheral service.

    Now, if you want to keep the service running in background, read the documentation in this link: https://developer.apple.com/library/content/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/CoreBluetoothBackgroundProcessingForIOSApps/PerformingTasksWhileYourAppIsInTheBackground.html#//apple_ref/doc/uid/TP40013257-CH7-SW1

    Quick steps: Open info.plist Add a new par key/value

    Required background modes
          App shares data using CoreBluetooth
    

    Apple says to ctrl + click on any key/value and add a add the following, but exactly the same as explained before.

    UIBackgroundModes
          bluetooth-peripheral 
    

    Dont forgot about the limitations of running the service in background:

    • The CBCentralManagerScanOptionAllowDuplicatesKey scan option key is ignored, and multiple discoveries of an advertising peripheral are coalesced into a single discovery event. If all apps that are scanning for peripherals are in the background, the interval at which your central device scans for advertising packets increases. As a result, it may take longer to discover an advertising peripheral.
    • These changes help minimize radio usage and improve the battery life on your iOS device.

提交回复
热议问题