我正在尝试制作一个简单的应用程序,它将扫描附近的蓝牙设备并在发现它们时列出它们的名称。我CoreBluetooth
按照我找到的所有指南使用,包括此处的 Apple 指南
但是,它永远不会起作用。我在运行该应用程序的 iPhone 5 旁边将 iPhone 4S 置于可发现模式,但它从未发现它。我也尝试过支持蓝牙的汽车,但我不知道它是否有 BLE。我究竟做错了什么?这是我的代码的精髓,在ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
[activity stopAnimating]; isScanning = NO; //activity is a GUI activity wheel
centralManager = [[CBCentralManager alloc] initWithDelegate: self queue: nil];
}
- (void)centralManagerDidUpdateState:(CBCentralManager *)central{
int state = central.state;
[self log: [NSString stringWithFormat: @"CBCentralManagerDidUpdateState: %d", state]];
//[self log] just NSLogs the message and adds it to a text view for the user to see.
if (state!=CBCentralManagerStatePoweredOn){
[self log: @"Error! Bluetooth not powered on!"]; //I never get this error.
}
}
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{
[self log: [NSString stringWithFormat: @"Peripheral found with CoreBluetooth: %@", peripheral.name]];
//And I never see any of these "peripheral found" messages.
}
- (IBAction)scanButton:(id)sender {
if (!isScanning){
[activity startAnimating];
isScanning = YES;
[centralManager scanForPeripheralsWithServices:nil options:nil];
[self log: @"Scanning started."];
}
else{
[activity stopAnimating];
isScanning = NO;
[centralManager stopScan];
[self log: @"Scanning stopped."];
}
}
感谢您的任何建议。