0

我正在开发蓝牙 4.0 BLE 硬件设备。它以一种非常简单的方式工作。它通告单个服务 UUID 和包含 0xFF(制造商特定数据)的附加特殊负载。除了广告中的服务 UUID 之外,它不发布任何 GATT 配置文件数据。

主要思想是,当用户靠近设备时,应在 iphone 上显示一条消息。我不是想创建另一个 iBeacon 协议,但这个应用程序将有一个特定的目的 :)

该应用程序在前台运行时完美无缺,有时在应用程序处于后台时也可以运行,尤其是在我将手机置于睡眠状态后几分钟。我在 UIBackgroundModes 中启用了“蓝牙中央”背景模式。

当应用程序处于后台时,应用程序永远不会注意到蓝牙硬件在附近,这是很常见的,这是我需要帮助的主要问题。几个月来,我一直认为这就是 iOS 的工作方式。

现在我买了一个 Kensington Proximo 蓝牙追踪器 Fob,经过一些测试我发现这个设备真的可以唤醒 iPhone。我试图弄清楚如何。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    NSDictionary *options = @{ CBCentralManagerOptionRestoreIdentifierKey:@"myCentralManagerIdentifier", CBCentralManagerScanOptionAllowDuplicatesKey:[NSNumber numberWithBool:NO]};
    self.manager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:options];
    return YES;
}

- (void)centralManager:(CBCentralManager *)central willRestoreState:(NSDictionary *)dict {
    NSLog(@"Restored, %@", dict);
}

- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
    NSDictionary *options = @{ CBCentralManagerOptionRestoreIdentifierKey:@"myCentralManagerIdentifier", CBCentralManagerScanOptionAllowDuplicatesKey:[NSNumber numberWithBool:NO]};

    if (self.manager.state == CBCentralManagerStatePoweredOn) {
        //Note, the actual UUID has been replaced below:
        [self.manager scanForPeripheralsWithServices:[NSArray arrayWithObject:[CBUUID UUIDWithString:@"12345678-1234-1234-1234-123456789012"]] options:options];
    }
}

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
    UILocalNotification* localNotification = [[UILocalNotification alloc] init];
    localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:1];
    localNotification.alertBody = @"It is close!";
    localNotification.soundName = UILocalNotificationDefaultSoundName;
    localNotification.applicationIconBadgeNumber = 1;
    localNotification.timeZone = [NSTimeZone defaultTimeZone];
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
    NSLog(@"%@", advertisementData);
}
4

1 回答 1

0

检查https://developer.apple.com/library/ios/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/CoreBluetoothBackgroundProcessingForIOSApps/PerformingTasksWhileYourAppIsInTheBackground.html

简而言之:在后台,如果您的应用程序之前已经查看过设备,则在扫描时将不会检测到设备。您可以通过尝试连接到已知设备来连接它。我猜肯辛顿追踪器就是这样做的。

于 2014-10-10T20:16:54.490 回答