检测WiFi扫描是否始终可用

时间:2015-05-13 18:15:45

标签: android wifi

我正在开发一款可以检测设备周围wifi网络的应用。我想检测“扫描始终可用”是否打开,但我找不到如何。我知道这是可能的,因为Google Now实际上是这样做的:

enter image description here

1 个答案:

答案 0 :(得分:8)

您可以使用isScanAlwaysAvailable() method in WifiManager

我刚刚在Android 4.4上对此进行了测试,但它确实有用。

要查询状态,并显示提示是否已禁用,请使用此代码(我将其放在 WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); if (Build.VERSION.SDK_INT >= 18 ) { if (wifiManager.isScanAlwaysAvailable()) { Toast.makeText(this, "Scan always available is on", Toast.LENGTH_SHORT).show(); } else{ startActivityForResult(new Intent(WifiManager.ACTION_REQUEST_SCAN_ALWAYS_AVAILABLE), 100); } } else{ //Not supported } 中):

@Override
protected void onActivityResult (int requestCode, int resultCode, Intent data){
    if (requestCode == 100) {
        if (resultCode == RESULT_OK) {
            Toast.makeText(this, "User enabled Scan always available", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "User did not enable Scan always available", Toast.LENGTH_SHORT).show();
        }
    }
}

然后,使用此代码捕获用户在被禁用的情况下在提示中做出的决定:

  <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

请注意,您还需要在AndroidManifest.xml中使用此权限:

function insertAt(ary, item, at) {
  ary.splice(at, 0, item);
}

function addColors(data, hue) {
  var dataWithColors = [];
  var highest = data[0].a;
  for (var i = 1; i < data.length; i++) {
    if (data[i].a > highest) {
      highest = data[i].a;
    }
  }
  var lowest = data[0].a;
  for (var i = 1; i < data.length; i++) {
    if (data[i].a < lowest) {
      lowest = data[i].a;
    }
  }
  var hueIndexes = {red: 0, green: 1, blue: 2};
  var range = highest - lowest;
  for (var i = 0; i < data.length; i++) {
    var value = data[i].a;
    var absInRange = value - lowest;
    var shade = Math.round(0xff - (0xff * absInRange / range));
    var subStr = shade.toString(16);
    if (subStr.length == 1) {
      subStr = "0" + subStr;
    }
    var color = ["00", "00"];
    insertAt(color, subStr, hueIndexes[hue]);
    var str = "#" + color.join("");
    dataWithColors.push({a: value, color: str});
      // (Alternative: eg https://lodash.com/docs#assign .)
  }
  return dataWithColors;
}

var sample = [{a:29},{a:17},{a:26},{a:19},{a:18},{a:40},{a:89},{a:14}];
var sampleWithColors = addColors(sample, "green");

console.dir(sampleWithColors);
/* [
  {a: 29, color: "#00cc00"},
  {a: 17, color: "#00f500"},
  {a: 26, color: "#00d600"},
  {a: 19, color: "#00ee00"},
  {a: 18, color: "#00f100"},
  {a: 40, color: "#00a700"},
  {a: 89, color: "#000000"},
  {a: 14, color: "#00ff00"}
] */