【问题标题】:BLE Swift write characterisitcBLE Swift 写入特性
【发布时间】:2014-12-10 05:44:04
【问题描述】:

我正在努力让我的 TI sensortag 温度传感器发出通知。根据http://processors.wiki.ti.com/images/a/a8/BLE_SensorTag_GATT_Server.pdf,我需要将 UUID F000AA02-0451-4000-B000-000000000000 的特征值设置为“01:00”。这是我的工作:

import UIKit
import CoreBluetooth

class ViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate{

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

    @IBOutlet weak var textField: UITextView!

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    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!) {

        output("Discovered", data: peripheral.name)

        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"

        }
        output("State", data: msg)

        if blueToothReady {
            discoverDevices()
        }
    }

    func centralManager(central: CBCentralManager!,didConnectPeripheral peripheral: CBPeripheral!)
    {
        peripheral.delegate = self
        peripheral.discoverServices([CBUUID.UUIDWithString("F000AA00-0451-4000-B000-000000000000")])
        output("Connected", data: peripheral.name)

    }

    func peripheral(peripheral: CBPeripheral!, didDiscoverServices error: NSError!)
    {

        if let servicePeripherals = peripheral.services as? [CBService]
        {
            for servicePeripheral in servicePeripherals
            {
                output("Service", data: servicePeripheral.UUID)
                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)

            //                *************************
            if charactericsx.UUID.UUIDString == "F000AA02-0451-4000-B000-000000000000"{
                output("Characteristic", data: charactericsx)
                let data: NSData = "01:00".dataUsingEncoding(NSUTF8StringEncoding)!
                peripheral.writeValue(data, forCharacteristic: charactericsx, type: CBCharacteristicWriteType.WithResponse)
                output("Characteristic", data: charactericsx)
            }
            //                *************************

                peripheral.readValueForCharacteristic(charactericsx)
            }

        }
    }

    func peripheral(peripheral: CBPeripheral!, didUpdateValueForCharacteristic characteristic: CBCharacteristic!, error: NSError!) {
        if var data :NSData = characteristic.value {
            output("Data", data: characteristic.value)
        }

    }

    func output(description: String, data: AnyObject){
        println("\(description): \(data)")
        textField.text = textField.text + "\(description): \(data)\n"
    }

}

问题是 peripheral.writeValue... 似乎没有改变任何东西。我查看了此处找到的目标 c 示例http://www.ti.com/tool/sensortag-sw 并认为对应的行是

let data: NSData = "01:00".dataUsingEncoding(NSUTF8StringEncoding)!
peripheral.writeValue(data, forCharacteristic: characteric, type: CBCharacteristicWriteType.WithResponse)

这些是:

uint8_t data = 0x01;
[BLEUtility writeCharacteristic:self.d.p sCBUUID:sUUID cCBUUID:cUUID data:[NSData dataWithBytes:&data length:1]];

我错过了什么?

【问题讨论】:

    标签: ios swift bluetooth core-bluetooth


    【解决方案1】:

    您编写的 Swift 代码并不等同于 Objective-C 示例。 data 参数应该用二进制“1”而不是字符串“01:00”初始化:

    var parameter = NSInteger(1)
    let data = NSData(bytes: &parameter, length: 1)
    peripheral.writeValue(data, for: characteristic, type: .withResponse)
    

    我认为,每当 TI 文档在“01:00”之类的引号中指定一个值时,它们实际上意味着一个十六进制值,例如0x0100,这有点令人困惑。

    【讨论】:

    • 它还活着! =D 非常感谢,这真的让我很头疼
    • @Michal Ciuba 如果我想写“0802”而不是 1。我可以简单地做var parameter = NSInteger(0802)吗?
    • @NikhilGupta 试试这个:var parameter = 0x0802 let data = NSData(bytes: &parameter, length: 2)
    • @Michal Ciuba。您知道这种方法是否适用于新的 CC2650 SensorTag,因为 gyro/acc/mag 被组织在一个 MovementData UUID 下?如果它说需要将通知值设置为 0x0001 以启用这些传感器的通知,我是否只需替换您示例中的以下代码? Obv 我知道它不能是整数,但感谢您对此的反馈,因为它对我不起作用。 var 参数 = NSInteger(0x0001)
    • 不幸的是,我对 CC2650 SensorTag 一无所知。我唯一想到的是“字节顺序”:也许你可以改变字节的顺序?我的意思是,写:var parameter = NSInteger(0x0100)。除此之外,恐怕我帮不了你。
    猜你喜欢
    • 1970-01-01
    • 2018-06-13
    • 1970-01-01
    • 1970-01-01
    • 2017-04-23
    • 2018-02-11
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    相关资源
    最近更新 更多