ios - 使用 CoreBluetooth 在 iOS 上更改间隔时间读取信标 RSSI

标签 ios swift bluetooth core-bluetooth ibeacon

我尝试读取信标(估计值)的 RSSI 的频率超过每秒一次。我知道使用 Core Location nd 方法 locationManager (manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon] inRegion region: CLBeaconRegion) 这是不可能的。

我找到了使用 Core Bluetooth 的建议。我的实现如下所示:

class Bluetooth : NSObject, CBCentralManagerDelegate, CBPeripheralDelegate {
    var manager: CBCentralManager!
    lazy var peripherals = [NSUUID:CBPeripheral]()


    static let sharedInstance = Bluetooth()

    private override init() {
        super.init()
        manager = CBCentralManager()
        manager.delegate = self
    }

    func centralManagerDidUpdateState(central: CBCentralManager) {
        switch(central.state) {
            case .PoweredOn:
                print("powered on")
                manager.scanForPeripheralsWithServices(nil, options: [CBCentralManagerScanOptionAllowDuplicatesKey: NSNumber(bool: true)])
                break
            default:
                print("Unknown status")
        }
    }

    func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {   
        //fot testo only one becon
        if(peripheral.name == "EST" && peripherals.count == 0) {            
            manager.connectPeripheral(peripheral, options: nil)         
            peripherals[peripheral.identifier] = peripheral
            peripherals[peripheral.identifier]?.delegate = self     
            manager.stopScan()
        }
    }

    func centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral) {
        print("Connect peripheral \(peripheral.name) \(peripheral.identifier)")
        peripheral.delegate = self
        peripheral.readRSSI()
    }

    func peripheral(peripheral: CBPeripheral, didReadRSSI RSSI: NSNumber, error: NSError?) {
        let currentTime = CACurrentMediaTime();
        print("\(currentTime) \(RSSI)")
        peripheral.readRSSI()
    }
}

我在信标上将广告间隔设置为 100 毫秒,毕竟 外围设备(外围设备:CBPeripheral,didReadRSSI RSSI:NSNumber,错误:NSError?) 似乎每秒调用一次。

有什么方法可以每 100 毫秒读取一次 RSSI 吗?

还有一个额外的问题——在上面的代码中,方法 peripheral (peripheral: CBPeripheral, didReadRSSI RSSI) 只运行了大约 10 次,然后停止了。有没有可能是beacon“挂了”,我需要重新连接他?

我用 iOS9 测试 iPhone 6s plus。

最佳答案

两个提示:

  1. 配置您的信标,使其以 10 赫兹而非 1 赫兹的频率传输。每次检测到广告时,您只会收到对 CoreBluetooth 的回调。如果它每秒仅广播一次,那么您只能经常获得 RSSI 读数。

  2. 删除调用connectPeripheral 的代码。如果您连接到蓝牙 LE 设备,它会停止广播,您将无法再收到广播回调。

请注意,使用此技术您无法保证您读取的是您的信标与附近任何其他蓝牙设备的 RSSI 值,因为 CoreBluetooth 不允许您读取信标标识符。因此,只有当周围有单个蓝牙设备时,这才会可靠地工作。

关于ios - 使用 CoreBluetooth 在 iOS 上更改间隔时间读取信标 RSSI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34175377/

相关文章:

ios - HID 服务不可见

ios - 在 ios 中使用后台

ios - WatchKit:在用户点击后退(取消)左上角按钮后调用 popToRootController

swift - 从 Spritekit 场景切换回 Storyboard

ios - 核心蓝牙 : CBPeripheral disconnects every ~10 seconds

ios - TextLabel 在 ScrollView 中被截断

macos - 用 LET 定义的 Swift 常量有时会在读取时产生 EXC_BAD_ACCESS (EXC_I386_GPFLT) (OSX)

swift - 播放文档目录中的 m4a 文件

.net - Windows上的蓝牙开发

蓝牙配对但没有 COM 端口