Iphone检测蓝牙设备

时间:2013-05-21 11:40:27

标签: iphone ios detection core-bluetooth bluetooth-lowenergy

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

2 个答案:

答案 0 :(得分:0)

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

如果没有尝试:

[mgr scanForPeripheralsWithServices:nil options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:CBCentralManagerScanOptionAllowDuplicatesKey]];

答案 1 :(得分:0)

您的代码似乎很好。 Search the app store for TI BLE for their free iPhone app.

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