iBeacon receiver app not working some devices - ios

Thanks in advance
I have IBeacon Broadcaster and receiver sample apps for ios. But Ibeacon receiver not working in some devices.
Here is my code
enter code here- (void)viewDidLoad{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Initialize location manager and set ourselves as the delegate
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
// Create a NSUUID with the same UUID as the broadcasting beacon
NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:#"A77A1B68-49A7-4DBF-914C-760D07FBB87B"];
// Setup a new region with that UUID and same identifier as the broadcasting beacon
self.myBeaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid
identifier:#"com.appcoda.testregion"];
// Tell location manager to start monitoring for the beacon region
[self.locationManager startMonitoringForRegion:self.myBeaconRegion];
// Check if beacon monitoring is available for this device
if (![CLLocationManager isMonitoringAvailableForClass:[CLBeaconRegion class]]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Monitoring not available" message:nil delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles: nil]; [alert show]; return;
}}
- (void)locationManager:(CLLocationManager*)manager didEnterRegion:(CLRegion *)region{
// We entered a region, now start looking for our target beacons!
self.statusLabel.text = #"Finding beacons.";
[self.locationManager startRangingBeaconsInRegion:self.myBeaconRegion];}
-(void)locationManager:(CLLocationManager*)manager didExitRegion:(CLRegion *)region{
// Exited the region
self.statusLabel.text = #"None found.";
[self.locationManager stopRangingBeaconsInRegion:self.myBeaconRegion];}
-(void)locationManager:(CLLocationManager*)manager
didRangeBeacons:(NSArray*)beacons
inRegion:(CLBeaconRegion*)region{
// Beacon found!
self.statusLabel.text = #"Beacon found!";
CLBeacon *foundBeacon = [beacons firstObject];
// You can retrieve the beacon data from its properties
//NSString *uuid = foundBeacon.proximityUUID.UUIDString;
//NSString *major = [NSString stringWithFormat:#"%#", foundBeacon.major];
//NSString *minor = [NSString stringWithFormat:#"%#", foundBeacon.minor];}
But these codes are working in some devices

1) You try to monitor before verifying that monitoring is available.
2) I don't see any request for authorization:
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
3) For authorization to even work, you need to have a string in your plist:
NSLocationAlwaysUsageDescription
If you google around using "CLLocationManager requestAlwaysAuthorization NSLocationAlwaysUsageDescription" you will find numerous blogs with step-by-step instructions on how to get beacon tracking to work.

Related

CLLocation current location not working

I have this code in my class ViewController:
CLLocationManager *locationManager;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
locationManager = [[CLLocationManager alloc] init];
[locationManager requestWhenInUseAuthorization];
}
- (IBAction)getCurrentLocation:(id)sender {
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
#pragma mark - CLLocationManagerDelegate
-(void) locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
NSLog(#"Did finish with error - %#", error);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Failed to get your location" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSLog(#"did Update Location - %#", newLocation);
CLLocation *currentLocation = newLocation;
if(currentLocation != nil) {
_longitudeLabel.text = [NSString stringWithFormat:#"%.8f", currentLocation.coordinate.longitude];
_latitudeLabel.text = [NSString stringWithFormat:#"%.8f", currentLocation.coordinate.latitude];
}
}
But I am not getting the location or the popup for allowing access.
I am using the Core Location framework.
On button click I am printing latitude, longitude and address in labels.
I am testing this on the simulator.
Sometimes Simulator wont work with location enabled services use Apple Device for perfect testing.
Add the following keys inside your info.plist file to get allow access popup.
or add them as updating info.plist file source code.
<key>NSLocationAlwaysUsageDescription</key>
<string>message for location uses</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>message for location uses</string>
Try this code in viewDidLoad...
//---- For getting current gps location
locationManager = [CLLocationManager new];
locationManager.delegate = self;
if ([locationManager respondsToSelector:#selector(requestWhenInUseAuthorization)]) {
[locationManager requestWhenInUseAuthorization];
}
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
//------
You also have to add a string for the NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription keys to the app's Info.plist.
locationManager =[[CLLocationManager alloc] init];
[locationManager requestWhenInUseAuthorization];
locationManager.delegate=self;
locationManager.desiredAccuracy=kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
you can add in your plist:
<key>NSLocationAlwaysUsageDescription</key>
<key>NSLocationWhenInUseUsageDescription</key>

How to scan Beacons in IOS?

I am developing an app to search nearest beacon device but not able to calling any delegate, Already I have beacons available but not finding any beacons.
Also I did not understand NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:#"3863C90B-1BA6-4A4E-ADD2-D64FF1286898"]; is static is passed or not?
NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:#"3863C90B-1BA6-4A4E-ADD2-D64FF1286898"];
self.myBeaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid
identifier:#"com.jss.myidentifire"];
self.myBeaconRegion.notifyOnEntry = YES;
[self.locationManager startMonitoringForRegion:self.myBeaconRegion];
[self.locationManager startUpdatingHeading];
[self.locationManager startRangingBeaconsInRegion:self.myBeaconRegion];
-(void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region
{
[locationManager requestStateForRegion:region];
}
-(void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
{
if (state == CLRegionStateInside)
{
[self locationManager:locationManager didEnterRegion:region];
}
}
- (void)locationManager:(CLLocationManager*)manager didEnterRegion:(CLRegion *)region
{
// We entered a region, now start looking for our target beacons!
self.statusLabel.text = #"Finding beacons.";
[self.locationManager startRangingBeaconsInRegion:self.myBeaconRegion];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"BeaconDetail" message:#"DidEnterRegion Called" delegate:self cancelButtonTitle:#"ok" otherButtonTitles:nil, nil];
[alert show];
}
-(void)locationManager:(CLLocationManager*)manager didExitRegion:(CLRegion *)region
{
// Exited the region
self.statusLabel.text = #"None found.";
[self.locationManager stopRangingBeaconsInRegion:self.myBeaconRegion];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"BeaconDetail" message:#"didExitRegion Called" delegate:self cancelButtonTitle:#"ok" otherButtonTitles:nil, nil];
[alert show];
}
-(void)locationManager:(CLLocationManager*)manager
didRangeBeacons:(NSArray*)beacons
inRegion:(CLBeaconRegion*)region
{
self.statusLabel.text = #"Beacon found!";
CLBeacon *foundBeacon = [beacons firstObject];
NSString *uuid = foundBeacon.proximityUUID.UUIDString;
NSString *major = [NSString stringWithFormat:#"%#", foundBeacon.major];
NSString *minor = [NSString stringWithFormat:#"%#", foundBeacon.minor];
NSString *accuracy = [NSString stringWithFormat:#"%f", foundBeacon.accuracy];
accuracy = [NSString stringWithFormat:#"%f", foundBeacon.accuracy];
if (foundBeacon.proximity == CLProximityUnknown) {
self.distanceLabel.text = #"Unknown Proximity";
} else if (foundBeacon.proximity == CLProximityImmediate) {
self.distanceLabel.text = #"Immediate";
} else if (foundBeacon.proximity == CLProximityNear) {
self.distanceLabel.text = #"Near";
} else if (foundBeacon.proximity == CLProximityFar) {
self.distanceLabel.text = #"Far";
}
NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
[dict setObject:uuid forKey:#"uuid"];
[dict setObject:major forKey:#"major"];
[dict setObject:minor forKey:#"minor"];
[dict setObject:accuracy forKey:#"accuracy"];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"BeaconDetail" message:[NSString stringWithFormat:#"%#",dict] delegate:self cancelButtonTitle:#"ok" otherButtonTitles:nil, nil];
[alert show];
}
Make sure you have obtained permission to scan for beacons by adding the following:
if([self.locationManager respondsToSelector:#selector(requestAlwaysAuthorization)]) {
[self.locationManager requestAlwaysAuthorization];
}
For the above to work, you also need to add a new key to your Info.plist file: NSLocationAlwaysUsageDescription with a value set to something like "This app wants to scan for beacons"
if you start the receiver when it's already in the beacon range, it's not going to fire. Just checking that you're walking far away from the beacon and then walking back into range to get it to fire?

Eddystone beacon detecting issue

Here is a code I use to detect Eddystone using iPhone iOS 9:
- (void)viewDidLoad
{
[super viewDidLoad];
if ([CLLocationManager locationServicesEnabled]) {
_locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.pausesLocationUpdatesAutomatically = NO;
[self.locationManager requestAlwaysAuthorization];
NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:#"f7826da6-4fa2-4e98-8024-bc5b71e0893e"];
NSString *bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];
CLBeaconRegion *beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid identifier:bundleIdentifier];
self.locationManager.allowsBackgroundLocationUpdates = YES;
[self.locationManager startMonitoringForRegion:beaconRegion];
[self.locationManager startRangingBeaconsInRegion:beaconRegion];
[self.locationManager startUpdatingLocation];
}
else {
NSLog(#"location service is disabled");
}
}
- (void)locationManager:(CLLocationManager *)manager
didRangeBeacons:(nonnull NSArray<CLBeacon *> *)beacons
inRegion:(nonnull CLBeaconRegion *)region
{
NSLog(#"beacons count: %lu", (unsigned long)[beacons count]); // beacons count always "0"
}
Also I added in plist NSLocationAlwaysUsageDescription field.
The problem is that it can't detect any Eddystone device with code above. But with third party apps it finds well.
What I'm doing wrong?
Your code is using Core Location, which only works with iBeacon. You won't be able to discover Eddystone beacons this way.
You need to use some Eddystone-compatible SDK. Since you seem to be using Kontakt.io's beacons, you might want to use their SDK:
http://developer.kontakt.io/ios-sdk/quickstart/#eddystone-support
Alternatively, you can use iOS's native Core Bluetooth to implement Eddystone scanning yourself. There's even an example on how to do that, available in the official Eddystone GitHub repo:
https://github.com/google/eddystone/tree/master/tools/ios-eddystone-scanner-sample

didRangeBeacons not getting uuid in ios

I used the following code to get message from a particular beacon but it always shows null in uuid. When i run this code the "viewdidLoad method works fine but when it comes to didRangebeacons it shows only the last uuid beacons. Actually i want to check that Is beacon available for particular uuid and receive that signals and keep on checking for uuids in array and when it receives another signal it should display that beacon signal also.
- (void)viewDidLoad
{
[super viewDidLoad];
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
NSLog(#"Before assigning UUID");
NSArray *uuidArr = [NSArray arrayWithObjects: #"9146947B-441D-4116-B60E-4DD4659825AB", #"9146947B-441D-4116-B60E-4DD46598257C", #"9146947B-441D-4116-B60E-4DD46598257B", nil];
for(int i=0; i<[uuidArr count]; i++)
{
NSString *uuidTemp = [uuidArr objectAtIndex:i];
NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:uuidTemp];
self.myBeaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid
identifier:#"com.gimbal.iosexample"];
[self.locationManager startMonitoringForRegion:self.myBeaconRegion];
[self.locationManager startRangingBeaconsInRegion:self.myBeaconRegion];
if (![CLLocationManager isMonitoringAvailableForClass:[CLBeaconRegion class]])
{
NSString *msg = [NSString stringWithFormat:#"Monitoring not available for %#", uuidTemp];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Monitoring not available" message:msg delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles: nil];
[alert show]; return;
}
}
}
-(void)locationManager:(CLLocationManager*)manager didRangeBeacons:(NSArray*)beacons inRegion:(CLBeaconRegion*)region
{
NSLog(#"iBeacons found");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Successfully found" message:nil delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles: nil];
[alert show];
self.statusLabel.text = #"iBeacon found!";
CLBeacon *foundBeacon = [beacons firstObject];
NSString *uuid = foundBeacon.proximityUUID.UUIDString;
NSString *major = [NSString stringWithFormat:#"%#", foundBeacon.major];
NSString *minor = [NSString stringWithFormat:#"%#", foundBeacon.minor];
NSLog(#"UUID: %#", uuid);
}
#end
Can anyone help me to do this.
Thanks in advance.
You are creating different beacon regions in your for loop, but your region identifier is the same, so the second region definition will replace the first, and then the second will be replaced by the third. You need to use a unique region identifier - something like
CLBeaconRegion *beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid
identifier:[NSString stringWithFormat:#"com.gimbal.iosexample.%d",i]];

can't get location on iOS - doesn't call delegate methods

i have a problem with the locationManager under iOS 6.
-(void) getLocationTest {
// Create a location manager instance to determine if location services are enabled. This manager instance will be
// immediately released afterwards.
manager = [[CLLocationManager alloc] init];
if (![manager locationServicesEnabled]) {
UIAlertView *servicesDisabledAlert = [[UIAlertView alloc] initWithTitle:#"Location Services Disabled" message:#"You currently have all location services for this device disabled. If you proceed, you will be asked to confirm whether location services should be reenabled." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[servicesDisabledAlert show];
//[servicesDisabledAlert release];
}
manager.delegate = self;
manager.desiredAccuracy = kCLLocationAccuracyKilometer;
// Set a movement threshold for new events.
manager.distanceFilter = 500;
[manager startUpdatingLocation];
//[manager release];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
double testDe = 2.434;
testDe = testDe * 2;
}
-(void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations {
CLLocation *location = [locations objectAtIndex:0];
double locDouble = location.coordinate.latitude;
NSString* locName = [[NSString alloc] initWithFormat:#"Hello, %f!", locDouble];
//[locations lastObject];
//NSLog(#"lat%f - lon%f", location.coordinate.latitude, location.coordinate.longitude);
}
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
double testD = 2.434;
testD = testD * 2;
}
i have breakpoints in all the delegate methods, but none of them is being called.
Does someone know what i'm doing wrong? or how i can find out where the error is?
i would be grateful for every answer!
thanks!
Are you testing this on a device or in the simulator? In the simulator you have to manually set some location from the simulator app menu. It's in Debug/Location/.

Resources