Question

I am developing an application where I have to connect to Bluetooth device on Android 4.3.

I have connect to the BLE device , and the function in Main.java will receive the device address from DeviceControl.java.

I stored the device address in String[] address where I have connected.

And if the address receive from DeviceControl.java already in String[] address, it will not list on the listview.

when I see the Log, the the value of string receive from the DeviceControl.java and String array is the same. address[0] = 90:59:AF:0B:8A:AC address = 90:59:AF:0B:8A:AC

But it still show the address on the list view, and the if else function indicated that is deiiferent!

This is my code:

public class Main extends Activity {

private String[] address = {"0","1","2","3","4","5"};
private String mdeviceAddress;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        mdeviceAddress = infoIntent.getStringExtra(EXTRAS_DEVICE_ADDRESS);
        address[0] = mdeviceAddress;
        devicelist = (ListView) findViewById(R.id.devicelist);
        for(int i=0;i<3;i++) {
                }
                if(address[i] == mdeviceAddress){
                    Log.v(TAG, "address double :" + mdeviceAddress);

                    break;
                }else if(address[i].length() == 1) {
                    address[i] = mdeviceAddress;
                    name[i] = mdevicename;
                    rssi[i] = rssithreshold;

                    tempaddress = address[i];
                    Log.v(TAG, "show the address :" + address[0]);
                    break;
                } 
            }

}

It always show the message: show the address : 90:59:AF:0B:8A:AC

Why the address is the same I have seen, but it still show it is difference ????

Était-ce utile?

La solution

You should not be using the reference equality operator (==) to compare your String objects in Java. Instead, you should be using the equals() method:

if(address[0].equals(mdeviceAddress)) {
    ...
} else {
    ...
}

See: How do I compare strings in Java?

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top