我制作了一款使用蓝牙的应用
在oncreate()
方法中,它启用蓝牙并将设备设置为无限期可见
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if(!adapter.isEnabled()) {
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent, REQUEST_ENABLE_BT);
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 0);
startActivity(discoverableIntent);
}
}
在onDestroy()
中,它会禁用蓝牙
protected void onDestroy() {
// TODO Auto-generated method stub
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if(adapter.isEnabled()) {
adapter.disable();
}
super.onDestroy();
}
但是当我退出应用程序后再次手动启用蓝牙时,它会自动设置为无限期可发现。
在Undiscoverable
功能
onDestroy()
仅在Nexus 5上测试
答案 0 :(得分:0)
这将使发现能够持续1秒,并使您无法被无限期发现
Intent discoverableIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,1);
startActivity(discoverableIntent);
答案 1 :(得分:0)
我知道这是一个黑客攻击,但没有别的 Android文档中提供的方式。
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,1);
startActivity(discoverableIntent);
链接到Android文档,了解蓝牙设备中的可发现性:http://developer.android.com/guide/topics/connectivity/bluetooth.html#EnablingDiscoverability