简体   繁体   中英

detect Bluetooth Discovery is on?

I want to detect whether Bluetooth discovery is on or no ? is it possible ?

Intent discoverableIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 1);
startActivity(discoverableIntent);

above code is about enabling /diabling blutooth disc state , but do not show bluetooth disco state

To find out if the bluetooth device is discoverable (in the inquiry scan state), use getScanMode() , as in the following:

BluetoothAdapter bAdapter = BluetoothAdapter.getDefaultAdapter();
if(bAdapter.getScanMode() == BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
    // device is discoverable & connectable
} else {
    // device is not discoverable & connectable
}

Documentation of getScanMode() :

int android.bluetooth.BluetoothAdapter.getScanMode()

Get the current Bluetooth scan mode of the local Bluetooth adapter.

The Bluetooth scan mode determines if the local adapter is connectable and/or discoverable from remote Bluetooth devices.

bluetoothAdapter.isDiscovering();

should do what you're looking for. You can get the default BluetoothAdapter object via:

BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

You can read about both of those methods in the Android BluetoothAdapter doc .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM