BLE Swift写特性

时间:2014-10-15 08:12:10

标签: ios swift bluetooth core-bluetooth

我正在努力让我的TI sensortag温度传感器通知。根据{{​​3}},我需要将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 ......似乎没有改变任何东西。我查看了http://processors.wiki.ti.com/images/a/a8/BLE_SensorTag_GATT_Server.pdf中找到的客观c示例,并将相应的行视为

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]];

我错过了什么?

1 个答案:

答案 0 :(得分:10)

您编写的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这样的十六进制值,这有点令人困惑。