iOS藍牙搜索

iOS的藍牙是這樣用滴:

代理:

<CBCentralManagerDelegate, 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];

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章