1

我正在做一个 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 蓝牙或附近的任何其他蓝牙设备。我需要一个解决方案,我快疯了。非常感谢。

4

2 回答 2

0

您确定 180A 是正确的服务吗?

如果不尝试:

[mgr scanForPeripheralsWithServices:nil options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:CBCentralManagerScanOptionAllowDuplicatesKey]];
于 2013-05-21T12:44:46.937 回答
0

你的代码看起来不错。 在应用商店中搜索 TI BLE 以获取他们的免费 iPhone 应用。

如果他们的应用程序无法发现您的蓝牙设备,请确保您的蓝牙设备已开启并且正在投放广告。

于 2013-08-30T20:46:10.873 回答