Re: Waiting for a specific device to appear
Re: Waiting for a specific device to appear
- Subject: Re: Waiting for a specific device to appear
- From: Zach Rosen <email@hidden>
- Date: Thu, 5 Jun 2008 16:23:25 -0700
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 ?
The long answer: device 'range detection' is generally discouraged
due to the interference it can cause. When two devices want to
connect, the master device must scan the 2.4GHz range in order to
synchronize frequencies with the slave device. Bluetooth connections
use spectrum hopping to avoid interference with other 2.4GHz devices,
but this initial scan for setting up the connection is like firing a
shotgun into the air. A single shot may not do much, but do it
enough and you're going to start hitting things. Expect decreased
wireless reception as well as dropped WiFi and Bluetooth connections,
interference with cordless phones, etc.
The short answer: I've found name requests to cause the 'least'
amount of interference. Don't initiate them AT ALL when a device is
pairing (it will mess up device discovery). Don't initiate more than
one at a time. Don't initiate more than one every 60 seconds.
b - what is the status of the IOBluetooth framework on TIger and
Leopard regarding multithreading, is the framework thread safe?
No. Not thread safe. It's best to keep everything on the main
thread. It might be possible to use a single thread instead; this
has been discussed in the past, but i can't remember what the
conclusion was. You might want to search the list.
c - what could possibly go wrong in what I've done so that I'm not
able to open and RFCOMMChannel ?
Try moving everything to the main thread.
--------------------------------------------------
Zach Rosen
Founder & Lead Engineer
mirasoftware.com
--------------------------------------------------
On Jun 4, 2008, at 3:50 PM, email@hidden wrote:
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:
40mirasoftware.com
This email sent to email@hidden
_______________________________________________
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