【问题标题】:didReadRSSI called on iOS but not on OS XdidReadRSSI 在 iOS 上调用,但在 OS X 上不调用
【发布时间】:2016-07-13 15:00:26
【问题描述】:

我正在尝试编写一个 OS X 客户端,该客户端可以频繁更新蓝牙外围设备的 RSSI 值。

我的客户端应用程序正在查找我的 iPhone 设备,在该设备上我正在运行一个正在宣传蓝牙服务的测试应用程序。客户端应用程序有一个计时器,它会导致每 3 秒执行一次 peripheral.readRSSI()

在 OS X 上运行时调用 didReadRSSI 没有成功,但是当我在 iOS 上运行完全相同的 Swift 代码时,它工作正常。

在 iOS 9.3 上 - didReadRSSI 使用有效的 RSSI 值按预期调用。

在 OS X 10.11.4 - didReadRSSI 永远不会被调用。

我在 iOS 和 OS X 上运行以下完全相同的 Swift 代码:

import Foundation
import CoreBluetooth

class SampleClient:NSObject, CBCentralManagerDelegate, CBPeripheralDelegate {

var manager: CBCentralManager!
var peripheral: CBPeripheral!
var characteristic: CBCharacteristic!
var readRSSITimer: NSTimer!

override init() {
    super.init()
    self.manager = CBCentralManager(delegate: self, queue: nil)
}

func centralManagerDidUpdateState(central: CBCentralManager) {
    if self.manager.state == .PoweredOn {
        self.manager.scanForPeripheralsWithServices(nil, options: nil)
    }
}

func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {
    if let name = peripheral.name {
        print(name)
        self.peripheral = peripheral
        self.manager.connectPeripheral(self.peripheral, options:
            [CBConnectPeripheralOptionNotifyOnDisconnectionKey : true])
    }
}

func centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral) {
    peripheral.readRSSI()
    self.startReadRSSI()
    peripheral.delegate = self
    peripheral.discoverServices(nil)
}

func centralManager(central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: NSError?) {
    self.stopReadRSSI()
    if self.peripheral != nil {
        self.peripheral.delegate = nil
        self.peripheral = nil
    }
}

func centralManager(central: CBCentralManager, didFailToConnectPeripheral aPeripheral: CBPeripheral, error: NSError?) {
    if self.peripheral != nil {
        self.peripheral.delegate = nil
        self.peripheral = nil
    }
}

func disconnect() {
    if self.characteristic != nil {
        self.peripheral.setNotifyValue(false, forCharacteristic: self.characteristic)
    }
    if self.peripheral != nil {
        self.manager.cancelPeripheralConnection(self.peripheral)
    }
}

func stopScan() {
    self.manager.stopScan()
}

func peripheral(peripheral: CBPeripheral, didDiscoverServices error: NSError?) {
    for service: CBService in peripheral.services! {
        peripheral.discoverCharacteristics(nil, forService: service)
    }
}

func peripheral(peripheral: CBPeripheral, didReadRSSI RSSI: NSNumber, error: NSError?) {
    print("RSSI = \(RSSI)")
}

func readRSSI() {
    if (self.peripheral != nil) {
        self.peripheral.delegate = self
        print("RSSI Request - \(peripheral.name!)")
        self.peripheral.readRSSI()
    } else {
        print("peripheral = nil")
    }
}

func startReadRSSI() {
    if self.readRSSITimer == nil {
        self.readRSSITimer = NSTimer.scheduledTimerWithTimeInterval(3.0, target: self, selector: #selector(SampleClient.readRSSI), userInfo: nil, repeats: true)
    }
}

func stopReadRSSI() {
    if (self.readRSSITimer != nil) {
        self.readRSSITimer.invalidate()
        self.readRSSITimer = nil
    }
}

}

在 iOS 上运行时的输出是:

iPhone
RSSI = -38
RSSI Request - iPhone
RSSI = -44
RSSI Request - iPhone
RSSI = -45

在 OS X 上运行时的输出是:

My iPhone 6
RSSI Request - My iPhone 6
RSSI Request - My iPhone 6

请注意,返回的外设名称在两个平台上是不同的。我不知道这是否重要。

如何在 OS X 上调用 didReadRSSI?

【问题讨论】:

    标签: ios swift macos core-bluetooth


    【解决方案1】:

    OSX 使用旧样式,将其添加到您的类中以获得预期的行为。

    func peripheralDidUpdateRSSI(peripheral: CBPeripheral!, error: NSError!) {
        print("peripheralDidUpdateRSSI \(peripheral.name!) = \(peripheral.RSSI)")
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-27
      • 2015-09-10
      • 1970-01-01
      • 2015-12-31
      • 2011-01-13
      • 2014-04-22
      相关资源
      最近更新 更多