iOS(Swift)でbleデバイスからデータを読み取ることができます。ble ペリフェラルから時間間隔で定期的にデータを読み取るにはどうすればよいですか? 以下のコードのように NSTimer を試しました。しかし、Appdelegate で ad1: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)")
}
}