Objective-C에서 startMonitoringForRegion을 사용하여 여러 지역을 스캔하는 방법 (How to scan multiple regions using startMonitoringForRegion in Objective-C)


문제 설명

Objective‑C에서 startMonitoringForRegion을 사용하여 여러 지역을 스캔하는 방법 (How to scan multiple regions using startMonitoringForRegion in Objective‑C)

두 개의 튜토리얼을 통해 기본 C에 대해 읽고 있습니다. 지난 주 정도 수행을 통해 가장 잘 배우고 몇 가지 가벼운 앱을 작성했습니다. ibeacon을 사용할 몇 가지 앱을 작성하는 데 속도를 내고 있습니다. 몇 가지 샘플을 살펴보고 참조 가이드를 읽으면서 각 UUID에 대해 startMonitoringForRegion을 실행하여 여러 영역을 스캔할 수 있음을 알 수 있습니다. 좋아, 그래서 각 UUID에 대해 실행할 수 있다고 생각하지만 작동하지 않습니다. 기본적으로 완전히 잘못된 작업을 수행하고 있다고 확신합니다. 아래 코드는 완전히 해킹된 것입니다. 일단 의미 체계를 파악하면 API 호출로 DB에서 UUID를 가져온 다음 루프를 통해 모니터링을 활성화합니다. 아래 코드는 4개의 UUID 중 2개만 표시하는 마지막 루프를 생성합니다.

헤더:

@property (strong, nonatomic) CLBeaconRegion *myBeaconRegion;
@property (strong, nonatomic) CLBeaconRegion *myBeaconRegion2;
@property (strong, nonatomic) CLBeaconRegion *myBeaconRegion3;
@property (strong, nonatomic) CLBeaconRegion *myBeaconRegion4;

메인:

NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"86E4BDEA‑C6FF‑442C‑95CB‑E6E557A23CF2"];
self.myBeaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid identifier:@"com.appcoda.testregion"];

NSUUID *uuid2 = [[NSUUID alloc] initWithUUIDString:@"C9AFF296‑A722‑4F2D‑8669‑47B7CCC79A14"];
self.myBeaconRegion2 = [[CLBeaconRegion alloc] initWithProximityUUID:uuid2 identifier:@"com.appcoda.testregion"];

NSUUID *uuid3 = [[NSUUID alloc] initWithUUIDString:@"1DBDDC7C‑49BB‑48BF‑A2F6‑A4825BD514EA"];
self.myBeaconRegion3 = [[CLBeaconRegion alloc] initWithProximityUUID:uuid3 identifier:@"com.appcoda.testregion"];

NSUUID *uuid4 = [[NSUUID alloc] initWithUUIDString:@"8D942B9E‑0197‑4C81‑8722‑92144599E9F7"];
self.myBeaconRegion4 = [[CLBeaconRegion alloc] initWithProximityUUID:uuid4 identifier:@"com.appcoda.testregion"];

[self.locationManager startMonitoringForRegion:self.myBeaconRegion];
[self.locationManager startMonitoringForRegion:self.myBeaconRegion2];
[self.locationManager startMonitoringForRegion:self.myBeaconRegion3];
[self.locationManager startMonitoringForRegion:self.myBeaconRegion4];

NSSet *setOfRegions = [self.locationManager monitoredRegions];
    for (CLRegion *region in setOfRegions) {
        NSLog (@"region info: %@", region);
    }

참조 솔루션

방법 1:

I think the problem is your region identifiers. Each beacon region identifier must be unique, otherwise CLLocationManager treats them as the same region.

Try setting a unique identifier for each region:

NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"86E4BDEA‑C6FF‑442C‑95CB‑E6E557A23CF2"];
self.myBeaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid identifier:@"com.appcoda.testregion"];

NSUUID *uuid2 = [[NSUUID alloc] initWithUUIDString:@"C9AFF296‑A722‑4F2D‑8669‑47B7CCC79A14"];
self.myBeaconRegion2 = [[CLBeaconRegion alloc] initWithProximityUUID:uuid2 identifier:@"com.appcoda.testregion2"];

NSUUID *uuid3 = [[NSUUID alloc] initWithUUIDString:@"1DBDDC7C‑49BB‑48BF‑A2F6‑A4825BD514EA"];
self.myBeaconRegion3 = [[CLBeaconRegion alloc] initWithProximityUUID:uuid3 identifier:@"com.appcoda.testregion3"];

NSUUID *uuid4 = [[NSUUID alloc] initWithUUIDString:@"8D942B9E‑0197‑4C81‑8722‑92144599E9F7"];
self.myBeaconRegion4 = [[CLBeaconRegion alloc] initWithProximityUUID:uuid4 identifier:@"com.appcoda.testregion4"];

You should see each region listed from your NSLog statement. No need for dispatch_async, either.

방법 2:

The header file for startMonitoringForRegion states "This is done asynchronously and may not be immediately reflected in monitoredRegions".

You can verify this by adding a time delay to your for loop:

double delayInSeconds = 5.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    NSSet *setOfRegions = [self.locationManager monitoredRegions];
    for (CLRegion *region in setOfRegions) {
        NSLog (@"region info: %@", region);
    }
});

방법 3:

If you need to use multiple beacons to be monitored then you can able to differentiate using the beacon Major and Minor values. Read the tutorial here to understand about the ibeacons better.

‑(void)setBeaconTranmitter:(NSInteger)major minorValue:(NSInteger)minor {
    // We need to set beacon regions here.
    NSUUID * uid = [[NSUUID alloc] initWithUUIDString:uuid]; //uuid value is static common string for all beacons.
    self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uid major:major minor:minor identifier:beaconsId];//beaconsId is a common identifier for all beacons.

// Call your Transmitter function here
    [self configureTransmitter];
}

Above way I have configured three beacons regions with major and minor value difference. I placed three buttons and called IBAction to send different Major and Minor values using tags and calling function. I installed the same app in three different iphones and enabled each different button in each phones and installed a receiver in another phone for demo. Worked like charm! but it takes time to detect region while moving from one region to another.

(by user3196820James Frostkenshin03Vijay)

참조 문서

  1. How to scan multiple regions using startMonitoringForRegion in Objective‑C (CC BY‑SA 2.5/3.0/4.0)

#bluetooth-lowenergy #regions #ibeacon #iOS






관련 질문

블루투스 주변기기가 응답하지 않음 (Bluetooth peripheral doesn't answer)

iPhone 6은 BLE 4.2를 지원하지만 예는 없습니다. (iPhone 6 support BLE 4.2, but no example)

Android에서 백그라운드 BLE 스캔을 수행하는 방법은 무엇입니까? (How to perform background BLE scanning in android?)

iOS의 BLE에서 AVRCP가 가능합니까? (Is it possible AVRCP on BLE with iOS?)

Android 애플리케이션의 BLE 장치에서 데이터 패킷을 수신하는 방법 (How to Recieve Data packets from a BLE device in android application)

IOS : 백그라운드 BLE(Bluetooth Low Energy) 검색 규칙 (IOS : Background Bluetooth Low Energy (BLE) scanning rules)

Android BLE GATT Server의 사용자 정의 특성에 READ, NOTIFY 속성을 추가하는 방법은 무엇입니까? (How to add READ, NOTIFY property to a custom characteristic in Android BLE GATT Server?)

BluetoothGatt 개체가 사용 중입니다. (BluetoothGatt Object is Busy)

BLE로 연결된 iOS에서 Android 웨어러블 기기를 사용할 수 있나요? 사용 가능한 SDK가 있습니까? (Can an Android wearable device used with iOS, connected by BLE ? Is there any available SDK?)

Bluetooth LE GATT에서 장기 키가 유효하지 않을 때 감지할 수 있는 방법이 있습니까? (In Bluetooth LE GATT, is there any way to detect when Long Term Keys are invalid?)

BLE를 통해 BlueTooth를 통해 특성에 작성된 명령의 일부를 Android에서 손실할 수 있습니까? (Is it possible to android to lost part of a command writen to a characteristic via BlueTooth via BLE?)

Linux에서 Python 패키지를 다운로드하려고 할 때 오류가 발생함 (Getting an error when trying to download a Python package on Linux)







코멘트