iOS蓝牙搜索

iOS的蓝牙是这样用滴:

代理:

, CBPeripheralDelegate>

@property (nonatomic, strong) CBCentralManager *manager;

@property (nonatomic, strong) CBPeripheral *peripheral;

- (void)viewDidLoad {

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

}


-(void)centralManagerDidUpdateState:(CBCentralManager *)central

{

    //判断蓝牙是否开启

    if(central.state != CBCentralManagerStatePoweredOn){

        return;

    }

    //开始搜索

    [_manager scanForPeripheralsWithServices:nil options:@{CBCentralManagerScanOptionAllowDuplicatesKey : @YES }];

    //停止搜索

    [self performSelector:@selector(stoplescan) withObject:nil afterDelay:5];


}


-(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {

    // 扫描到的蓝牙设备

    NSLog(@"已发现 peripheral: %@ rssi: %@, UUID: %@ advertisementData: %@ ", peripheral.name, RSSI, peripheral.identifier, advertisementData);

}


-(void)stoplescan{

    // 5s后停止扫描

    NSLog(@"5秒后停止");

    [_manager stopScan];

}


你可能感兴趣的:(iOS控件基础)