1

I want to create one application that show me paired devices in my app.(for example any device that paired me before detect and show me.) also in the next time I want to send one NSString like "hello" to paired device. I searching in google and I got so confused!!!

Please tell me first how to get paired device with my mobile and second how to send NSString value to them.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
user3599133
  • 155
  • 3
  • 17

1 Answers1

1

Here is an exemple for what you need :

You have to adapt it to match what u want. But you can see how it work ...

@implementation ViewController
{
   CBPeripheralManager *_peripheralManager;
   BOOL _isAdvertising;
}

- (void)_startAdvertising
{
   NSUUID *estimoteUUID = [[NSUUID alloc] initWithUUIDString:@"B9407F30-F5F8-466E-AFF9-25556B57FE6D"];

   CLBeaconRegion *region = [[CLBeaconRegion alloc] initWithProximityUUID:estimoteUUID
                                                                    major:2
                                                                    minor:1
                                                               identifier:@"SimEstimote"];
   NSDictionary *beaconPeripheralData = [region peripheralDataWithMeasuredPower:nil];

   [_peripheralManager startAdvertising:beaconPeripheralData];
}

- (void)_updateEmitterForDesiredState
{
   if (_peripheralManager.state == CBPeripheralManagerStatePoweredOn)
   {
      // only issue commands when powered on

      if (_isAdvertising)
      {
         if (!_peripheralManager.isAdvertising)
         {
            [self _startAdvertising];
         }
      }
      else
      {
         if (_peripheralManager.isAdvertising)
         {
            [_peripheralManager stopAdvertising];
         }
      }
   }
}

#pragma mark - CBPeripheralManagerDelegate

- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral
{
   [self _updateEmitterForDesiredState];
}

#pragma mark - Actions

- (IBAction)advertisingSwitch:(UISwitch *)sender
{
   _isAdvertising = sender.isOn;

   [self _updateEmitterForDesiredState];
}

@end

This for monitoring :

@implementation AppDelegate
{
   CLLocationManager *_locationManager;
   BOOL _isInsideRegion; // flag to prevent duplicate sending of notification
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)options
{
   // create a location manager
   _locationManager = [[CLLocationManager alloc] init];

   // set delegate, not the angle brackets
   _locationManager.delegate = self;

   NSUUID *estimoteUUID = [[NSUUID alloc] initWithUUIDString:@"B9407F30-F5F8-466E-AFF9-25556B57FE6D"];
   CLBeaconRegion *region = [[CLBeaconRegion alloc] initWithProximityUUID:estimoteUUID 
                                                               identifier:@"Estimote Range"];

   // launch app when display is turned on and inside region
   region.notifyEntryStateOnDisplay = YES;

   if ([CLLocationManager isMonitoringAvailableForClass:[CLBeaconRegion class]])
   {
      [_locationManager startMonitoringForRegion:region];

      // get status update right away for UI
      [_locationManager requestStateForRegion:region];
   }
   else
   {
      NSLog(@"This device does not support monitoring beacon regions");
   }

    // Override point for customization after application launch.
    return YES;
}

- (void)_sendEnterLocalNotification
{
   if (!_isInsideRegion)
   {
      UILocalNotification *notice = [[UILocalNotification alloc] init];

      notice.alertBody = @"Inside Estimote beacon region!";
      notice.alertAction = @"Open";

      [[UIApplication sharedApplication] scheduleLocalNotification:notice];
   }

   _isInsideRegion = YES;
}

- (void)_sendExitLocalNotification
{
   if (_isInsideRegion)
   {
      UILocalNotification *notice = [[UILocalNotification alloc] init];

      notice.alertBody = @"Left Estimote beacon region!";
      notice.alertAction = @"Open";

      [[UIApplication sharedApplication] scheduleLocalNotification:notice];
   }

   _isInsideRegion = NO;
}

- (void)_updateUIForState:(CLRegionState)state
{
   ViewController *vc = (ViewController *)self.window.rootViewController;

   if (state == CLRegionStateInside)
   {
      vc.label.text = @"Inside";
   }
   else if (state == CLRegionStateOutside)
   {
      vc.label.text = @"Outside";
   }
   else
   {
      vc.label.text = @"Unknown";
   }
}

#pragma mark - CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager
      didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
{
   // always update UI
   [self _updateUIForState:state];

   if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive)
   {
      // don't send any notifications
      return;
   }

   if (state == CLRegionStateInside)
   {
      [self _sendEnterLocalNotification];
   }
   else
   {
      [self _sendExitLocalNotification];
   }
}

@end

You can have the complete description at this adress :

  http://www.cocoanetics.com/2013/11/can-you-smell-the-ibeacon/
mad_mask
  • 776
  • 2
  • 10
  • 31
  • 1
    You can define a region with uuid, minor and major. These values need to match with the value inside of your beacon. – mad_mask May 25 '14 at 14:08
  • 1
    and yes uuid is unique value. UUIDs are handled in Core Foundation, by the CFUUID library. The function you are looking for is CFUUIDCreate. You can also use the app Beecon to get the value of your beacons easily. – mad_mask May 25 '14 at 14:12
  • 1
    the [self.locationManager startRangingBeaconsInRegion:self.beaconRegion] method returns you an array of beacons with their own values – mad_mask May 25 '14 at 14:19
  • my friend how I get UUID? – user3599133 May 25 '14 at 14:20
  • once the `[self.locationManager startRangingBeaconsInRegion:self.beaconRegion]` returns you the sorted array by proximity, you can do something like this : `self.selectedBeacon = [beacons objectAtIndex:0]; currentBeaconUuid = self.selectedBeacon.uuid;` – mad_mask May 25 '14 at 14:26
  • 2
    -1 **This has nothing to do with the question asked**. CLBeaconRegion is for BLE-based ibeacons which do not get "paired" while the question specifically asks about bluetooth devices which are paired. – Chris Stratton May 27 '14 at 13:59