Skip to content

Instantly share code, notes, and snippets.

@yostane
Created March 15, 2017 16:06
Show Gist options
  • Select an option

  • Save yostane/2c69da69839747f20b3099ef823f8954 to your computer and use it in GitHub Desktop.

Select an option

Save yostane/2c69da69839747f20b3099ef823f8954 to your computer and use it in GitHub Desktop.
func read(){
//read a value from the characteristic
let readFuture = self.dataCharacteristic?.read(timeout: 5)
readFuture?.onSuccess { (_) in
//the value is in the dataValue property
let s = String(data:(self.dataCharacteristic?.dataValue)!, encoding: .utf8)
DispatchQueue.main.async {
self.valueLabel.text = "Read value is \(s)"
print(self.valueLabel.text!)
}
}
readFuture?.onFailure { (_) in
self.valueLabel.text = "read error"
}
}
func write(){
self.valueToWriteTextField.resignFirstResponder()
guard let text = self.valueToWriteTextField.text else{
return;
}
//write a value to the characteristic
let writeFuture = self.dataCharacteristic?.write(data:text.data(using: .utf8)!)
writeFuture?.onSuccess(completion: { (_) in
print("write succes")
})
writeFuture?.onFailure(completion: { (e) in
print("write failed")
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment