iOS core bluetooth CBCentralManager scanForPeripheralsWithServices no results

≡放荡痞女 提交于 2019-12-11 05:34:28

问题


I want to scan all the bluetooth devices in a ViewController, on a iOS 7 application, like this :

In ViewController.h

#import <UIKit/UIKit.h>
#import <CoreBluetooth/CoreBluetooth.h>

@interface ViewController : UIViewController <CBCentralManagerDelegate>

@property (strong, nonatomic) CBCentralManager *centralManager;

@end

In ViewController.m

#import "ViewController.h"

@interface ViewController ()
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.centralManager = [[CBCentralManager alloc]initWithDelegate:self queue:nil];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - CBCentralManager Delegate

- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
    if (self.centralManager.state == CBCentralManagerStatePoweredOn)
    {
        NSLog(@"is on");
        [self.centralManager scanForPeripheralsWithServices:nil options:nil];

    }
}


- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{   
    NSLog(@"Discovered %@", peripheral.name);

}

I have the log message "is on" but i have no results, I find no bluetooth device when there are several around me... why ?


回答1:


First of all, you will have to confirm if all the bluetooth devices around you are bluetooth low energy devices indeed, all bluetooth 4.0 devices aren't bluetooth low energy devices. And as far as I have seen my mac is not yet detected when I scan for peripherals. Secondly, you won't get the name of a peripheral device from peripheral.name unless and until the peripheral device has been paired atleast once. You may try this method to get the name if there is any without pairing, call this under didDiscoverPeripheral method

[advertisementData valueForKey:@"kCBAdvDataLocalName"]

If there is a local name for the peripheral, it will get displayed now.



来源:https://stackoverflow.com/questions/21392987/ios-core-bluetooth-cbcentralmanager-scanforperipheralswithservices-no-results

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!