将蓝牙设置为不可发现

时间:2014-06-10 03:04:54

标签: android bluetooth discoverability

我制作了一款使用蓝牙的应用

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上测试

2 个答案:

答案 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

这可能是问题的重复:Disable Bluetooth discoverable mode on Android