frostney / react-native-ibeacon

:satellite: iBeacon support for React Native
MIT License
375 stars 54 forks source link

Add requestStateForRegion / didDetermineState #15

Closed erikhuisman closed 1 year ago

erikhuisman commented 9 years ago

A method requestStateForRegion and event didDetermineState to request the initial/current state of a beacon region. I can do a PR some time soon

fkattan commented 8 years ago

I added this code to RNBeacon.m; So far it works fine.

RCT_EXPORT_METHOD(requestStateForRegion:(NSDictionary *)dict) {
    [self.locationManager requestStateForRegion:[self convertDictToBeaconRegion:dict]];
}

-(void)locationManager:(CLLocationManager *)manager
     didDetermineState:(CLRegionState)state
             forRegion:(nonnull CLRegion *)region {
    CLBeaconRegion *beaconRegion = (CLBeaconRegion *) region;
    NSDictionary *event = @{
                            @"region": region.identifier,
                            @"uuid": [beaconRegion.proximityUUID UUIDString],
                            };

    [self.bridge.eventDispatcher sendDeviceEventWithName:@"didDetermineStateForRegion" body:event];
}

You can then call this and listen to the result in your JS like this:

const region = {
    identifier: 'your identifier',
    uuid: 'your uuid'
};

iBeacon.requestStateForRegion(region);
this.determineStateForRegionSubscription = DeviceEventEmitter.addListener(
        'didDetermineStateForRegion',
        data => {
            // Do what you want with 'data' {region: 'your region identifier', uuid: 'your region uuid'}
        }
);