项目作者: rinat-enikeev

项目描述 :
Framework for listening to RuuviTag BLE broadcasts
高级语言: Swift
项目地址: git://github.com/rinat-enikeev/BTKit.git
创建时间: 2019-06-09T17:21:18Z
项目社区:https://github.com/rinat-enikeev/BTKit

开源协议:BSD 3-Clause "New" or "Revised" License

下载


BTKit

Access Ruuvi BLE devices with dot syntax: device.ruuvi?.tag

Swift Version
License
PRs Welcome

Scan for Ruuvi BLE devices and access them with dot syntax. See tutorial.

Features

Requirements

  • iOS 10.0+
  • Xcode 12.0+

Installation

CocoaPods

You can use CocoaPods to install BTKit by adding it to your Podfile:

  1. platform :ios, '10.0'
  2. use_frameworks!
  3. pod 'BTKit'

Swift Package Manager

You can add link to this repo in XCode/File/Swift Packages/Add Package Dependency…

Usage example

To make it work import BTKit

  1. import BTKit

Check for iPhone/iPad Bluetooth state

  1. view.isBluetoothEnabled = scanner.bluetoothState == .poweredOn
  2. BTForeground.shared.state(self, closure: { (observer, state) in
  3. observer.view.isBluetoothEnabled = state == .poweredOn
  4. })

Listen to advertisements in foreground

  1. BTForeground.shared.scan(self) { (observer, device) in
  2. if let ruuviTag = device.ruuvi?.tag {
  3. print(ruuviTag)
  4. }
  5. }

Determine if device is out of range or went offline

  1. BTForeground.shared.lost(self, options: [.lostDeviceDelay(10)], closure: { (observer, device) in
  2. if let ruuviTag = device.ruuvi?.tag {
  3. print("Ruuvi Tag is offline or went out of range")
  4. }
  5. })

Observe specific device advertisements

  1. BTForeground.shared.observe(self, uuid: ruuviTag.uuid, options: [.callbackQueue(.untouch)]) { (observer, device) in
  2. print("Specific RuuviTag is advertising")
  3. }

Connect to specific device

  1. if ruuviTag.isConnectable {
  2. ruuviTag.connect(for: self, options: [.connectionTimeout(10)], connected: { observer, result in
  3. switch result {
  4. case .just:
  5. print("just connected")
  6. case .already:
  7. print("was already connected")
  8. case .disconnected:
  9. print("just disconnected")
  10. case .failure(let error):
  11. print(error.localizedDescription)
  12. }
  13. }, heartbeat: { observer, device in
  14. if let ruuviTag = device.ruuvi?.tag {
  15. print(ruuviTag)
  16. }
  17. }, disconnected: { observer, result in
  18. switch result {
  19. case .just:
  20. print("just disconnected")
  21. case .already:
  22. print("disconnected")
  23. case .stillConnected:
  24. print("still connected because of other callers")
  25. case .failure(let error):
  26. print(error.localizedDescription)
  27. }
  28. })
  29. }

or use uuid

  1. BTBackground.shared.connect(for: self, options: [.connectionTimeout(10)], connected: { observer, result in
  2. switch result {
  3. case .just:
  4. print("just connected")
  5. case .already:
  6. print("was already connected")
  7. case .disconnected:
  8. print("just disconnected")
  9. case .failure(let error):
  10. print(error.localizedDescription)
  11. }
  12. }, heartbeat: { observer, device in
  13. if let ruuviTag = device.ruuvi?.tag {
  14. print(ruuviTag)
  15. }
  16. }, disconnected: { observer, result in
  17. switch result {
  18. case .just:
  19. print("just disconnected")
  20. case .already:
  21. print("disconnected")
  22. case .stillConnected:
  23. print("still connected because of other callers")
  24. case .failure(let error):
  25. print(error.localizedDescription)
  26. }
  27. })
  28. }

Read temperature, humidity, pressure logs from the connectable device

  1. if let from = Calendar.current.date(byAdding: .minute, value: -5, to: Date()) {
  2. ruuviTag.celisus(for: self, from: from) { (observer, result) in
  3. switch result {
  4. case .success(let values):
  5. print(values)
  6. case .failure(let error):
  7. print(error.localizedDescription)
  8. }
  9. }
  10. ruuviTag.humidity(for: self, from: from) { (observer, result) in
  11. switch result {
  12. case .success(let values):
  13. print(values)
  14. case .failure(let error):
  15. print(error.localizedDescription)
  16. }
  17. }
  18. ruuviTag.pressure(for: self, from: from) { (observer, result) in
  19. switch result {
  20. case .success(let values):
  21. print(values)
  22. case .failure(let error):
  23. print(error.localizedDescription)
  24. }
  25. }
  26. }

or use BTKit if you know only the uuid:

  1. BTBackground.shared.services.ruuvi.nus.celisus(for: self, uuid: ruuviTag.uuid, from: from, result: { (observer, result) in
  2. switch result {
  3. case .success(let values):
  4. print(values)
  5. case .failure(let error):
  6. print(error.localizedDescription)
  7. }
  8. })
  9. BTBackground.shared.services.ruuvi.nus.humidity(for: self, uuid: ruuviTag.uuid, from: from, result: { (observer, result) in
  10. switch result {
  11. case .success(let values):
  12. print(values)
  13. case .failure(let error):
  14. print(error.localizedDescription)
  15. }
  16. })
  17. BTBackground.shared.services.ruuvi.nus.pressure(for: self, uuid: ruuviTag.uuid, from: from, result: { (observer, result) in
  18. switch result {
  19. case .success(let values):
  20. print(values)
  21. case .failure(let error):
  22. print(error.localizedDescription)
  23. }
  24. })

Read full log in one batch

  1. if let from = Calendar.current.date(byAdding: .minute, value: -5, to: Date()) {
  2. ruuviTag.log(for: self, from: from) { (observer, result) in
  3. switch result {
  4. case .success(let logs):
  5. print(logs)
  6. case .failure(let error):
  7. print(error.localizedDescription)
  8. }
  9. }
  10. }

Or use BTKit if you know only the uuid

  1. BTBackground.shared.services.ruuvi.nus.log(for: self, uuid: ruuviTag.uuid, from: from, result: { (observer, result) in
  2. switch result {
  3. case .success(let logs):
  4. print(logs)
  5. case .failure(let error):
  6. print(error.localizedDescription)
  7. }
  8. })

Disconnect from the device

  1. ruuviTag.disconnect(for: self) { (observer, result) in
  2. switch result {
  3. case .just:
  4. observer.isConnected = false
  5. case .already:
  6. observer.isConnected = false
  7. case .stillConnected:
  8. observer.isConnected = true
  9. case .failure(let error):
  10. observer.isConnected = false
  11. print(error.localizedDescription)
  12. }
  13. }

or use BTKit if you know only uuid

  1. BTBackground.shared.disconnect(for: self, uuid: ruuviTag.uuid) { (observer, result) in
  2. switch result {
  3. case .just:
  4. observer.isConnected = false
  5. case .already:
  6. observer.isConnected = false
  7. case .stillConnected:
  8. observer.isConnected = true
  9. case .failure(let error):
  10. observer.isConnected = false
  11. print(error.localizedDescription)
  12. }
  13. }

Contribute

We would love you for the contribution to BTKit, check the LICENSE file for more info.

Meta

Rinat Enikeev – rinat.enikeev@gmail.com

Distributed under the BSD license. See LICENSE for more information.