Objective c 从IOBluetoothHostController获取RSSIValue
我正试图编写一个简单的应用程序来收集RSSIValue并通过NSLog显示它,我的代码如下:Objective c 从IOBluetoothHostController获取RSSIValue,objective-c,macos,Objective C,Macos,我正试图编写一个简单的应用程序来收集RSSIValue并通过NSLog显示它,我的代码如下: #import <Foundation/Foundation.h> #import <Cocoa/Cocoa.h> #import <IOBluetooth/objc/IOBluetoothDeviceInquiry.h> #import <IOBluetooth/objc/IOBluetoothDevice.h> #import <IOBlue
#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
#import <IOBluetooth/objc/IOBluetoothDeviceInquiry.h>
#import <IOBluetooth/objc/IOBluetoothDevice.h>
#import <IOBluetooth/objc/IOBluetoothHostController.h>
#import <IOBluetooth/IOBluetoothUtilities.h>
@interface getRSSI: NSObject {}
-(void) readRSSIForDeviceComplete:(id)controller device:(IOBluetoothDevice*)device
info:(BluetoothHCIRSSIInfo*)info error:(IOReturn)error;
@end
@implementation getRSSI
- (void) readRSSIForDeviceComplete:(id)controller device:(IOBluetoothDevice*)device
info:(BluetoothHCIRSSIInfo*)info error:(IOReturn)error
{
if (error != kIOReturnSuccess) {
NSLog(@"readRSSIForDeviceComplete return error");
CFRunLoopStop(CFRunLoopGetCurrent());
}
if (info->handle == kBluetoothConnectionHandleNone) {
NSLog(@"readRSSIForDeviceComplete no handle");
CFRunLoopStop(CFRunLoopGetCurrent());
}
NSLog(@"RSSI = %i dBm ", info->RSSIValue);
[NSThread sleepUntilDate: [NSDate dateWithTimeIntervalSinceNow: 5]];
[device closeConnection];
[device openConnection];
[controller readRSSIForDevice:device];
}
@end
int main (int argc, const char * argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSLog(@"start");
IOBluetoothHostController *hci = [IOBluetoothHostController defaultController];
NSString *addrStr = @"xx:xx:xx:xx:xx:xx";
BluetoothDeviceAddress addr;
IOBluetoothNSStringToDeviceAddress(addrStr, &addr);
IOBluetoothDevice *device = [[IOBluetoothDevice alloc] init];
device = [IOBluetoothDevice withAddress:&addr];
[device retain];
[device openConnection];
getRSSI *rssi = [[getRSSI alloc] init];
[hci setDelegate:rssi];
[hci readRSSIForDevice:device];
CFRunLoopRun();
[hci release];
[rssi release];
[pool release];
return 0;
}
#导入
#进口
#进口
#进口
#进口
#进口
@接口getRSSI:NSObject{}
-(void)readRSSIForDeviceComplete:(id)控制器设备:(IOBluetoothDevice*)设备
信息:(BluetoothCirsiInfo*)信息错误:(IOReturn)错误;
@结束
@实现getRSSI
-(void)readRSSIForDeviceComplete:(id)控制器设备:(IOBluetoothDevice*)设备
信息:(BluetoothCirsiInfo*)信息错误:(IOReturn)错误
{
if(错误!=kioreturnsucture){
NSLog(@“readRSSIForDeviceComplete返回错误”);
CFRunLoopStop(CFRunLoopGetCurrent());
}
如果(信息->句柄==kBluetoothConnectionHandleNone){
NSLog(@“readRSSIForDeviceComplete无句柄”);
CFRunLoopStop(CFRunLoopGetCurrent());
}
NSLog(@“RSSI=%i dBm”,info->RSSIValue);
[NSThread sleepUntilDate:[NSDate Date WithTimeIntervalencesInnow:5];
[设备紧密连接];
[设备开放连接];
[控制器readRSSIForDevice:设备];
}
@结束
int main(int argc,const char*argv[]){
NSAutoreleasePool*池=[[NSAutoreleasePool alloc]init];
NSLog(@“开始”);
IOBluetoothHostController*hci=[IOBluetoothHostController defaultController];
NSString*addrStr=@“xx:xx:xx:xx:xx:xx”;
蓝牙设备地址地址;
IOBluetoothNSStringToDeviceAddress(addrStr和addr);
IOBluetoothDevice*设备=[[IOBluetoothDevice alloc]init];
设备=[IOBluetooth设备,地址:&addr];
[设备保留];
[设备开放连接];
getRSSI*rssi=[[getRSSI alloc]init];
[hci setDelegate:rssi];
[hci readRSSIForDevice:设备];
CFRunLoopRun();
[盐酸释放];
[rssi释放];
[池释放];
返回0;
}
我面临的问题是readrsiForDeviceComplete似乎工作得很好,信息传递了一个值。问题是RSSI值与我可以通过点击顶部蓝牙图标从OSX上查看的值有很大不同。对于我的应用程序来说,当菜单显示-64 dBm、-66、-70、-42等时,通常打印1,2、-1、-8等
我非常感谢您的指导。您从readRSSIForDeviceComplete获得的值是RSSI值。option/alt单击BlueTooth菜单时通过OS X显示的值为原始RSSI值 假设您有开发工具,您可以使用“Bluetooth Explorer”(/Developer/Applications/Bluetooth/Bluetooth Explorer)查看这两个值。我不知道这两个值之间有什么有意义的区别,但对我来说,它们似乎总是相差-60 所以你的RSSI值1,2,-1,-8对应于原始的RSSI值-59,-58,-61,-68