Ios Swift/BLE通知不起作用

Ios Swift/BLE通知不起作用,ios,swift,bluetooth-lowenergy,notify,Ios,Swift,Bluetooth Lowenergy,Notify,我正在使用Ti sensortag玩蓝牙和传感器。我的目标是让气压计和按钮正常工作。按下按钮时会发出通知,但气压计根本不会发出通知或改变其值。代码如下: import UIKit import CoreBluetooth class ViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate{ var centralManager:CBCentralManager! var

我正在使用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)")
        }

    }

}
这是属性表:


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

根据该文档,您需要写入0x01到0xAA42以启动气压计,写入0x01到0x2902以启用通知STHX,我也这么认为,但另一方面,我没有对按钮执行此操作,因此我感到困惑。我尝试了peripheral.writeValuedata,forCharacteristic:,type:,但未能定义正确的数据对象。你能再帮我一次吗?你能从ti下载示例代码吗?我只是看了一下,它显示了您需要编写的各种特性。我得到了用objective-c编写的SENSORTAG-IOS-OSX源代码示例,我找到了相关的代码。问题是我对objective-c一无所知。你有swift示例代码吗?恐怕没有。如果查看deviceSelector.m,您可以看到所需的UUID。d只是一本字典。如果你打算写iOS代码,那么我认为你需要学习一些关于Objective-c的知识,因为该语言中有很多代码和示例。除了方法调用的[]符号外,它与其他语言没有太大区别,因此swift中的someObj.somefunc应该是[someObj somefunc]