1

我正在尝试自动连接到蓝牙设备。第一次,用户必须通过 IOBluetoothDeviceSelectorController 选择设备。然后将该地址存储在某处,之后,该设备应自动连接。

我的想法是使用pairedDevices遍历所有已知和配对的蓝牙设备,然后在设备地址等于前一个地址时中断。但由于某种原因,我无法让这种休息发生。

这是我的(有些缩短的)代码:

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;
    }
}

日志窗口输出如下:

2012-11-18 00:06:02.385 程序[5093:303] =00-80-25-15-29-20=

2012-11-18 00:06:04.772 程序[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!

4

1 回答 1

1

我通过用“isEqualToString”语句替换“==”语句来解决它......

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;
    }
}

无论如何,感谢您调查它!

于 2012-11-18T18:34:27.560 回答