Waiting for a specific device to appear
site_archiver@lists.apple.com Delivered-To: bluetooth-dev@lists.apple.com Hello, I do have theee questions : Jean-Baptiste LE STANG _______________________________________________ Do not post admin requests to the list. They will be ignored. Bluetooth-dev mailing list (Bluetooth-dev@lists.apple.com) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/bluetooth-dev/site_archiver%40lists.a... I'm trying to deal with a bluetooth phone and use the Serial Port service offered by my phone to get to the modem and make a phone call (for example), here is the code I'm using - (BOOL)lookForDeviceAndConnect { NSArray * devices = nil; IOBluetoothSDPServiceRecord * rec = nil; UInt8 rfcommChannelID = 0; IOReturn status = -1; unsigned int i = 0; BOOL bShouldPoll = TRUE; devices = [IOBluetoothDevice pairedDevices]; [IOBluetoothDevice registerForConnectNotifications:self selector:@selector(bluetoothDeviceDidConnect:device:)]; while(bShouldPoll){ if (devices != nil){ for (i = 0 ; i < [devices count] ; i++){ IOBluetoothDevice * device = nil; rec = nil; device = [devices objectAtIndex:i]; rec = [device getServiceRecordForUUID:[IOBluetoothSDPUUID uuid16:kBluetoothSDPUUID16ServiceClassSerialPort]]; if (rec != nil){ if ([[rec getServiceName] isEqualToString: @"Serial Port"]){ status = [rec getRFCOMMChannelID:&rfcommChannelID]; if( status == kIOReturnSuccess){ if( (status =[device openConnection]) == kIOReturnSuccess){ if((status = [device openRFCOMMChannelSync:&mRFCOMMChannel withChannelID:rfcommChannelID delegate:self]) == kIOReturnSuccess){ [self sendData:"ATDXXXXXXXX\r\n" length:strlen("ATDXXXXXXXX\r \n")]; [mRFCOMMChannel retain]; [device registerForDisconnectNotification:self selector:@selector(bluetoothDeviceDidDisconnect:device:)]; bShouldPoll = FALSE; } else { NSLog( @"Error: 0x%lx openRFCOMMChannelSync failed .\n", status ); } } else { NSLog( @"Error: 0x%lx openConnection failed .\n", status ); } } else { NSLog( @"Error: 0x%lx getRFCOMMChannelID failed .\n", status ); } } } } } [NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:5]]; } return TRUE; } When I'm stopping the bluetooth service of my phone I got the disconnect event and I do close the RFCOMMChannel, and I close the connection to the device. After detecting the device disconnection, I try to look for the device and open a new connection but each time I got an error on openConnection wich is the expected behaviour. Once I start again the Bluetooth service of my phone, my application never receives a notifcation that the phone is connected and openRFCOMMChannelSync is failing each time with a 0x10000003 error code. 0x10000003 stands for MACH_SEND_INVALID_DEST from what I've seen but I'm not able to say much more about this error code. I also tried to make this detection from another thread, but I've found that on Leopard I've the behaviour I'm expecting but on Tiger, IOBluetoothDevice pairedDevices] returns nil each time. a - what is the best / smart way for an application to wait for a bluetooth device to be in range of the computer and start interacting with the device ? b - what is the status of the IOBluetooth framework on TIger and Leopard regarding multithreading, is the framework thread safe? c - what could possibly go wrong in what I've done so that I'm not able to open and RFCOMMChannel ? Thanks in advance for any piece of advise or info you could give me so that I can solve my problems! This email sent to site_archiver@lists.apple.com
participants (1)
-
lists@lestang.org