iPhone을 백그라운드에서 iBeacon으로 실행
iOS 7 장치를 Bluetooth LE 주변 장치 (iBeacon)로 실행하고 백그라운드에서 광고 할 수 있습니까? 아래 코드를 사용하여 전경에서 광고 할 수 있었고 다른 iOS 기기에서도 볼 수 있었지만 홈 화면으로 돌아가 자마자 광고가 중지됩니다. plist에 블루투스 주변기기 백그라운드 모드를 추가했지만 기기가 백그라운드에서 블루투스를 사용하고 싶다는 메시지가 표시되지만 도움이되지 않는 것 같습니다. 내가 잘못하고 있거나 iOS 7에서 이것이 가능하지 않습니까?
peripManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil];
- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral
{
if (peripheral.state != CBPeripheralManagerStatePoweredOn) {
return;
}
NSString *identifier = @"MyBeacon";
//Construct the region
CLBeaconRegion *beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid identifier:identifier];
//Passing nil will use the device default power
NSDictionary *payload = [beaconRegion peripheralDataWithMeasuredPower:nil];
//Start advertising
[peripManager startAdvertising:payload];
}
수신 / 수신 끝에있는 코드는 다음과 같습니다.
- (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons
inRegion:(CLBeaconRegion *)region
{
//Check if we have moved closer or farther away from the iBeacon…
if (beacons.count > 0) {
CLBeacon *beacon = [beacons objectAtIndex:0];
switch (beacon.proximity) {
case CLProximityImmediate:
[self log:[NSString stringWithFormat:@"You're Sitting on it! %li", (long)beacon.rssi]];
break;
case CLProximityNear:
[self log:[NSString stringWithFormat:@"Getting Warmer! %li", (long)beacon.rssi]];
break;
default:
[self log:[NSString stringWithFormat:@"It's around here somewhere! %li", (long)beacon.rssi]];
break;
}
}
}
표준 CoreBluetooth 광고는 앱이 백그라운드에있는 동안 방송 할 수 있지만 CLBeaconRegion
사전 으로 시작한 경우에는 방송 할 수 없습니다 . 해결 방법은 CoreLocation 프레임 워크를 모두 버리고 CoreBlueTooth 만 사용하여 자신 만의 근접 "프레임 워크"를 만드는 것입니다.
Info.plist 파일에서 적절한 배경 지정자를 사용해야합니다 (예 : bluetooth-peripheral
및 bluetooth-central
).
코드는 다음과 같습니다.
1) 다음을 사용하여 표준 주변 광고를 만듭니다. CBPeripheralManager
NSDictionary *advertisingData = @{CBAdvertisementDataLocalNameKey:@"my-peripheral",
CBAdvertisementDataServiceUUIDsKey:@[[CBUUID UUIDWithString:identifier]]};
// Start advertising over BLE
[peripheralManager startAdvertising:advertisingData];
2) use를 사용 CBCentralManager
하여 지정한 UUID를 사용하여 해당 서비스를 검색합니다.
NSDictionary *scanOptions = @{CBCentralManagerScanOptionAllowDuplicatesKey:@(YES)};
NSArray *services = @[[CBUUID UUIDWithString:identifier]];
[centralManager scanForPeripheralsWithServices:services options:scanOptions];
3) CBCentralManagerDelegate
방법 에서 광고 didDiscoverPeripheral
의 RSSI
값을 읽습니다 .
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral
advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
NSLog(@"RSSI: %d", [RSSI intValue]);
}
4) RSSI 값을 거리로 변환합니다.
- (INDetectorRange)convertRSSItoINProximity:(NSInteger)proximity
{
if (proximity < -70)
return INDetectorRangeFar;
if (proximity < -55)
return INDetectorRangeNear;
if (proximity < 0)
return INDetectorRangeImmediate;
return INDetectorRangeUnknown;
}
I found that I needed to "ease" or "average" the RSSI values to get anything workable. This is no different than when you are working with any sensor data (e.g. accelerometer data).
I have this concept fully working hope to publish it somewhere at some point.
Also, use the docs (Core Bluetooth Programming Guide) if you get stuck.
Update: A full code sample is up on Github. I worked on this as part of a work related project.
Update #2: Apple release major improvements to iBeacon background behavior for iOS7.1
The Can you smell the iBeacon? article discusses both the use of Estimotes and advertising from Macs and iOS devices. You need to check the capability “Acts as Bluetooth LE accessory” in the project target.
No, iOS devices only advertise iBeacon when the app that does the advertising runs in the foreground. so, if you switch to another app or if the device goes to sleep, the advertisement stops.
Of course, if you really want the advertisement to continue, disable the idle timer and do Guided Access so that the iOs device does not go to sleep and no one can switch to another app.
I am also hoping to be able to set up my (test) app to advertise an iBeacon from the background. The docs on the UIBackgroundModes info.plist key suggest that the bluetooth-peripheral key might work, but it seems that it doesn't. (I just tested it a few minutes ago.)
What I'm doing for now is setting the idle timer to disabled, as RawMean suggests, and then setting the screen brightness to 0. Finally, when my test app is acting as an iBeacon, I add a shake event handler that lights the screen up again for 30 seconds. Dimming the screen as low as it will go helps reduce battery drain somewhat.
참고URL : https://stackoverflow.com/questions/18944325/run-iphone-as-an-ibeacon-in-the-background
'programing tip' 카테고리의 다른 글
OpenGL이 라디안 대신 각도를 사용하는 이유는 무엇입니까? (0) | 2020.11.09 |
---|---|
종속성이있는 동적 라이브러리와 연결 (0) | 2020.11.09 |
Xcode 7 iOS 9 UITableViewCell Separator Inset 문제 (0) | 2020.11.09 |
실제 사용되는 "코드 액세스 보안"이 있습니까? (0) | 2020.11.09 |
PDO 사용 방법에 대한 좋은 튜토리얼이 있습니까? (0) | 2020.11.09 |