我正在尝试编写一个OS X客户端,可以频繁更新蓝牙外设的RSSI值.
我的客户端应用程序正在找我的iPhone设备,我正在运行一个广告蓝牙服务的测试应用程序.客户端应用程序有一个计时器,导致每3秒执行一次peripheral.readRSSI().
在OS X上运行时,我没有成功调用didReadRSSI,但是当我在iOS上运行完全相同的Swift代码时,它运行正常.
在iOS 9.3上 – 使用有效的RSSI值按预期调用didReadRSSI.
在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?
最佳答案 OSX使用较旧的样式,将其添加到您的类中以获得预期的行为.
func peripheralDidUpdateRSSI(peripheral: CBPeripheral!, error: NSError!) {
print("peripheralDidUpdateRSSI \(peripheral.name!) = \(peripheral.RSSI)")
}