繁体   English   中英

Swift BLE“ didDiscoverServices”未执行。 我想念什么吗?

[英]Swift BLE “didDiscoverServices” not executing. Am I missing something?

Peripherial.services为零。 会不会是个问题?

extension OpenSesameVC: CBCentralManagerDelegate {

    func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {


        let identifier = "\(peripheral.identifier)"
        var deviceName = ""

        if let name = peripheral.name, !name.isEmpty {
            print("\nName: \(name)")
            deviceName = name
            print("\nIdentifier: \(identifier)")
        } else {
            //print("\nName: ")
        }

        if deviceName == "Amp" {
            // We've found it so stop scan
            self.stopScanForBLEDevices()

            // Copy the peripheral instance
            self.mainPeripheral = peripheral
            self.mainPeripheral.delegate = self

            manager.connect(mainPeripheral, options: nil)
            loadingNotification.label.text = "Device Found, Connecting..."


        }
    }

    func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
        mainPeripheral = nil
        print("Disconnected" + peripheral.name!)
    }


    func centralManagerDidUpdateState(_ central: CBCentralManager) {
        print(central.state)
        switch central.state {
        case .unknown:
            print("unknown")
        case .resetting:
            print("resetting")
        case .unsupported:
            print("unsupported")
        case .unauthorized:
            print("unauthorized")
        case .poweredOff:
            print("poweredOff")
            if macID.count > 0 {
                UIView.animate(withDuration: 0.7) {
                    self.lblBluetoothAlert.alpha = 1.0
                }
            }
            stopScanForBLEDevices()
        case .poweredOn:
            if macID.count > 0 {
                UIView.animate(withDuration: 0.7) {
                    self.lblBluetoothAlert.alpha = 0.0
                }
             }
            scanBLEDevices()
            print("poweredOn")
        default:
            print("default")
        }
    }

    func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {

        //pass reference to connected peripheral to parent view
        if peripheral == self.mainPeripheral {

            self.mainPeripheral.delegate = self
            self.mainPeripheral.discoverServices(nil)

            loadingNotification.label.text = "Connected, Looking for Services..."
        }

    }
}

extension OpenSesameVC: CBPeripheralDelegate {
    func didReadValueForCharacteristic(_ characteristic: CBCharacteristic) {
        //characteristic.uuid == BleDeviceProfile.MAC_ADDRESS,
        if let mac_address = characteristic.value?.hexEncodedString().uppercased() {
            let macAddress = mac_address.separate(every: 2, with: ":")
            print("MAC_ADDRESS: \(macAddress)")
        }
    }

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

        for service in peripheral.services! {

            print("Service found with UUID: " + service.uuid.uuidString)

            //device information service
            if (service.uuid.uuidString == "180A") {
                peripheral.discoverCharacteristics(nil, for: service)
            }

            //GAP (Generic Access Profile) for Device Name
            // This replaces the deprecated CBUUIDGenericAccessProfileString
            if (service.uuid.uuidString == "1800") {
                peripheral.discoverCharacteristics(nil, for: service)
            }

            //Bluno Service
            if (service.uuid.uuidString == BLEService) {
                peripheral.discoverCharacteristics(nil, for: service)
            }

        }
    }

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

        //get device name
        if (service.uuid.uuidString == "1800") {

            for characteristic in service.characteristics! {

                if (characteristic.uuid.uuidString == "2A00") {
                    peripheral.readValue(for: characteristic)
                    print("Found Device Name Characteristic")
                }

            }

        }

        if (service.uuid.uuidString == "180A") {

            for characteristic in service.characteristics! {

                if (characteristic.uuid.uuidString == "2A29") {
                    peripheral.readValue(for: characteristic)
                    print("Found a Device Manufacturer Name Characteristic")
                } else if (characteristic.uuid.uuidString == "2A23") {
                    peripheral.readValue(for: characteristic)
                    print("Found System ID")
                }

            }

        }

        if (service.uuid.uuidString == BLEService) {

            for characteristic in service.characteristics! {

                if (characteristic.uuid.uuidString == BLECharacteristic) {
                    //we'll save the reference, we need it to write data
                    mainCharacteristic = characteristic

                    //Set Notify is useful to read incoming data async
                    peripheral.setNotifyValue(true, for: characteristic)
                    print("Found Bluno Data Characteristic")
                }

            }

        }

    }

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

        if (characteristic.uuid.uuidString == "2A00") {
            //value for device name recieved
            let deviceName = characteristic.value
            print(deviceName ?? "No Device Name")
        } else if (characteristic.uuid.uuidString == "2A29") {
            //value for manufacturer name recieved
            let manufacturerName = characteristic.value
            print(manufacturerName ?? "No Manufacturer Name")
        } else if (characteristic.uuid.uuidString == "2A23") {
            //value for system ID recieved
            let systemID = characteristic.value
            print(systemID ?? "No System ID")
        } else if (characteristic.uuid.uuidString == BLECharacteristic) {
            //data recieved
            if(characteristic.value != nil) {
                let stringValue = String(data: characteristic.value!, encoding: String.Encoding.utf8)!

                print(stringValue)
            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM