Android Wear WifiManager - 必须在设置中手动打开 WiFi 才能获得结果
Android Wear WifiManager - Have to Manually Open WiFi in Settings to Get Results
我正在尝试获取 Moto 360 Sport (Android 6.0.1) 中当前可用的 WiFi 列表。我的代码有效,但我必须在“设置”中手动打开 WiFi,以便可以调用 BroadcastReceiver
。
我的 WiFi 接收器:
broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
List<ScanResult> scanResults = wifiManager.getScanResults();
sendWifiResult(scanResults);
unregister();
}
}
};
我已经向用户请求了一堆权限:
private static String[] permissions = {
"android.permission.BODY_SENSORS",
"android.permission.RECORD_AUDIO",
"android.permission.INTERNET",
"android.permission.ACCESS_NETWORK_STATE",
"android.permission.CHANGE_WIFI_STATE",
"android.permission.ACCESS_WIFI_STATE",
"android.permission.LOCATION_HARDWARE",
"android.permission.ACCESS_FINE_LOCATION",
"android.permission.ACCESS_COARSE_LOCATION",
"android.permission.ACCESS_LOCATION_EXTRA_COMMANDS",
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// some unrelated codes
if (!checkPermissions())
this.requestPermissions(permissions, 1);
else
startService(new Intent(this, DataSourceService.class));
}
private boolean checkPermissions() {
for (String permission : permissions) {
if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED)
return false;
}
return true;
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (checkSelfPermission("android.permission.BODY_SENSORS")
!= PackageManager.PERMISSION_GRANTED)
requestPermissions(permissions, 1);
else
startService(new Intent(this, DataSourceService.class));
}
我搜索了很多。但我发现的所有相关问题要么全有,要么全无 - 他们解决了所有问题,或者,他们无法获得广播或得到空列表。
就我而言,我确实得到了结果,但前提是我自己在系统设置中打开 WiFi。
这真的很奇怪,我找不到解决办法。我需要 Android Wears 中的任何特殊配置吗?
如有任何帮助,我们将不胜感激。谢谢
已解决。所以而不是使用
if (!wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(true);
}
...开火
wifiManager.setWifiEnabled(true);
直接。由于某种原因 isWifiEnabled()
总是 returns true
.
我正在尝试获取 Moto 360 Sport (Android 6.0.1) 中当前可用的 WiFi 列表。我的代码有效,但我必须在“设置”中手动打开 WiFi,以便可以调用 BroadcastReceiver
。
我的 WiFi 接收器:
broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
List<ScanResult> scanResults = wifiManager.getScanResults();
sendWifiResult(scanResults);
unregister();
}
}
};
我已经向用户请求了一堆权限:
private static String[] permissions = {
"android.permission.BODY_SENSORS",
"android.permission.RECORD_AUDIO",
"android.permission.INTERNET",
"android.permission.ACCESS_NETWORK_STATE",
"android.permission.CHANGE_WIFI_STATE",
"android.permission.ACCESS_WIFI_STATE",
"android.permission.LOCATION_HARDWARE",
"android.permission.ACCESS_FINE_LOCATION",
"android.permission.ACCESS_COARSE_LOCATION",
"android.permission.ACCESS_LOCATION_EXTRA_COMMANDS",
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// some unrelated codes
if (!checkPermissions())
this.requestPermissions(permissions, 1);
else
startService(new Intent(this, DataSourceService.class));
}
private boolean checkPermissions() {
for (String permission : permissions) {
if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED)
return false;
}
return true;
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (checkSelfPermission("android.permission.BODY_SENSORS")
!= PackageManager.PERMISSION_GRANTED)
requestPermissions(permissions, 1);
else
startService(new Intent(this, DataSourceService.class));
}
我搜索了很多。但我发现的所有相关问题要么全有,要么全无 - 他们解决了所有问题,或者,他们无法获得广播或得到空列表。
就我而言,我确实得到了结果,但前提是我自己在系统设置中打开 WiFi。
这真的很奇怪,我找不到解决办法。我需要 Android Wears 中的任何特殊配置吗?
如有任何帮助,我们将不胜感激。谢谢
已解决。所以而不是使用
if (!wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(true);
}
...开火
wifiManager.setWifiEnabled(true);
直接。由于某种原因 isWifiEnabled()
总是 returns true
.