通过搜索SO,我找到了一种扫描区域中的wifi网络并通过TextView显示这些结果的方法。这是代码:
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.wifinetworks);
mainText = (TextView) findViewById(R.id.mainText);
mainText.setText("");
mWifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
if(mWifiManager.isWifiEnabled()==false)
{
mWifiManager.setWifiEnabled(true);
}
receiverWifi = new WifiReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
registerReceiver(receiverWifi, intentFilter);
mainText.setText("Starting Scan...");
mWifiManager.startScan();
}
public class WifiReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i(debugString, "I am in onRecieve");
List<ScanResult> wifiScanList = mWifiManager.getScanResults();
mainText.setText("");
for (int i = 0; i < wifiScanList.size(); i++) {
String info = (wifiScanList.get(i).toString());
mainText.append(info + "\n\n");
}
}
}
似乎永远不会调用日志语句,我的清单文件中有以下权限:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
这里可能出现什么问题?感谢任何建议。
编辑:我意识到我没有在清单中声明我的接收器,所以我制作了一个WifiReceiver类(更改在上面)并声明它是这样的: <receiver android:name="com.example.alexander.bluetoothcontroller.MainActivity$WifiReceiver" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.INPUT_METHOD_CHANGED" />
</intent-filter>
</receiver>
然而,除非我在声明中遗漏了某些内容,否则这仍然没有改变。我还添加了建议的额外权限,但仍未发生任何变化。我确实注意到这个在logcat中突显出来的callstack:
08-09 14:27:17.821 1579-1622 /? E / WifiHW:无法停止请求者 08-09 14:27:17.821 1579-1622 /? E / wifi:无法初始化供应商函数指针表 08-09 14:27:17.821 1579-1622 /? E / WifiNative-HAL:无法开始 08-09 14:27:17.821 1579-1622 /? E / WifiStateMachine:无法启动HAL 08-09 14:27:17.821 1579-1622 /? E / WifiHW:无法打开&#34; /system/etc/wifi/wpa_supplicant.conf":没有这样的文件或目录 08-09 14:27:17.821 1579-1622 /? E / WifiHW:无法启用Wi-Fi 08-09 14:27:17.821 1579-1622 /? E / WifiStateMachine:无法启动请求者! 08-09 14:27:17.822 1579-1579 /? E / WifiController:WifiControllerWifi开启失败
这可能是问题吗?
编辑2:
所以我尝试在运行时检查权限但仍然没有变化。
private boolean checkAndRequestPermissions() {
int wifi_state = ContextCompat.checkSelfPermission(this, permission.ACCESS_WIFI_STATE);
int change_wifi_state = ContextCompat.checkSelfPermission(this, permission.CHANGE_WIFI_STATE);
int loc = ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION);
int loc2 = ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION);
int internet = ContextCompat.checkSelfPermission(this, permission.INTERNET);
List<String> listPermissionsNeeded = new ArrayList<>();
if (wifi_state != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(permission.ACCESS_WIFI_STATE);
}
if (change_wifi_state != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(permission.CHANGE_WIFI_STATE);
}
if (loc2 != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(android.Manifest.permission.ACCESS_FINE_LOCATION);
}
if (loc != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(android.Manifest.permission.ACCESS_COARSE_LOCATION);
}
if (internet != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(permission.INTERNET);
}
if (!listPermissionsNeeded.isEmpty())
{
ActivityCompat.requestPermissions(this,listPermissionsNeeded.toArray
(new String[listPermissionsNeeded.size()]),REQUEST_ID_MULTIPLE_PERMISSIONS);
return false;
}
return true;
}
我在onCreate中调用此方法。我仍然收到控制器Wifi无法打开的调用堆栈消息,我认为这个问题越来越多。
答案 0 :(得分:0)
通过访问附近外部设备的硬件标识符 蓝牙和Wi-Fi扫描,您的应用程序现在必须具有 ACCESS_FINE_LOCATION或ACCESS_COARSE_LOCATION权限
尝试添加缺少的权限ACCESS_COARSE_LOCATION
答案 1 :(得分:0)
问题是我的错误,当我应该使用实际手机时,我使用Android模拟器来测试我的应用程序。