How to detect if wi-fi and/or bluetooth are connected

Asked

Viewed 392 times

1

Good afternoon! I wonder if anyone knows any open source for an application to check if the wi-fi or Bluetooth are connected.

  • I hope you’re not asking me to make an app for you (kkk)

  • 1

    repeating the question, because I don’t think it was clear... Good afternoon I wonder if anyone knows if you have an application with open source that checks if Wi-fi or Bluetooth is connected unnecessarily.

  • Hi Klever. I had even voted to close as out of scope, but I withdrew the vote because you already had a useful answer. Still I think it would be nice to edit the question to, instead of asking open source, request how to do what you want on Android. I know that in practice it does the same thing, but it potentially generates better answers and so the question becomes more useful to the whole community.

  • Once again, Good afternoon I wonder if anyone knows if you have an application with open source= Where in the text it says I asked for the source code: "I asked if anyone knows of an APPLICATION that has its source code available. Abs

1 answer

3

Based on my comment and believing that you have knowledge about what you request, I show you this open source that analyzes whether the wi-fi is connected.

private static final String DEBUG_TAG = "NetworkStatusExample";
ConnectivityManager connMgr = (ConnectivityManager) 
        getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI); 
boolean isWifiConn = networkInfo.isConnected();
networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
boolean isMobileConn = networkInfo.isConnected();
Log.d(DEBUG_TAG, "Wifi conectado: " + isWifiConn);
Log.d(DEBUG_TAG, "3G conectado: " + isMobileConn);

Source: http://developer.android.com/training/basics/network-ops/managing.html

Open source for Bluetooth

  • There is no way to recover a list of connected devices on application startup. The Bluetooth API does not allow you to consult, instead it allows you to see the changes.

  • A hoaky job would be to retrieve the list of all known paired devices / ...then try calling each one (to determine if you are connected).

  • Alternatively, you can have a background control with the Bluetooth API and write device states.

    public void onCreate() {        
    IntentFilter filter1 = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED);
    IntentFilter filter2 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED);
    IntentFilter filter3 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED);
    this.registerReceiver(mReceiver, filter1);
    this.registerReceiver(mReceiver, filter2);
    this.registerReceiver(mReceiver, filter3);
    }
    
    
    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
    
        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
           ... //Device found
        }
        else if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
           ... //Device is now connected
        }
        else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
           ... //Done searching
        }
        else if (BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED.equals(action)) {
           ... //Device is about to disconnect
        }
        else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
           ... //Device has disconnected
        }           
    }
    };
    

Don’t forget the permission: <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />


Source: https://stackoverflow.com/questions/4715865/how-to-programmatically-tell-if-a-bluetooth-device-is-connected-android-2-2


  • Thanks...I actually have an application, in which check some system security items, but wanted the device, example: "look you your Bluetooth is on or the wi-fi wants to leave on". Abs

  • Don’t forget the permission: <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Browser other questions tagged

You are not signed in. Login or sign up in order to post.