Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jun 5, 2013, 02:46 AM   #1
Tweetdezweet
macrumors newbie
 
Join Date: May 2013
Bluetooth connection failing (suspected race condition)

I'm trying to get my app to remember the last used bluetooth and use that as a default.
With the code below I have "rfcommChannelOpenComplete" returning an error (error code 11).
However it doesntt always return an error which makes me think I got some sort of race condition going on.

Any help is appreciated, if more info is needed plz dont hesitate to ask me

Called in "applicationDidFinishLaunching"
Code:
[self setUpBluetoothConnection];
Code:
- (void) setUpBluetoothConnection {
    _btDevice = nil;
    self.connectionOpened = false;
    
    CFStringRef keyForMacAddresOfLastUsedCube = CFSTR("last_used_device");
    CFStringRef macAddressOfLastUsedCube;
    
    macAddressOfLastUsedCube = (CFStringRef)CFPreferencesCopyAppValue(keyForMacAddresOfLastUsedCube, kCFPreferencesCurrentApplication);
    if(macAddressOfLastUsedCube) {
        _btDevice = [IOBluetoothDevice deviceWithAddressString:(__bridge NSString *)(macAddressOfLastUsedCube)];
        if(_btDevice != nil) {
            [self connectToCube];
        } else {
            [self showAddBluetoothDeviceWindow];
        }    
    } else {
        [self showAddBluetoothDeviceWindow];
    }
}
Code:
- (void) connectToCube {
    if(_btDevice != nil) { //so I have something to query.
        NSLog(@"I gottcha!");
        IOReturn ret = [_btDevice performSDPQuery:self]; //some delegate methoed called after SDP is finished.
        if (ret != kIOReturnSuccess) {
            NSLog(@"something went wrong");
            return;
        }
        
        BluetoothRFCOMMChannelID rfCommChan;
        if([self findRfCommChannel:&rfCommChan] != kIOReturnSuccess) //check If the device have RFCOMM channel.
            return;
        
        NSLog(@"Found rfcomm channel on device.. %d",rfCommChan);
        self.connectionOpened = [self openConnection:&rfCommChan];
    } else {
        NSLog(@"Not Found!");
    }
}
Code:
-(IOReturn) findRfCommChannel:(BluetoothRFCOMMChannelID *) rfChan{
    if(self.btDevice == nil)
        return kIOReturnNotFound;
    IOReturn ret;

    NSArray* services = [_btDevice services];
    BluetoothRFCOMMChannelID newChan;
    for (IOBluetoothSDPServiceRecord* service in services) {
        NSLog(@"Service: %@", [service getServiceName]);
        ret = [service getRFCOMMChannelID:&newChan];
        if (ret == kIOReturnSuccess) {
            *rfChan = newChan;
            NSLog(@"ChannelID FOUND %d %d", newChan, *rfChan);
            return kIOReturnSuccess;
        }
    }
    
    return kIOReturnNotFound;
}
Code:
-(BOOL) openConnection:(BluetoothRFCOMMChannelID *) chanId{
    IOBluetoothRFCOMMChannel *channel;
    if ([_btDevice openRFCOMMChannelAsync:&channel withChannelID:*chanId delegate:self] != kIOReturnSuccess) { // after connection it is established.. the delegates methoed are triggered.
        NSLog(@"Couldn't open channel");
        return NO;
    }
    [channel setSerialParameters: 9600 dataBits: 8 parity: kBluetoothRFCOMMParityTypeNoParity stopBits: 2];
    [channel closeChannel];
    return YES;
}
Code:
//delegate RFComm channel
- (void)rfcommChannelOpenComplete:(IOBluetoothRFCOMMChannel*)rfcommChannel
                           status:(IOReturn)error {
    if (error != kIOReturnSuccess) {
        NSLog(@"Failed to open channel, error %d", error);

        return;
    }
    
    NSLog(@"channel complete");
    
    NSLog(@"Channel MTU: %hu", [rfcommChannel getMTU]);
    
    self.rfcommChannel = rfcommChannel;
}
Tweetdezweet is offline   0 Reply With Quote
Old Jun 5, 2013, 04:26 AM   #2
Tweetdezweet
Thread Starter
macrumors newbie
 
Join Date: May 2013
I did some more digging and the response code 11 (0x0B) seems to indicate the device is waiting for the pin code, not sure though
Tweetdezweet is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Carrier: Verizon iPad Air Failing Cellular Connection Zerotolerance iPad 3 Dec 31, 2013 10:53 AM
Mavericks failing local network connection? williemyers OS X Mavericks (10.9) 1 Dec 8, 2013 12:08 PM
iPad: Bluetooth connection problems redliner56 iPad Accessories 1 Nov 17, 2013 12:52 PM
bluetooth connection fanspeed OS X Mavericks (10.9) 0 Oct 31, 2013 04:18 PM
iPad: Help with Bluetooth connection please TooLowToZero iPad 2 Mar 20, 2013 10:04 AM

Forum Jump

All times are GMT -5. The time now is 04:24 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC