【问题标题】:Trapping Fails For A Specific MAC Address in my Application在我的应用程序中捕获特定 MAC 地址失败
【发布时间】:2014-01-13 17:47:28
【问题描述】:

我正在使用蓝牙控制家用设备的应用程序,我的应用程序将在其中连接并需要与我的 HC-05 蓝牙模块配对,但问题是当我编写了一些代码以捕获除 20:13 之外的其他 MAC 地址时: 06:19:34:00 这是 HC-05 蓝牙模块的 MAC 地址,它会捕获包括我的 HC-05 在内的所有蓝牙设备,我在代码中特别包含了它的 MAC 地址。

所以这是我捕获mac地址的部分:

case REQUEST_DEVICE_CONNECT:
            String HC05 = "4C:0F:6E:0F:12:F4";
                // When DeviceList Activity returns with a device to connect
                if (resultCode == Activity.RESULT_OK) {
                    // Get the device MAC address
                    //String address = data.getExtras().getString(DeviceList.EXTRA_DEVICE_MAC_ADDRESS);
                    String address = new String(data.getExtras().getString(DeviceList.EXTRA_DEVICE_MAC_ADDRESS));
                    if (address == HC05)
                    {
                        // Get the BLuetoothDevice object
                        BluetoothDevice device = BTAdapter.getRemoteDevice(address);
                        // Attempt to connect to the device
                        commandService.connect(device);
                    }
                    else if (address != HC05)
                    {
                        Toast.makeText(this, "This device is not for JavaC101 Application. Please connect to HC-05 device.", Toast.LENGTH_LONG).show();
                    }
                }
                break;

我已经尝试了其他一些可能性,但我仍然无法使用此功能。无论如何,它是我第一次创建一个 android 应用程序。任何帮助将不胜感激。谢谢。

【问题讨论】:

    标签: java bluetooth comparison-operators


    【解决方案1】:
    address == HC05
    

    这是java中的参考比较。你想要字符串比较。

    改用address.equals(HC05)。 (同样,address != HC05 应该是 !address.equals(HC05) 或者在这种特定情况下,您可以将 else if 替换为 else

    供参考:Java comparison with == of two strings is false?

    【讨论】:

      猜你喜欢
      • 2019-05-30
      • 2016-03-09
      • 2017-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-27
      • 2015-01-23
      相关资源
      最近更新 更多