how to detect if the device is near to the phone using bluetooth?

395 views Asked by At

i'm trying to create an application and wish it detects the device which is already connected with the phone if it is near to it that's because i want to unlock the phone when the device is too close with the phone!

I've already searched on Google on the rssi but the problem is that it works only if the device doesn't have any connection with the phone !

this is the code wish it have tryed

public class RSSIActivity extends Activity {

private BluetoothAdapter BTAdapter = BluetoothAdapter.getDefaultAdapter();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_rssi);
    registerReceiver(receiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));

    Button boton = (Button) findViewById(R.id.button1);
    boton.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {
            BTAdapter.startDiscovery();
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_rssi, menu);
    return true;
}

private final BroadcastReceiver receiver = new BroadcastReceiver(){
    @Override
    public void onReceive(Context context, Intent intent) {

        String action = intent.getAction();
        if(BluetoothDevice.ACTION_FOUND.equals(action)) {
            int rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE);
            String name = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
            TextView rssi_msg = (TextView) findViewById(R.id.textView1);
            rssi_msg.setText(rssi_msg.getText() + name + " => " + rssi + "dBm\n");
        }
    }
};

}

but in my case the device is already connected with the phone using Bluetooth.

any suggestions ? thank you

0

There are 0 answers