【问题标题】:How to get paired bluetooth devices如何获得配对的蓝牙设备
【发布时间】:2014-07-14 09:13:32
【问题描述】:

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

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

【问题讨论】:

    标签: ios objective-c bluetooth


    【解决方案1】:

    这是您需要的示例:

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

    @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/
    

    【讨论】:

    • 你可以定义一个带有uuid、minor和major的区域。这些值需要与信标内部的值匹配。
    • 是的,uuid 是唯一值。 UUID 在 Core Foundation 中由 CFUUID 库处理。您正在寻找的功能是 CFUUIDCreate。您还可以使用应用程序 Beecon 轻松获取信标的价值。
    • [self.locationManager startRangingBeaconsInRegion:self.beaconRegion] 方法会返回一个具有自己值的信标数组
    • 我的朋友我如何获得 UUID?
    • -1 这与提出的问题无关。 CLBeaconRegion 适用于未“配对”的基于 BLE 的 ibeacons,而问题专门询问配对的蓝牙设备。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-26
    • 1970-01-01
    • 2011-10-19
    • 1970-01-01
    • 1970-01-01
    • 2016-04-08
    相关资源
    最近更新 更多