BLE Swift写字符

我正在努力让我的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 …似乎没有改变任何东西。 我查看了http://www.ti.com/tool/sensortag-sw中找到的客观c示例,并考虑相应的行

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

这些是:

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

我错过了什么?

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

 var parameter = NSInteger(1) let data = NSData(bytes: &parameter, length: 1) peripheral.writeValue(data, forCharacteristic: characteric, type: CBCharacteristicWriteType.WithResponse) 

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