我们正在开发蓝牙 iOS 应用程序。首先我们需要最近的蓝牙设备names
,放入uitableView
。所以我们使用corebluetooth.framework
。像这样尝试
- (void)viewDidLoad {
[super viewDidLoad];
self.peripherals=[NSMutableArray new];
NSDictionary *scanOptions = @{CBCentralManagerScanOptionAllowDuplicatesKey:@(YES)};
NSArray *services = @[[CBUUID UUIDWithString:@"F3501201-3F04-4744-BE91-154BB7D51BDC"]];
centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
}
-(void) centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
NSLog(@"Discovered peripheral %@",peripheral.identifier.UUIDString);
NSLog(@"Discovered peripheral %@",peripheral.name);
[self.peripherals addObject:peripheral];
[central connectPeripheral:peripheral options:nil];
}
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
switch (central.state)
{
case CBCentralManagerStateUnsupported:
{
NSLog(@"State: Unsupported");
} break;
case CBCentralManagerStateUnauthorized:
{
NSLog(@"State: Unauthorized");
} break;
case CBCentralManagerStatePoweredOff:
{
NSLog(@"State: Powered Off");
} break;
case CBCentralManagerStatePoweredOn:
{
NSLog(@"State: Powered On");
[centralManager scanForPeripheralsWithServices:services options:scanOptions];
} break;
case CBCentralManagerStateUnknown:
{
NSLog(@"State: Unknown");
} break;
default:
{
}
}
}
我们得到了State: Powered On。但是 didDiscoverPeripheral 方法从未被调用过。我们是新手。我们需要当我的应用程序打开时,所有蓝牙设备都放在 UItableView 中,然后我们点击特定的设备。我的代码中有什么错误请指导我们。