IOS蓝牙通知值更新

时间:2017-05-03 18:09:59

标签: ios bluetooth

编写蓝牙应用程序以读取模块的值。我试图设置其中一个特征,以便在值更改时通知。它会在连接时触发func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?)一次调用,但在此之后似乎没有触发更多,即使我知道值每5秒更改一次。我做错了什么?

import UIKit
import CoreBluetooth

class ViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate {
    var manager: CBCentralManager!
    var watch: CBPeripheral!
    var devices: [CBPeripheral] = []
    var closestDevice: CBPeripheral? = nil
    var closestRSSI = 0

    let SERVICE_UUID = "06758213-258D-44B2-A577-8AE43E9DF674"
    let ALERT_UUID = "B9FBC271-666B-4CA7-8F9C-F8E9C0223D20"
    let BATTERY_UUID = "4D757E85-6A28-48CB-9CF5-299CB72D5AB2"
    let FIRMWARE_UUID = "64CD5AF5-B6EE-4D46-A164-246BB197F5DA"
    let CLIENT_CONFIG_UUID = "00002902-0000-1000-8000-00805F9B34FB"

    @IBOutlet weak var connectButton: UIButton!
    @IBOutlet weak var infoLabel: UILabel!
    @IBOutlet weak var headLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        manager = CBCentralManager(delegate: self, queue: nil)
    }

    @IBAction func connectButtonPressed(_ sender: Any) {
        manager.stopScan()
        print("\nButton Pressed")
        print("Closest Device Name: \(closestDevice!.name!) Closest Device RSSI: \(closestRSSI)")

        self.watch = closestDevice
        watch.delegate = self
        manager.connect(watch, options: nil)

        //connectButton.isEnabled = false
    }

    func centralManagerDidUpdateState(_ central: CBCentralManager) {
        switch central.state{
        case .unauthorized:
            print("Unauthorized to use BLE")
            break
        case .poweredOff:
            print("Bluetooth is currently powered off")
            break
        case .poweredOn:
            print("Bluetooth is ready to use")
            central.scanForPeripherals(withServices: nil, options: nil)
            break
        case .resetting:
            print("Blueooth is resetting")
            break
        case .unknown:
            print("Bluetooth is unknown")
            break
        case .unsupported:
            print("Bluetooth is unsupported")
            break
        }
    }

    func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
        print(peripheral.name ?? "Null")

        if (peripheral.name == "Watch"){
            if closestDevice == nil {
                closestDevice = peripheral
                closestRSSI = Int(RSSI)
            }else{
                if closestRSSI > Int(RSSI){
                    closestDevice = peripheral
                    closestRSSI = Int(RSSI)
                }
            }
        }
    }

    func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
        print("Did Connect\n")
        peripheral.discoverServices(nil)
    }

    func peripheral(_ peripheral: CBPeripheral, didUpdateNotificationStateFor characteristic: CBCharacteristic, error: Error?) {
        print("Notification State Updated for \(characteristic.description) - \(characteristic.isNotifying)")
    }

    func peripheral(_ peripheral: CBPeripheral, didDiscoverDescriptorsFor characteristic: CBCharacteristic, error: Error?) {
        print("Discriptors found for \(characteristic.description)")
    }

    func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
        print("Discover Services")
        for service in peripheral.services! {
            let thisService = service as CBService

            let UUIDMatch: Bool = thisService.uuid.uuidString == SERVICE_UUID

            if UUIDMatch{
                print("Found Main Service: \(SERVICE_UUID)")
                let _ = peripheral.discoverCharacteristics(nil, for: thisService)
            }else{
                print("Debug Time:\n\(thisService.uuid.uuidString)")
                print(SERVICE_UUID)
            }
        }
    }

    func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
        print("Discover Characteristics - Found \(service.characteristics!.count) characteristics\n")

        for characteristic in service.characteristics! {
            let thisCharacteristic = characteristic as CBCharacteristic
            if thisCharacteristic.uuid.uuidString == ALERT_UUID {
                peripheral.setNotifyValue(true, for: thisCharacteristic)
                watch.readValue(for: thisCharacteristic)
                print("Set Alert Notify True")
            }
        }
    }

    func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
        print("Characteristic Changed\n")
        if(characteristic.uuid.uuidString == ALERT_UUID){
            let value = characteristic.value!
            infoLabel.text = String(value[value.endIndex])
        }
    }
}

跑步时打印的是什么

Bluetooth is ready to use
Null
Null
Watch
Null
Null
One
Null
Null

Button Pressed
Closest Device Name: Watch Closest Device RSSI: -50
Did Connect

Discover Services
Found Main Service: 06758213-258D-44B2-A577-8AE43E9DF674
Discover Characteristics - Found 5 characteristics

Set Alert Notify True
Notification State Updated for <CBCharacteristic: 0x1740bab20, UUID = B9FBC271-666B-4CA7-8F9C-F8E9C0223D20, properties = 0x12, value = <00>, notifying = YES> - true
Characteristic Changed

0 个答案:

没有答案