Integrating iBeacon and coreBluetooth peripheral

2019-08-12 21:21发布

问题:

I'd like to know if its possible to advertise services through coreBluetooth with a device acting as a peripheral and also range for iBeacons, also accomplish both in the background? Essentially the purpose being to range for beacons and then if a beacon is within .Near or .Immediate range update the characteristics of a service advertised by the device. I can happily accomplish the ranging task, and although I've found information on device acting as a peripheral slim I think i can manage that, however having been stopped repeatedly by background functionality limitations I wanted to know if this type of scenario is possible before i spend lots of time trying to accomplish something which it turns out everyone knows is not possible. Im new to Swift and iOS in general so it takes me time to get much done. Any advice would be greatly appreciated. Thanks

回答1:

This is possible, although there are restrictions in the background.

Foreground:

  • Exactly what you describe is possible. iOS allows simultaneous advertising of a Bluetooth LE service using CoreBluetooth while ranging for iBeacons using CoreLocation.

Background:

  • iOS limits background beacon scanning using CoreLocation's beacon ranging APIs to only five seconds after a beacon region is first detected. You can extend this five seconds of scanning to up to three minutes using a technique in a blog post I wrote here. Extending for longer requires adding a location background mode to your Info.plist, which may make it harder to get your app approved for the AppStore.

  • Advertising of Bluetooth LE Services in the background on iOS does not work like advertising in the foreground. It uses an Apple proprietary technique to collapse the service identifiers into a smaller advertising packet that is shared across all apps advertising Bluetooth LE services. These may not be easily discoverable by non-iOS Bluetooth LE devices:

The CBAdvertisementDataLocalNameKey advertisement key is ignored, and the local name of peripheral is not advertised. All service UUIDs contained in the value of the CBAdvertisementDataServiceUUIDsKey advertisement key are placed in a special “overflow” area; they can be discovered only by an iOS device that is explicitly scanning for them. If all apps that are advertising are in the background, the frequency at which your peripheral device sends advertising packets may decrease.

See the The bluetooth-peripheral Background Execution Mode section here for more info.