I am writing a Cocoa application which uses bluetooth. I am trying to connect to a bluetooth device but it fails.

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 );
}

And I get the device when searches, but openConnection method fails. And NSLog prints

Error = failed to connect. 4

Now what this error code indicates? I looked at IOKit.framework/IOReturn.h file and it shows many error codes

#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

And I wrote a function to identify what is error code 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");
    }
}

And it prints

No price for you

What does error code 4 imply? Also is there any easier way to identify error reason from OSStatus error codes?

有帮助吗?

解决方案

[IOBluetoothDevice openConnection] returns an IOReturn code (which is a I/O Kit specific error number) while your logError: method tests for OSStatus codes.
OSStatus is not the same as IOReturn.

Apple has a Technical Q&A that explains the macros to lookup I/O Kit errors. http://developer.apple.com/library/mac/#qa/qa1075/_index.html

In your case it seems to be a Mach error (that's probably the 0x4 hi bits of the error that show up as decimal 4 in your log line).

其他提示

I think the 4 response is actually kBluetoothHCIErrorPageTimeout. The only code I've found that uses this is this: https://www.ida.liu.se/~TDDD63/projects/2013/mindstorms/Installation/Mac/lightblue-0.4-master/src/mac/_bluetoothsockets.py

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top