2013-06-21 86 views
0

我在写一个使用蓝牙的Cocoa应用程序。我正尝试连接到蓝牙设备,但失败。IOBluetoothDevice openConnection失败 - 识别错误代码

IOBluetoothDevice *btDevice; 

// I do search and find the device 
btDevice = ;//device found 
//btDevice is not nil 

IOReturn status   = [btDevice openConnection]; 
if (status != kIOReturnSuccess) { 
    NSLog(@"Error - failed to connect. %d", status); 
} 

我什么时候搜索获得设备,但openConnection方法失败。和NSLog打印

错误=无法连接。 4

现在这个错误代码表示什么? 我看着IOKit.framework/IOReturn.h文件,它显示了许多错误代码

#define kIOReturnError   iokit_common_err(0x2bc) // general error 
#define kIOReturnNoMemory  iokit_common_err(0x2bd) // can't allocate memory 
#define kIOReturnNoResources  iokit_common_err(0x2be) // resource shortage 
#define kIOReturnIPCError  iokit_common_err(0x2bf) // error during IPC 
#define kIOReturnNoDevice  iokit_common_err(0x2c0) // no such device 
....... 
//And many more 

我写一个函数来确定哪些是错误代码4

- (void)logError:(OSStatus)status{ 
    if (status == kIOReturnError) { 
     NSLog(@"kIOReturnError"); 
    }else if(status == kIOReturnNoMemory){ 
     NSLog(@"kIOReturnNoMemory"); 
    }else if(status == kIOReturnNoResources){ 
     NSLog(@"kIOReturnNoResources"); 
    }else if(status == kIOReturnIPCError){ 
     NSLog(@"kIOReturnIPCError"); 
    }else if(status == kIOReturnNoDevice){ 
    ...... 
    ...... 
    }else{ 
     NSLog(@"No price for you"); 
    } 
} 

而且它打印

没有价格你

什么是e错误代码4暗示?还有什么更容易的方法来识别来自OSStatus错误代码的错误原因吗?

回答

1

[IOBluetoothDevice openConnection]返回IOReturn代码(这是I/O Kit特定的错误编号),而您的logError:方法测试OSStatus代码。
OSStatus与IOReturn不同。

Apple有一个技术问题Q & A解释了查找I/O Kit错误的宏。 http://developer.apple.com/library/mac/#qa/qa1075/_index.html

在你的情况下,它似乎是一个错误马赫(这可能是出现在你的日志行小数4的错误为0x4喜位)。

+0

你是对的OSStatus部分,即使我正在检查函数中的不同IOReturn错误代码。我弄错了方法原型。我正在检查你的链接。 – Krishnabhadra

相关问题