0

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()
    }
}
 }
alex
  • 118
  • 1
  • 6
  • [How can iOS detect bluetooth signal strength similar to Airpods?](https://stackoverflow.com/questions/45173454/how-can-ios-detect-bluetooth-signal-strength-similar-to-airpods) – MadProgrammer Dec 31 '22 at 00:12
  • @MadProgrammer I went through that and it's quite interesting. I'm not sure how this would apply in my case where all I wanna do is check if a given iPhone is (let's say) '-45dBm' to the Mac? What kind of if statement would I even write in this kind of case? – alex Dec 31 '22 at 00:30
  • You’re looking for the RSSI value – MadProgrammer Dec 31 '22 at 00:47
  • @MadProgrammer Correct. But I'm not how I would call that centralManager function in my case to return the RSSI value if that makes sense? – alex Dec 31 '22 at 01:09
  • You wouldn't `didDiscover` would be called on by the system, you'd need to keep track of it via some other means (store it some kind of registry/lookup) – MadProgrammer Dec 31 '22 at 01:11
  • Okay, could you give some example code to help explain this? I'm not sure how I would achieve this. For instance you can try copy pasting my code into some swift file and look at the log and see the RSSI value. I'm not sure how to proceed from there – alex Dec 31 '22 at 01:20
  • `device.rawRSSI()` or `device.RSSI()` once connected? See the methods/properties of https://developer.apple.com/documentation/iobluetooth/iobluetoothdevice – Larme Dec 31 '22 at 10:00

0 Answers0