Waiting for a specific device to appear
Waiting for a specific device to appear
- Subject: Waiting for a specific device to appear
- From: "email@hidden" <email@hidden>
- Date: Thu, 5 Jun 2008 00:50:55 +0200
Hello,
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.
I do have theee questions :
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!
Jean-Baptiste LE STANG
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Bluetooth-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden