我正在测试应用程序以显示附近的蓝牙设备。现在,代码很简单如下:
- (IBAction)scanForDevices:(id)sender
{
UIButton * btn = sender;
if(self.cManager.state != CBCentralManagerStatePoweredOn)
{
NSLog(@"CoreBluetooth is %s",[self centralManagerStateToString:self.cManager.state]);
}
if(!self.isSearching){
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO], CBCentralManagerScanOptionAllowDuplicatesKey, nil];
[self.cManager scanForPeripheralsWithServices:nil options:options];
btn.titleLabel.text = @"Stop";
self.isSearching = YES;
}
else
{
btn.titleLabel.text = @"Search";
self.isSearching = NO;
}
}
#pragma mark - CBCentral Delegate
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
NSLog(@"Ready to discovery..");
}
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
NSLog(@"Receive periferal: %@", peripheral);
}
但每次按下按钮执行scanForDevices时,都会调用回调函数,但外围设备的值为null。我不确定我的设备是否正常工作。在我的笔记本电脑上,我的iphone 4S(6.0.1)和ipad(7.1)都被发现了,但不是来自iphone和ipad。
答案 0 :(得分:0)
您是否尝试过重置设备上的网络设置?这修复了我曾经遇到的一些与随机核心蓝牙相关的问题。