我正在做一个 iPhone 应用程序,我需要获取附近的蓝牙设备。我正在使用 CoreBluetooth 框架,但它不起作用。我知道这个框架只适用于低功耗蓝牙,所以我买了一个支持低功耗的蓝牙 4.0 USB 适配器。我正在我的 iPhone 5 中测试这个应用程序。这是我的代码
- (void)centralManagerDidUpdateState:(CBCentralManager *)central{
NSString *messtoshow;
switch (central.state) {
case CBCentralManagerStateUnknown:
{
messtoshow=[NSString stringWithFormat:@"State unknown, update imminent."];
break;
}
case CBCentralManagerStateResetting:
{
messtoshow=[NSString stringWithFormat:@"The connection with the system service was momentarily lost, update imminent."];
break;
}
case CBCentralManagerStateUnsupported:
{
messtoshow=[NSString stringWithFormat:@"The platform doesn't support Bluetooth Low Energy"];
break;
}
case CBCentralManagerStateUnauthorized:
{
messtoshow=[NSString stringWithFormat:@"The app is not authorized to use Bluetooth Low Energy"];
break;
}
case CBCentralManagerStatePoweredOff:
{
messtoshow=[NSString stringWithFormat:@"Bluetooth is currently powered off."];
break;
}
case CBCentralManagerStatePoweredOn:
{
messtoshow=[NSString stringWithFormat:@"Bluetooth is currently powered on and available to use."];
[mgr scanForPeripheralsWithServices:[NSArray arrayWithObject:[CBUUID UUIDWithString:@"180A"]] options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:CBCentralManagerScanOptionAllowDuplicatesKey]];
[mgr retrieveConnectedPeripherals];
break;
}
}
NSLog(messtoshow);
}
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:
(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
NSLog(@"Did discover peripheral. peripheral: %@ rssi: %@, UUID: %@ advertisementData: %@ ", peripheral, RSSI, peripheral.UUID, advertisementData);
[mgr retrievePeripherals:[NSArray arrayWithObject:(id)peripheral.UUID]];
}
-(void)centralManager:(CBCentralManager *)central didRetrievePeripherals:(NSArray *)peripherals{
NSLog(@"This is it!");
}
此打印“蓝牙当前已打开并可使用。”,但找不到我的 USB 蓝牙或附近的任何其他蓝牙设备。我需要一个解决方案,我快疯了。非常感谢。