我正在尝试编写一个 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?
最佳答案
OSX 使用较旧的样式,将其添加到您的类中以获得预期的行为。
func peripheralDidUpdateRSSI(peripheral: CBPeripheral!, error: NSError!) {
print("peripheralDidUpdateRSSI \(peripheral.name!) = \(peripheral.RSSI)")
}
关于ios - didReadRSSI 在 iOS 上调用但在 OS X 上不调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36226984/
我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search
我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下
如何在ruby中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL
我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我需要一些关于TDD概念的帮助。假设我有以下代码defexecute(command)casecommandwhen"c"create_new_characterwhen"i"display_inventoryendenddefcreate_new_character#dostufftocreatenewcharacterenddefdisplay_inventory#dostufftodisplayinventoryend现在我不确定要为什么编写单元测试。如果我为execute方法编写单元测试,那不是几乎涵盖了我对create_new_character和display_invent
在应用开发中,有时候我们需要获取系统的设备信息,用于数据上报和行为分析。那在鸿蒙系统中,我们应该怎么去获取设备的系统信息呢,比如说获取手机的系统版本号、手机的制造商、手机型号等数据。1、获取方式这里分为两种情况,一种是设备信息的获取,一种是系统信息的获取。1.1、获取设备信息获取设备信息,鸿蒙的SDK包为我们提供了DeviceInfo类,通过该类的一些静态方法,可以获取设备信息,DeviceInfo类的包路径为:ohos.system.DeviceInfo.具体的方法如下:ModifierandTypeMethodDescriptionstatic StringgetAbiList()Obt
1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里
我已经在mountainlion上成功安装了rbenv和rubybuild。运行rbenvinstall1.9.3-p392结束于:校验和不匹配:ruby-1.9.3-p392.tar.gz(文件已损坏)预期f689a7b61379f83cbbed3c7077d83859,得到1cfc2ff433dbe80f8ff1a9dba2fd5636它正在下载的文件看起来没问题,如果我使用curl手动下载文件,我会得到同样不正确的校验和。有没有人遇到过这个?他们是如何解决的? 最佳答案 tl:博士;使用浏览器从http://ftp.rub