Skip to content

Instantly share code, notes, and snippets.



Created May 3, 2018
What would you like to do?
import UIKit
class ViewController: UIViewController {
var btHelper: BTHelper = BTHelper()
var timer: Timer? = nil
var bgTask = BackgroundTask()
@objc func stop() { btHelper.shutOff() }
@objc func action() { btHelper.startUp() }
// User selected a new time interval in minutes
// The 1 minute time out is for testing.
// App defaults to 5 minutes refocus time out.
@IBAction func poke(_ sender: Any) {
guard let sc = sender as? UISegmentedControl else { return }
timer = Timer.scheduledTimer(timeInterval: 60.0 * [1.0, 3.0, 5.0, 10.0, 15.0, 30.0][sc.selectedSegmentIndex], target: self, selector: #selector(action), userInfo: nil, repeats: true)
override func viewDidAppear(_ animated: Bool) {
// Live forever
// Listen for device. Buzz on connect.
// Observer lives forever. I don't bother to
// save it for deallocation.
NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "2A06"), object: nil, queue: .main) { note in
self.btHelper.vibrate(degree: .low)
self.perform(#selector(self.stop), with: nil, afterDelay: 5.0)
// Repeat every n seconds
timer = Timer.scheduledTimer(timeInterval: 60.0 * 10, target: self, selector: #selector(action), userInfo: nil, repeats: true)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.