ios - 在后台快速启动线程

标签 ios multithreading swift2

我正在尝试使用以下代码在后台线程中发送进程:

let qualityOfServiceClass = QOS_CLASS_BACKGROUND
let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)

dispatch_async(backgroundQueue, {
    print("running in the background queue")
    btDiscovery
})

但是该类仅在前台开始时进行处理...有什么想法吗?

编辑1:

btDiscovery 是一个每 X 秒执行一次 BLE 设备扫描的类:

let btDiscoverySharedInstance = Beacon();

class Beacon: NSObject, CBCentralManagerDelegate {

private var centralManager: CBCentralManager?
private var peripheralBLE: CBPeripheral?
....


   func centralManagerDidUpdateState(central: CBCentralManager) {
    switch (central.state) {
    case CBCentralManagerState.PoweredOff:
        print("BLE powered off")
        self.clearDevices()

    case CBCentralManagerState.Unauthorized:
        // Indicate to user that the iOS device does not support BLE.
        print("BLE not supported")
        break

    case CBCentralManagerState.Unknown:
        // Wait for another event
        print("BLE unknown event")
        break

    case CBCentralManagerState.PoweredOn:
        print("BLE powered on")
            self.startScanning()
        break

    case CBCentralManagerState.Resetting:
        print("BLE reset")
        self.clearDevices()

    case CBCentralManagerState.Unsupported:
        print("BLE unsupported event")
        break
    }
}


    func startScanning() {
    print("Start scanning...")
        if let central = centralManager {
            central.scanForPeripheralsWithServices(nil, options: nil)
        }
    }

  func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {
    print("Discovered peripheral \(RSSI) dBM name: \(peripheral.name)")
    print("UUID: \(peripheral.identifier.UUIDString)")
    ...
    sleep(delayPolling)
    self.startScanning()
  }

当应用程序启动并保持在前台时,扫描会每隔“delayPolling”秒正确执行一次。 但是一旦我将我的应用程序置于后台,扫描就会暂停。只有当它再次回到前台时它才会重新启动。

我每次都需要让这个扫描在后台运行(即使我们为这个线程设置了较低的优先级)。

编辑2:

通过阅读文档 https://developer.apple.com/library/ios/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/CoreBluetoothBackgroundProcessingForIOSApps/PerformingTasksWhileYourAppIsInTheBackground.html

我看到了

当实现中心角色的应用程序在其 Info.plist 文件中包含具有 bluetooth-central 值的 UIBackgroundModes 键时,Core Bluetooth 框架允许您的应用程序在后台运行以执行某些与蓝牙相关的任务.当您的应用程序处于后台时,您仍然可以发现并连接到外围设备,探索外围设备数据并与之交互。此外,系统会在调用任何 CBCentralManagerDelegate 或 CBPeripheralDelegate 委托(delegate)方法时唤醒您的应用

我在 Info.plist 文件中选择了相应的选项:

enter image description here

但我的应用程序没有在后台运行我的线程。

最佳答案

我意识到这是一个老问题,但在后台扫描需要您提供服务 UUID。

central.scanForPeripheralsWithServices(nil, options: nil)

需要

central.scanForPeripheralsWithServices(serviceUUID, options: nil)

关于ios - 在后台快速启动线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38818250/

相关文章:

ios - HealthKit - 当用户不允许访问或关闭所有类别时,源查询是否返回应用程序?

ios - 在 iPhone 6 上的 Meteor Cordova 应用程序中进行缩放

ios - 以 NSIntegers 作为参数返回一个 NSNumbers 数组

c# - Dispatcher 的实用性和良好实践

java - Hibernate 无法初始化代理 - 在线程中访问对象时没有 session

ios - UIAlertView 索引按钮

java - 如何监听 Firebase setValue 完成

ios - iPhone 设备不连接到应用程序中的本地服务器

swift - 在 Swift 的 case 语句中赋值的 2 种方式

http - swift 2.0 : HTTP GET error handling in Xcode 7