Detecting If Wifi or Bluetooth Is Turned on or Off by the User

Detecting if Wifi or Bluetooth is turned on or off by the user

For Bluetooth in iOS, you have CBPeripheralManager (in CoreBluetooth Framework). To check for bluetooth connection, you declare your class as delegate of CBPeripheralManager then create a local variable:

var myBTManager = CBPeripheralManager(delegate: self, queue: nil, options: nil)

then, your class must implement the callback to get noticed when your Bluetooth is enabled or disabled. The code below is extracted from my project which is for Beacon manager

//BT Manager
func peripheralManagerDidUpdateState(peripheral: CBPeripheralManager!) {
println(__FUNCTION__)
if peripheral.state == CBPeripheralManagerState.PoweredOn {
println("Broadcasting...")
//start broadcasting
myBTManager!.startAdvertising(_broadcastBeaconDict)
} else if peripheral.state == CBPeripheralManagerState.PoweredOff {
println("Stopped")
myBTManager!.stopAdvertising()
} else if peripheral.state == CBPeripheralManagerState.Unsupported {
println("Unsupported")
} else if peripheral.state == CBPeripheralManagerState.Unauthorized {
println("This option is not allowed by your application")
}
}

And for Wifi, take a look at this Github: https://github.com/ashleymills/Reachability.swift

Detect if wifi is turned on

Maybe this is what you are looking for:

DEAD LINK: http://www.enigmaticape.com/blog/determine-wifi-enabled-ios-one-weird-trick

Wayback Machine Archive: https://web.archive.org/web/20161114213529/http://www.enigmaticape.com/blog/determine-wifi-enabled-ios-one-weird-trick

There isn't a framework to what you want to do, but there is a trick that might work. If you list the available interfaces, there will be some interfaces that just appear when the wifi is turned on (and some just appear when you are connected to one. You can list the interfaces like this:

struct ifaddrs *interfaces;

if(!getifaddrs(&interfaces)) {
for( struct ifaddrs *interface = interfaces; interface; interface=interface->ifa_next) {
BOOL up = (interface->ifa_flags & IFF_UP) == IFF_UP;
if ( up ) {
NSLog(
@"Name : %s, sa_family : %d",
interface->ifa_name,
interface->ifa_addr->sa_family
);
}
}
}

Output with Wifi off:

Name : lo0, sa_family : 18
Name : lo0, sa_family : 30
Name : lo0, sa_family : 2
Name : lo0, sa_family : 30
Name : pdp_ip0, sa_family : 18
Name : pdp_ip0, sa_family : 2
Name : en0, sa_family : 18
Name : awdl0, sa_family : 18

Output with wifi on:

Name : lo0, sa_family : 18
Name : lo0, sa_family : 30
Name : lo0, sa_family : 2
Name : lo0, sa_family : 30
Name : pdp_ip0, sa_family : 18
Name : pdp_ip0, sa_family : 2
Name : en0, sa_family : 18
Name : awdl0, sa_family : 18
Name : awdl0, sa_family : 30

Output with wifi on and connected:

Name : lo0, sa_family : 18
Name : lo0, sa_family : 30
Name : lo0, sa_family : 2
Name : lo0, sa_family : 30
Name : pdp_ip0, sa_family : 18
Name : pdp_ip0, sa_family : 2
Name : en0, sa_family : 18
Name : en0, sa_family : 30
Name : en0, sa_family : 2
Name : awdl0, sa_family : 18
Name : awdl0, sa_family : 30

If you explore the ifaddrs structure you will find also the BSSID/SSID of the connected network.

Detect if Bluetooth scanning for location is turned on

After a lot of search, I finally found a way to know the state:

try {
ContentResolver resolver = getApplicationContext().getContentResolver();
int result = Settings.Global.getInt(resolver,"ble_scan_always_enabled");
if(result == 1) {
// Bluetooth scanning is on
} else {
// Bluetooth scanning is off
}
} catch(SettingNotFoundException e) {
// Settings doesn't exist, on Android < 6
}

You can also use "wifi_scan_always_enabled" to get wifi scanning state.
It doesn't require any permissions.

I will update this post if I find a way to redirect the user to the screen or to disable it.

The perfect function to check Android internet connectivity including bluetooth pan

I agree with Muzikant and thanks for the idea. I thought it will be better to post the implemented solution as it needs some additions.

This is how I solved it.

Created and AsyncTask to avoid network on main thread exception.

public class GetInternetStatus extends AsyncTask<Void,Void,Boolean> {

@Override
protected Boolean doInBackground(Void... params) {

return hasInternetAccess();
}

protected boolean hasInternetAccess()
{

try
{
URL url = new URL("http://www.google.com");

HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
urlc.setRequestProperty("User-Agent", "Android Application:1");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(1000 * 30);
urlc.connect();

// http://www.w3.org/Protocols/HTTP/HTRESP.html
if (urlc.getResponseCode() == 200 || urlc.getResponseCode() > 400)
{
// Requested site is available
return true;
}
}
catch (Exception ex)
{
// Error while trying to connect
ex.printStackTrace();
return false;
}
return false;
}

}

Now add the following function to activity and call it to check the connectivity.

    // Checking for all possible internet connections
public static boolean isConnectingToInternet() {
Boolean result = false;
try {
//get the result after executing AsyncTask
result = new GetInternetStatus().execute().get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
return result;
}

Checking Bluetooth Status

Unfortunately (stupidly?) there is no way to do it. Sorry. If you want to use the private SDK methods linked to in one of the comments to your question you certainly can, but Apple statically analyze code at the point of submission, so it's unlikely you'd be able to get it through the approval process.

I would strongly recommend you file a feature request to Apple asking for that ability in a future update. You used to be able to check for Bluetooth using GameKit, but that ability was removed when GameKit started supporting WiFi connections as well as Bluetooth.



Related Topics



Leave a reply



Submit