Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 3-核心蓝牙-背景-阵列参数_Swift_Bluetooth_Background_Core_Discover - Fatal编程技术网

Swift 3-核心蓝牙-背景-阵列参数

Swift 3-核心蓝牙-背景-阵列参数,swift,bluetooth,background,core,discover,Swift,Bluetooth,Background,Core,Discover,我使用以下设置在后台运行蓝牙扫描,singleton: class BluetoothService: NSObject { var manager: CBCentralManager! var characteristic: CBCharacteristic! var peripheral: CBPeripheral! let peripheral_name: String = "mygadget" let service_uuid_array: [

我使用以下设置在后台运行蓝牙扫描,singleton:

class BluetoothService: NSObject {

    var manager: CBCentralManager!
    var characteristic: CBCharacteristic!

    var peripheral: CBPeripheral!
    let peripheral_name: String = "mygadget"
    let service_uuid_array: [CBUUID] = [CBUUID(string: "85948G74-245Z-4861-G54R-198F368E380M")]

    static let sharedInstance = BluetoothService()
    private override init() {
        super.init()
        manager = CBCentralManager(delegate: self, queue: nil)
    }

}

extension BluetoothService: CBCentralManagerDelegate {

    func centralManagerDidUpdateState(_ central: CBCentralManager) {
        if (central.state == CBManagerState.poweredOn) {
            self.manager?.scanForPeripherals(withServices: service_uuid_array, options: nil)
        }
    }

    func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
        if (peripheral.name == self.peripheral_name) {
            self.manager.stopScan()
            self.peripheral = peripheral
            self.peripheral.delegate = self
            self.manager.connect(peripheral, options: nil)
        }
    }

    func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
        peripheral.discoverServices(nil)
    }

    func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
        if (central.state == CBManagerState.poweredOn) {
            self.manager?.scanForPeripherals(withServices: service_uuid_array, options: nil)
        }
    }

    func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) {
    }

}

extension BluetoothService: CBPeripheralDelegate {

    func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
    }

    func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
    }

    func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
    }

    func peripheral(_ peripheral: CBPeripheral, didUpdateNotificationStateFor characteristic: CBCharacteristic, error: Error?) {
    }

}
我还激活了背景模式并激活了“使用蓝牙LE配件”

当我使用

self.manager?.scanForPeripherals(withServices: nil, options: nil)
而不是

self.manager?.scanForPeripherals(withServices: service_uuid_array, options: nil)
它在前景中运行良好,但当我通过此阵列时,它无法运行。。。为什么?

我需要使用“service\u uuid\u array”以使其在后台工作

有人能帮我解决这个问题吗


谢谢和问候

以前没有人遇到过这个问题?你找到解决办法了吗?以前没有人遇到过这个问题?你找到解决办法了吗?