【问题标题】:Iphone detecting bluetooth devicesiphone检测蓝牙设备
【发布时间】:2013-05-16 04:03:01
【问题描述】:

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

【问题讨论】:

  • 我遇到了类似的问题。您在这里找到任何解决方案了吗?

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


【解决方案1】:

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

如果不尝试:

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

【讨论】:

【解决方案2】:

您的代码看起来不错。 Search the app store for TI BLE for their free iPhone app.

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-26
    • 2013-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-08
    相关资源
    最近更新 更多