I'm trying to detect if the iPhone is placed close enough to the Mac using BLE signal strength. I am using the following code to detect if the iPhone is in range(and it works), but I don't know how to detect if the iPhone is 'close enough'. Like -30db for instance. Does anyone know how I can achieve that? Any comments/discussions are welcome :)
import SwiftUI
import AVFoundation
import IOBluetooth
import CoreBluetooth
import Foundation
class BluetoothSearchDelegate : NSObject, IOBluetoothDeviceInquiryDelegate {
func deviceInquiryDeviceFound(_ sender: IOBluetoothDeviceInquiry, device: IOBluetoothDevice) {
print("Found Device \(device.name ?? "nil")")
}
func deviceInquiryStarted(_ sender: IOBluetoothDeviceInquiry) {
print("started")
}
func deviceInquiryComplete(_ sender: IOBluetoothDeviceInquiry, error: IOReturn, aborted: Bool) {
print("completed")
}
}
struct ContentView: View {
let delegate = BluetoothSearchDelegate()
let inquiry: IOBluetoothDeviceInquiry
init() {
self.inquiry = IOBluetoothDeviceInquiry(delegate: delegate)
}
var body: some View {
Button("Bluetooth") {
inquiry.start()
}
}
}