我正在尝试创建一个应用程序,并希望它检测到已经与手机连接的设备,如果它接近它,那是因为我想在设备距离手机太近时解锁手机!
我已经在谷歌搜索了rssi,但问题是它只有在设备与手机没有任何连接时才有效!
这是希望它尝试的代码
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");
}
}
};
}
但在我的情况下,设备已使用蓝牙与手机连接。
有什么建议吗? 谢谢