Reputation: 21
I am trying to connect automatically to a bluetooth device. The first time, a user has to select the device through the IOBluetoothDeviceSelectorController. This address is then stored somewhere and afterwards, this device should connect automatically.
The idea I had was to step through all known and paired Bluetooth devices using pairedDevices and then break when the device address equals the previous address. But for some reason I cannot get this break to happen.
This is my (somewhat shortened) code:
IOBluetoothDevice *device;
NSArray *devices = [IOBluetoothDevice pairedDevices];
NSEnumerator *e = [devices objectEnumerator];
NSString *mytempstring, *mytempstring2 = @"AA";
while (device = [e nextObject])
{
NSLog(@"=%@=", [device addressString]);
mytempstring = [device addressString];
NSLog(@"=%@=", mytempstring);
if (mytempstring == @"00-80-25-15-29-20")
{
break;
}
if (mytempstring2 == @"AA")
{
break;
}
}
The log window output is as follows:
2012-11-18 00:06:02.385 Program[5093:303] =00-80-25-15-29-20=
2012-11-18 00:06:04.772 Program[5093:303] =00-80-25-15-29-20=
The outputs clearly match, but for some reason the if statement thinks otherwise and the break is never executed. I have added a second if statement for checking, and this one does execute as expected and initiates the second break...
Any thoughts why the address string compare does not work?
Thanks in advance!
Upvotes: 1
Views: 1332
Reputation: 21
I solved it by replacing the "==" satement with the "isEqualToString" statement...
IOBluetoothDevice *device;
NSArray *devices = [IOBluetoothDevice pairedDevices];
NSEnumerator *e = [devices objectEnumerator];
while (device = [e nextObject])
{
if ([[device addressString] isEqualToString:@"00-80-25-15-29-20"])
{
break;
}
}
Thanks for looking into it anyway!
Upvotes: 1