ios - 如何获得配对的蓝牙设备

标签 ios objective-c bluetooth

我想创建一个应用程序,在我的应用程序中向我显示已配对的设备。(例如,在检测并向我显示之前与我配对的任何设备。) 同样在下一次我想发送一个 NSString"hello" 到配对设备。 我在谷歌搜索,我很困惑!!!

请先告诉我如何将设备与我的手机配对,然后告诉我如何向它们发送 NSString 值。

最佳答案

这是您需要的示例:

你必须调整它来匹配你想要的。但是你可以看到它是如何工作的......

@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

这用于监控:

@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

您可以在这个地址获得完整的描述:

  http://www.cocoanetics.com/2013/11/can-you-smell-the-ibeacon/

关于ios - 如何获得配对的蓝牙设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23855844/

相关文章:

ios - UITableViewCell 何时何地完成布局?

ios - 菜单内容不会溢出它在 iOS 的 Safari 中的父项

ios - 获取 sharedApplication' 在我的苹果 watch 应用程序中不可用

ios - 在初始运行时打开不同的 View

bluetooth - bluez 同时具有经典和低能耗设备

c# - 蓝牙串行端口 (SPP) 传入端口创建

ios - UITableViewCell UIView 颜色在类加载时不会立即更改

ios - UILabel 闪烁效果

objective-c - 寻找过度释放的根源

Android 蓝牙Gatt : Unhandled execption in callback