Access Ruuvi BLE devices with dot syntax: device.ruuvi?.tag
Scan for Ruuvi BLE devices and access them with dot syntax. See tutorial.
- Listen to RuuviTag advertisements
- Connect/disconnect RuuviTag
- Read RuuviTag logs
- RuuviTag logging in background (firmware 3.27.2+)
- iOS 10.0+
- Xcode 12.0+
You can use CocoaPods to install BTKit
by adding it to your Podfile
:
platform :ios, '10.0'
use_frameworks!
pod 'BTKit'
You can add link to this repo in XCode/File/Swift Packages/Add Package Dependency...
To make it work import BTKit
import BTKit
view.isBluetoothEnabled = scanner.bluetoothState == .poweredOn
BTForeground.shared.state(self, closure: { (observer, state) in
observer.view.isBluetoothEnabled = state == .poweredOn
})
BTForeground.shared.scan(self) { (observer, device) in
if let ruuviTag = device.ruuvi?.tag {
print(ruuviTag)
}
}
BTForeground.shared.lost(self, options: [.lostDeviceDelay(10)], closure: { (observer, device) in
if let ruuviTag = device.ruuvi?.tag {
print("Ruuvi Tag is offline or went out of range")
}
})
BTForeground.shared.observe(self, uuid: ruuviTag.uuid, options: [.callbackQueue(.untouch)]) { (observer, device) in
print("Specific RuuviTag is advertising")
}
if ruuviTag.isConnectable {
ruuviTag.connect(for: self, options: [.connectionTimeout(10)], connected: { observer, result in
switch result {
case .just:
print("just connected")
case .already:
print("was already connected")
case .disconnected:
print("just disconnected")
case .failure(let error):
print(error.localizedDescription)
}
}, heartbeat: { observer, device in
if let ruuviTag = device.ruuvi?.tag {
print(ruuviTag)
}
}, disconnected: { observer, result in
switch result {
case .just:
print("just disconnected")
case .already:
print("disconnected")
case .stillConnected:
print("still connected because of other callers")
case .failure(let error):
print(error.localizedDescription)
}
})
}
or use uuid
BTBackground.shared.connect(for: self, options: [.connectionTimeout(10)], connected: { observer, result in
switch result {
case .just:
print("just connected")
case .already:
print("was already connected")
case .disconnected:
print("just disconnected")
case .failure(let error):
print(error.localizedDescription)
}
}, heartbeat: { observer, device in
if let ruuviTag = device.ruuvi?.tag {
print(ruuviTag)
}
}, disconnected: { observer, result in
switch result {
case .just:
print("just disconnected")
case .already:
print("disconnected")
case .stillConnected:
print("still connected because of other callers")
case .failure(let error):
print(error.localizedDescription)
}
})
}
if let from = Calendar.current.date(byAdding: .minute, value: -5, to: Date()) {
ruuviTag.celisus(for: self, from: from) { (observer, result) in
switch result {
case .success(let values):
print(values)
case .failure(let error):
print(error.localizedDescription)
}
}
ruuviTag.humidity(for: self, from: from) { (observer, result) in
switch result {
case .success(let values):
print(values)
case .failure(let error):
print(error.localizedDescription)
}
}
ruuviTag.pressure(for: self, from: from) { (observer, result) in
switch result {
case .success(let values):
print(values)
case .failure(let error):
print(error.localizedDescription)
}
}
}
or use BTKit
if you know only the uuid
:
BTBackground.shared.services.ruuvi.nus.celisus(for: self, uuid: ruuviTag.uuid, from: from, result: { (observer, result) in
switch result {
case .success(let values):
print(values)
case .failure(let error):
print(error.localizedDescription)
}
})
BTBackground.shared.services.ruuvi.nus.humidity(for: self, uuid: ruuviTag.uuid, from: from, result: { (observer, result) in
switch result {
case .success(let values):
print(values)
case .failure(let error):
print(error.localizedDescription)
}
})
BTBackground.shared.services.ruuvi.nus.pressure(for: self, uuid: ruuviTag.uuid, from: from, result: { (observer, result) in
switch result {
case .success(let values):
print(values)
case .failure(let error):
print(error.localizedDescription)
}
})
if let from = Calendar.current.date(byAdding: .minute, value: -5, to: Date()) {
ruuviTag.log(for: self, from: from) { (observer, result) in
switch result {
case .success(let logs):
print(logs)
case .failure(let error):
print(error.localizedDescription)
}
}
}
Or use BTKit
if you know only the uuid
BTBackground.shared.services.ruuvi.nus.log(for: self, uuid: ruuviTag.uuid, from: from, result: { (observer, result) in
switch result {
case .success(let logs):
print(logs)
case .failure(let error):
print(error.localizedDescription)
}
})
ruuviTag.disconnect(for: self) { (observer, result) in
switch result {
case .just:
observer.isConnected = false
case .already:
observer.isConnected = false
case .stillConnected:
observer.isConnected = true
case .failure(let error):
observer.isConnected = false
print(error.localizedDescription)
}
}
or use BTKit
if you know only uuid
BTBackground.shared.disconnect(for: self, uuid: ruuviTag.uuid) { (observer, result) in
switch result {
case .just:
observer.isConnected = false
case .already:
observer.isConnected = false
case .stillConnected:
observer.isConnected = true
case .failure(let error):
observer.isConnected = false
print(error.localizedDescription)
}
}
We would love you for the contribution to BTKit, check the LICENSE
file for more info.
Rinat Enikeev – [email protected]
Distributed under the BSD license. See LICENSE
for more information.