0

我能够在 ios(swift) 中从 ble 设备读取数据。如何以时间间隔从 ble 外围设备定期读取数据?我尝试使用 NSTimer,如下面的代码所示。但是我在 Appdelegate 中得到了 thead1:SIGABRT ERROR。

func peripheral(peripheral: CBPeripheral, didDiscoverServices error: NSError?) {
    self.statusLabel.text = "Looking at peripheral services"
    for service in peripheral.services! {

        let thisService = service as CBService
        if service.UUID == IRTemperatureServiceUUID {
            // Discover characteristics of IR Temperature Service
            peripheral.discoverCharacteristics(nil, forService: thisService)
        }
        // Uncomment to print list of UUIDs
        //println(thisService.UUID)
    }
}


// Enable notification and sensor for each characteristic of valid service
func peripheral(peripheral: CBPeripheral, didDiscoverCharacteristicsForService service: CBService, error: NSError?) {


    // update status label
    self.statusLabel.text = "Enabling sensors"

    // 0x01 data byte to enable sensor
    var enableValue = 1
    let enablyBytes = NSData(bytes: &enableValue, length: sizeof(UInt8))


    // check the uuid of each characteristic to find config and data characteristics
    for charateristic in service.characteristics! {
        let thisCharacteristic = charateristic as CBCharacteristic
        // check for data characteristic


        if thisCharacteristic.UUID == IRTemperatureDataUUID {
            // Enable Sensor Notification

         var Say = NSTimer.scheduledTimerWithTimeInterval(10.0, target: self, selector: Selector(self.sensorTagPeripheral.readValueForCharacteristic(thisCharacteristic)),userInfo: nil, repeats: true)    //NStimer


        }


        // check for config characteristic
        if thisCharacteristic.UUID == IRTemperatureConfigUUID {
            // Enable Sensor
            self.sensorTagPeripheral.writeValue(enablyBytes, forCharacteristic: thisCharacteristic, type: CBCharacteristicWriteType.WithResponse)
        }
    }

}

@IBOutlet weak var testLabel: UILabel!


// Get data values when they are updated
func peripheral(peripheral: CBPeripheral, didUpdateValueForCharacteristic characteristic: CBCharacteristic, error: NSError?) {

    self.statusLabel.text = "Connected"
    var test:UInt32 = 0
    characteristic.value?.getBytes(&test,range: NSRange(location: 0, length: 1))
    Humidity.text = test.description
    print("Humidity\(test)")
    //    print("tst\(characteristic.value?.bytes)")

    var test1:UInt16 = 0
    characteristic.value?.getBytes(&test1,range: NSRange(location: 1, length: 1))
    Temperature.text = String(test1)
    print("Temperature\(test1)")



}

}
4

0 回答 0