iBeacon using two iOS devices - one startAdvertising and one startMonitoringForRegion - ios

Update: Found this Then I am able from an Android device to spot the iOS beacon, but no App on the app store(searched for locate beacon) can find the beacon. Is iOS 9 having issues?
I am trying to use one device for advertising and one for monitoring.
I was expecting "didEnterRegion" to be called, but nothing - do you know why?
Project_1: Code for the advertising:
ViewController.h fil
#import <UIKit/UIKit.h>
#import CoreBluetooth;
#interface ViewController : UIViewController <CBPeripheralManagerDelegate>
#property (strong, nonatomic) CBPeripheralManager *peripheralManager;
#end
ViewController.m file
//
// ViewController.m
// MyBeacon
//
//
// 17BEFCE4-43B9-4921-BDAA-8361A24727A9
#import "ViewController.h"
// #import <CoreBluetooth/CoreBluetooth.h>
#import CoreLocation;
#import CoreBluetooth;
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSUUID *proximityUUID = [[NSUUID alloc]
initWithUUIDString:#"39ED98FF-2900-441A-802F-9C398FC199D2"];
// Create the beacon region.
CLBeaconRegion *beaconRegion = [[CLBeaconRegion alloc]
initWithProximityUUID:proximityUUID
identifier:#"com.mycompany.myregion"];
// Create a dictionary of advertisement data.
NSDictionary *beaconPeripheralData =
[beaconRegion peripheralDataWithMeasuredPower:nil];
// Create the peripheral manager.
CBPeripheralManager *peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil];
self.peripheralManager = peripheralManager;
// Start advertising your beacon's data.
[self.peripheralManager startAdvertising:beaconPeripheralData];
}
- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral{
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Project_2: Code for monitoring:
ViewController.h file
//
// ViewController.h
// UseMyBeacon
//
//
#import <UIKit/UIKit.h>
#import CoreLocation;
#interface ViewController : UIViewController <CLLocationManagerDelegate>
#property (strong, nonatomic) CLLocationManager *locationManager;
#property (strong, nonatomic) CLBeaconRegion *region;
#end
ViewController.m
//
// ViewController.m
// UseMyBeacon
//
//
#import "ViewController.h"
#import CoreLocation;
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
NSUUID *proximityUUID = [[NSUUID alloc]initWithUUIDString:#"39ED98FF-2900-441A-802F-9C398FC199D2"];
CLBeaconRegion *thisRegion = [[CLBeaconRegion alloc] initWithProximityUUID:proximityUUID identifier:#"com.mycompany.myregion"];
self.region = thisRegion;
Boolean b = [CLLocationManager isMonitoringAvailableForClass:[thisRegion class]];
if (b) {
// Create the beacon region to be monitored.
// CLBeaconRegion *beaconRegion = [[CLBeaconRegion alloc]
// initWithProximityUUID:proximityUUID
// identifier:identifier];
// Register the beacon region with the location manager.
self.locationManager.delegate = self;
[self.locationManager startMonitoringForRegion:thisRegion];
}
}
- (void)locationManager:(CLLocationManager *)manager
didEnterRegion:(CLRegion *)region{
}
- (void) locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region
{
[self.locationManager requestStateForRegion:self.region];
}
- (void)locationManager:(CLLocationManager *)manager
rangingBeaconsDidFailForRegion:(CLBeaconRegion *)region
withError:(NSError *)error{
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error{
}
- (void) locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
{
switch (state) {
case CLRegionStateInside:
[self.locationManager startRangingBeaconsInRegion:self.region];
break;
case CLRegionStateOutside:
case CLRegionStateUnknown:
default:
// stop ranging beacons, etc
NSLog(#"Region unknown");
}
}
- (void) locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region
{
if ([beacons count] > 0) {
// Handle your found beacons here
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end

Related

Objective-C issues with location services

I am trying to make a simple app the will get the users longitude and latitude
I followed the tutorial here:
http://www.appcoda.com/how-to-get-current-location-iphone-user/
and came with this:
.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#interface ViewController : UIViewController <CLLocationManagerDelegate>
#property (strong, nonatomic) IBOutlet UILabel *longitudeLabel;
#property (strong, nonatomic) IBOutlet UILabel *latitudeLabel;
- (IBAction)getCurrentLocation:(id)sender;
#end
.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
{
CLLocationManager *locationManager;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
locationManager = [[CLLocationManager alloc] init];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)getCurrentLocation:(id)sender {
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(#"didFailWithError: %#", error);
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(#"didUpdateToLocation: %#", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
_longitudeLabel.text = [NSString stringWithFormat:#"%.8f", currentLocation.coordinate.longitude];
_latitudeLabel.text = [NSString stringWithFormat:#"%.8f", currentLocation.coordinate.latitude];
}
}
#end
My issue is that "Would like to use your current location" popup.
and none of the delegate methods are being hit, at all. I put a break point at the beginning of each delegate method and nothing. Please help.
Please ensure you have following keys in your info.plist:
NSLocationAlwaysUsageDescription with value I need Location
NSLocationWhenInUseUsageDescription with value I need Location
privacy - location usage description with value I need Location
Ensure the info.plist details as answered earlier and then add the below code during your initialization;
if (IS_OS_8_OR_LATER) {
[locationmanager requestWhenInUseAuthorization];
//or (as per your app requirement)
[locationmanager requestAlwaysAuthorization];
}

CLLocationManager not calling didUpdateLocations

I've been unable to figure out why location manager isn't providing updates to me. I've added the key, tested on simulator and on a device.
Edit: STDOUT also contains "Delegate: "
STDOUT
2014-11-28 22:18:30.066 Speedo[14091:150298] Status: 4
2014-11-28 22:18:30.069 Speedo[14091:150298] Is when in use? 1
2014-11-28 22:18:30.158 Speedo[14091:150298] New status: 4
2014-11-28 22:18:30.159 Speedo[14091:150298] Is when in use? 1
FirstViewController.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#interface FirstViewController : UIViewController <CLLocationManagerDelegate>
#property (weak, nonatomic) IBOutlet UILabel* speedLabel;
#end
FirstViewController.cpp
#import "FirstViewController.h"
#import <CoreLocation/CoreLocation.h>
#interface FirstViewController ()
#end
#implementation FirstViewController {
CLLocationManager *locationManager;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
self.speedLabel.text = #"";
if([locationManager respondsToSelector: #selector(requestWhenInUseAuthorization)]) {
[locationManager requestWhenInUseAuthorization];
}
// locationManager.desiredAccuracy = kCLLocationAccuracyBest;
// locationManager.distanceFilter = 0.01;
[locationManager startUpdatingLocation];
NSLog(#"Delegate: %#", locationManager.delegate);
NSLog(#"Status: %d", [CLLocationManager authorizationStatus]);
NSLog(#"Is when in use? %d", [CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(#"didFailWithError: %#", error);
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle:#"Error" message:#"Failed to Get Your Location" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[errorAlert show];
}
-(void) locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
NSLog(#"New status: %d", status);
NSLog(#"Is when in use? %d", [CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse);
[manager startUpdatingLocation];
}
-(void)locationManage: (CLLocationManager *)manager didUpdateLocations:(NSArray*)locations {
CLLocation* last = [locations lastObject];
NSLog(#"Speed: %f m/s", last.speed); // meter per second
// TODO: support mutli-units
// m/s -> mph
// 1 -> 2.236
self.speedLabel.text = [NSString stringWithFormat:#"%d", (int)(last.speed * 2.23694)];
}
#end
Perhaps it's just the result of a typo.
-(void)locationManage: (CLLocationManager *)manager didUpdateLocations:(NSArray*)locations {
should be
-(void)locationManager: (CLLocationManager *)manager didUpdateLocations:(NSArray*)locations {
You forgot the "r" at the end of "locationManager".

UIlabel doesn't update user current speed

been having this problem for a few days now and can't seem to find a solution for it. It's probably some very basic stuff but still can't come up with a solution. So I'm trying to have my uilabel update a user speed in a car or on bike. But When the UIlabel update, it doesn't update the correct value. Any help will be appreciated.
Heres my .h file
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#interface MainViewController : UIViewController <LoginDelegate,WEPopoverParentView,PopoverControllerDelegate,MainMenuDelegate,MKMapViewDelegate,UIActionSheetDelegate,UIAccelerometerDelegate, CLLocationManagerDelegate, NSObject>
{
AppDelegate *appDelegate;
IBOutlet MKMapView *userMap;
CLLocationManager *locationManager;
}
#property (strong, nonatomic) IBOutlet UILabel *speedView;
#property(nonatomic) int speedCount;
#property (nonatomic,retain) CLLocationManager *locationManager;
#property (nonatomic, strong) WEPopoverController *popoverController;
+ (NSString *) speedToMPH: (float) value;
- (IBAction)btnMenuTapped:(id)sender;
#end
and my .h file
#implementation MainViewController
#synthesize speedCount;
#synthesize speedView;
#synthesize popoverController;
#synthesize locationManager;
- (void)locationError:(NSError *)error {
speedView.text = [error description];
}
-(void)viewWillAppear:(BOOL)animated
{
[userMap setRegion:MKCoordinateRegionMakeWithDistance(userMap.userLocation.coordinate, 5, 5) animated:YES];
}
- (void)viewDidLoad
{
[super viewDidLoad];
appDelegate = [[UIApplication sharedApplication] delegate];
locationManager =[[CLLocationManager alloc] init];
// Do any additional setup after loading the view, typically from a nib.
[userMap setCenterCoordinate: userMap.userLocation.coordinate animated: YES];
[self performSelector:#selector(checkForLogin) withObject:nil afterDelay:1];
[self startLocationServices];
// create LM
self.locationManager = [CLLocationManager new];
// set its delegate
[self.locationManager setDelegate:self];
// set configuration
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[self.locationManager setDistanceFilter:kCLDistanceFilterNone];
// start location services
[self.locationManager startUpdatingLocation];
// create an oldLocation variable if one doesn't exist
}
- (void)startLocationServices
{
// create the Location Manager
if (self.locationManager == nil) {
self.locationManager = [CLLocationManager new];
}
// stop services
[self.locationManager stopUpdatingLocation];
[self.locationManager setDelegate:nil];
self.speedView.text = #"Location Services stopped.";
}
// locationManager didUpdateToLocation FromLocation
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
NSLog(#"%#", newLocation);
self.speedView.text = [NSString stringWithFormat:#"%d %.f ", speedCount, [newLocation speed]];
}
+ (NSString *) speedToMPH: (float) value
{
NSString *speedCount = #"0.0 ";
if (value>0) {
float mile = 1609.344f;
float mph = value / mile * 3600;
speedCount = [NSString stringWithFormat: #"%.f ", mph];
}
return speedCount;
}
CLLocation returns the speed in meters per second. So you will have to convert it to miles per hour by multiplying the value with 2.23694.
self.speedView.text = [NSString stringWithFormat:#"%d %.f ", speedCount, [newLocation speed] * 2.23694];

CLLocationManager not calling delegate in an NSObject

I'm trying to create a helper class to get the coordinates of the phone in any other class easily. I've followed a tutorial in which the UIViewController implemented the <CLLocationManagerDelegate> and it worked. I tried to do the same in a simple NSObject, but then my delegate was not called anymore.
This is the code I have :
PSCoordinates.h
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#interface PSCoordinates : NSObject <CLLocationManagerDelegate>
#property (nonatomic, retain) CLLocationManager* locationManager;
#end
PSCoordinates.m
#import "PSCoordinates.h"
#implementation PSCoordinates
- (id) init {
self = [super init];
if (self) {
self.locationManager = [[CLLocationManager alloc] init];
if ([CLLocationManager locationServicesEnabled])
{
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = 100.0f;
NSLog(#"PSCoordinates init");
[self.locationManager startUpdatingLocation];
}
}
return self;
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSLog(#"Géolocalisation : %#",[newLocation description]);
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
NSLog(#"Géolocalisation (erreur) : %#",[error description]);
}
#end
I'm calling it by calling
PSCoordinates * coordinates = [[PSCoordinates alloc] init];
when pressing a button. The init is working as I can see the NSLog PSCoordinates init.
I've found other topics of people having the same problem but none of the answer solved it.
Your help would be really appreciated.
Make "PSCoordinates * coordinates" as global in your class. It will work :)

corelocation keeps looping

I essentially started off with this CoreLocation iOS tutorial to get my head around how to implement CoreLocation into my app. (http://www.techotopia.com/index.php/An_Example_iOS_5_iPhone_Location_Application)
But what I have encountered when trying to incorporate this tutorial into my app is that it now just keeps on looping over and over, which is just baffling me. Can anyone please help?
GPSViewController.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#interface GPSViewController : UIViewController
#property (strong, nonatomic) CLLocationManager *locationManager;
#property (strong, nonatomic) CLLocation *startLocation;
#end
GPSViewController.m
#import "GPSViewController.h"
#import "DataClass.h"
#interface GPSViewController ()
#end
#implementation GPSViewController
#synthesize locationManager, startLocation;
DataClass *obj;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
//initialization og global varable.
DataClass *obj=[DataClass getInstance];
//GPS Initialise
self.locationManager = [[CLLocationManager alloc] init];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.delegate = self;
[locationManager startUpdatingLocation];
startLocation = nil;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark -
#pragma mark CLLocationManagerDelegate
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSString *currentLatitude = [[NSString alloc]
initWithFormat:#"%g",
newLocation.coordinate.latitude];
//latitude.text = currentLatitude;
obj.Latatude = currentLatitude;
NSString *currentLongitude = [[NSString alloc]
initWithFormat:#"%g",
newLocation.coordinate.longitude];
//longitude.text = currentLongitude;
obj.Longitude = currentLongitude;
NSLog(#"latitude %+.6f, longitude %+.6f\n",
newLocation.coordinate.latitude,
newLocation.coordinate.longitude);
if(obj.Latatude != NULL && obj.Longitude != NULL){
[self performSegueWithIdentifier:#"GPSSuccess" sender:self];
}
}
-(void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.startLocation = nil;
self.locationManager = nil;
}
#end
You should call [locationManager stopUpdatingLocation]; in order to stop it from getting the user's location over and over.

Resources