Swift / BLE通知不起作用

时间:2014-10-13 12:00:13

标签: ios swift bluetooth-lowenergy notify

我正在使用Ti sensortag来玩蓝牙和传感器。我的目标是让气压计和按钮工作。按下时按钮会发出通知,但气压计根本不会通知或更改其值。这是代码:

import UIKit
import CoreBluetooth

class ViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate{

    var centralManager:CBCentralManager!
    var blueToothReady = false
    var connectingPeripheral: CBPeripheral!

    let data = "01".dataUsingEncoding(NSUTF8StringEncoding)
    @IBOutlet weak var textField: UITextView!

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        startUpCentralManager()
    }

    func startUpCentralManager() {
        centralManager = CBCentralManager(delegate: self, queue: nil)
    }

    func discoverDevices() {
        centralManager.scanForPeripheralsWithServices(nil, options: nil)
    }

    func centralManager(central: CBCentralManager!, didDiscoverPeripheral peripheral: CBPeripheral!, advertisementData: (NSDictionary), RSSI: NSNumber!) {

        println("Discovered: \(peripheral.name)")
        textField.text = textField.text + "Discovered: \(peripheral.name)\n"


        self.connectingPeripheral = peripheral
        centralManager.stopScan()
        self.centralManager.connectPeripheral(peripheral, options: nil)

    }

    func centralManagerDidUpdateState(central: CBCentralManager!) { //BLE status
        var msg = ""
        switch (central.state) {
        case .PoweredOff:
            msg = "CoreBluetooth BLE hardware is powered off"
            println("\(msg)")

        case .PoweredOn:
            msg = "CoreBluetooth BLE hardware is powered on and ready"
            blueToothReady = true;

        case .Resetting:
            var msg = "CoreBluetooth BLE hardware is resetting"

        case .Unauthorized:
            var msg = "CoreBluetooth BLE state is unauthorized"

        case .Unknown:
            var msg = "CoreBluetooth BLE state is unknown"

        case .Unsupported:
            var msg = "CoreBluetooth BLE hardware is unsupported on this platform"

        }
        textField.text = textField.text + "\(msg)\n"

        println(msg)

        if blueToothReady {
            discoverDevices()
        }
    }

    func centralManager(central: CBCentralManager!,didConnectPeripheral peripheral: CBPeripheral!)
    {
        peripheral.delegate = self
        peripheral.discoverServices([CBUUID.UUIDWithString("F000AA40-0451-4000-B000-000000000000"),CBUUID.UUIDWithString("FFE0")])
        println("Connected")
        textField.text = textField.text + "Connected\n"

    }

    func peripheral(peripheral: CBPeripheral!, didDiscoverServices error: NSError!)
    {
        if let servicePeripherals = peripheral.services as? [CBService]
        {
            for servicePeripheral in servicePeripherals
            {
                println("Service: \(servicePeripheral.UUID)")
                textField.text = textField.text + "Service: \(servicePeripheral.UUID)\n"
                peripheral.discoverCharacteristics(nil, forService: servicePeripheral)
            }
        }
    }

    @IBAction func refreshBLE(sender: UIButton) {
        centralManager.scanForPeripheralsWithServices(nil, options: nil)
    }

    func peripheral(peripheral: CBPeripheral!, didDiscoverCharacteristicsForService service: CBService!, error: NSError!) {
        if let charactericsArr = service.characteristics  as? [CBCharacteristic]
        {
            for charactericsx in charactericsArr
            {
                peripheral.setNotifyValue(true, forCharacteristic: charactericsx)
                println("Characteristic: \(charactericsx)")
                textField.text = textField.text + "Characteristic: \(charactericsx)\n"

                peripheral.readValueForCharacteristic(charactericsx)
            }

        }
    }

    func peripheral(peripheral: CBPeripheral!, didUpdateValueForCharacteristic characteristic: CBCharacteristic!, error: NSError!) {
        if var data :NSData = characteristic.value {
            textField.text = textField.text + "Data: \(characteristic.value)\n Notifying: \(characteristic.isNotifying)\n"
            println("Data: \(characteristic.value)\n Notifying: \(characteristic.isNotifying)")
        }

    }

}

这是属性表:http://processors.wiki.ti.com/images/a/a8/BLE_SensorTag_GATT_Server.pdf

为什么我只获得气压计的初始值而没有通知?

0 个答案:

没有答案