Task: you need to check at startup whether the application is connected to Bluetooth, and also to the Internet.

If something of the voiced falls off during the work or the user is forced down, the application issues a message and stops the work algorithm. For example, it will not break into a server until it gains access to the network. Or (which is natural) will not look for devices, but if the bluetooth works, it will automatically continue.

Please tell me what I need to read and what methods should I use in my project?

So far I have learned only to check the connection. But how to detect a sudden disconnection or loss of the network, and wait in real time until it appears, until I know. How to turn to search - too.

  • Also interesting) - Flippy

1 answer 1

The code to check if there is an Internet connection is standard:

if (connectivityManager.getActiveNetworkInfo() != null && connectivityManager.getActiveNetworkInfo().isConnected() && (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || !powerManager.isDeviceIdleMode() || powerManager.isIgnoringBatteryOptimizations())) { // do something } 

But you need to understand that this check will show only "technical capability". There is no guarantee that this connection will allow you to contact a specific server, so if you need to check the connection to the server, and not to the “Internet in general,” then you need to think about this test and implement it separately.

With Bluetooth it is still worse, because via Bluetooth, devices of various functionalities are connected. It is generally impossible to connect to Bluetooth. You probably have enough of the list of "paired" devices. If not - specify the task.

 BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();