Pregunta

I'm having my application for controlling domestic devices using bluetooth in which my application will connect and needs to pair with my HC-05 Bluetooth module but the problem is when I coded something to trap other MAC addresses except 20:13:06:19:34:00 which is the HC-05 Bluetooth Module's MAC address, it traps all bluetooth devices including my HC-05 in which I specifically include its MAC address in my code.

So here is a part where I trap mac addresses:

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;

I tried already some other possibilities but still I'm a failure with this feature. Its my first time anyway to create an android app. Any help will be so much appreciated. Thank you.

¿Fue útil?

Solución

address == HC05

This is reference compare in java. You want String compare.

Try address.equals(HC05) instead. (Similarly, address != HC05 should be !address.equals(HC05) or in this specific case, you can replace the else if with just else)

For reference: Java comparison with == of two strings is false?

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top