didReadRSSI 调用了 iOS 但没有调用 OS X
didReadRSSI called on iOS but not on OS X
我正在尝试编写一个 OS X 客户端,它可以频繁更新蓝牙外围设备的 RSSI 值。
我的客户端应用程序正在查找我的 iPhone 设备,我正在 运行 上安装一个测试应用程序,该应用程序正在宣传蓝牙服务。客户端应用程序有一个计时器,每 3 秒执行一次 peripheral.readRSSI()
。
当 运行ning 在 OS X 上时,我无法成功调用 didReadRSSI,但是当我 运行 Swift 代码完全相同时 iOS 它工作正常。
在 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
运行 on OS X 时的输出为:
My iPhone 6
RSSI Request - My iPhone 6
RSSI Request - My iPhone 6
请注意,返回的外设名称在两个平台上是不同的。我不知道这是否重要。
如何在 OS X 上调用 didReadRSSI?
OSX 使用旧样式,将此添加到您的 class 以获得预期的行为。
func peripheralDidUpdateRSSI(peripheral: CBPeripheral!, error: NSError!) {
print("peripheralDidUpdateRSSI \(peripheral.name!) = \(peripheral.RSSI)")
}
我正在尝试编写一个 OS X 客户端,它可以频繁更新蓝牙外围设备的 RSSI 值。
我的客户端应用程序正在查找我的 iPhone 设备,我正在 运行 上安装一个测试应用程序,该应用程序正在宣传蓝牙服务。客户端应用程序有一个计时器,每 3 秒执行一次 peripheral.readRSSI()
。
当 运行ning 在 OS X 上时,我无法成功调用 didReadRSSI,但是当我 运行 Swift 代码完全相同时 iOS 它工作正常。
在 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
运行 on OS X 时的输出为:
My iPhone 6
RSSI Request - My iPhone 6
RSSI Request - My iPhone 6
请注意,返回的外设名称在两个平台上是不同的。我不知道这是否重要。
如何在 OS X 上调用 didReadRSSI?
OSX 使用旧样式,将此添加到您的 class 以获得预期的行为。
func peripheralDidUpdateRSSI(peripheral: CBPeripheral!, error: NSError!) {
print("peripheralDidUpdateRSSI \(peripheral.name!) = \(peripheral.RSSI)")
}