Ok, a quick overview of what you need to do to get started:
1.) Before you can scan something, you need to select CentralManager, accept its delegate and wait to receive the delegate callback:
- Select the center part indicated in the title
central = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:nil];
-After waiting for the delegate callback
- (void)centralManagerDidUpdateState:(CBCentralManager *)central { if(central.state == CBCentralManagerStatePoweredOn) {
2.) If you want to use IBAction to control the scan, you also need to check the status every time.
- (IBAction)startScan:(id)sender { if(central.state == CBCentralManagerStatePoweredOn) {
3.) Now, since you already configured the delegate and scan for devices, just wait until the didDiscoverPeripheral callback occurred:
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
4.) Save the detected CBPeripherals inside the array or only the names of peripheral devices (depending on your use case). Submit this to your table data and call reloadData for each new peripheral discovery. You can continue and allow duplicates if you want to keep track of which ones are nearby, and you can remove / add depending on the rssi time / advertisement found.
Note. The central device should ideally be configured inside the Singleton class in which you accept its delegation methods inside your view controller. (But you can wet your feet this way).
Hope this helps!
Tommy devoy
source share