我的客户端应用程序正在找我的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 Foundationimport CoreBluetoothclass 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上运行时的输出是:
iPhoneRSSI = -38RSSI Request - iPhoneRSSI = -44RSSI Request - iPhoneRSSI = -45
在OS X上运行时的输出是:
My iPhone 6RSSI Request - My iPhone 6RSSI Request - My iPhone 6
请注意,返回的外围设备名称在两个平台上是不同的.我不知道这是否重要.
如何在OS X上调用dIDReadRSSI?
解决方法 OSX使用较旧的样式,将其添加到您的类中以获得预期的行为.func peripheralDIDUpdateRSSI(peripheral: CBPeripheral!,error: NSError!) { print("peripheralDIDUpdateRSSI \(peripheral.name!) = \(peripheral.RSSI)")}总结
以上是内存溢出为你收集整理的didReadRSSI在iOS上调用,但在OS X上调用全部内容,希望文章能够帮你解决didReadRSSI在iOS上调用,但在OS X上调用所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)