0
我想掃描可用的Wi-Fi網絡來獲取網絡列表並選擇特定的網絡連接。它的工作是棒棒糖,但不是棉花糖。根據this和this博客,幾乎沒有什麼變化,但我沒有得到如何實現這些變化。棉花糖wifi掃描和連接不工作
目前在棉花糖,應用程序只連接到由應用程序創建的那些網絡。當我嘗試連接到其他網絡時,應用程序崩潰。
這裏是我的活動代碼:
import android.Manifest;
import android.annotation.TargetApi;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class CustomWifiListAcivity extends AppCompatActivity {
private TextView mainText;
private ListView wifiDeviceList;
private WifiManager mainWifi;
private CustomWifiListAcivity.WifiReceiver receiverWifi;
private List<ScanResult> wifiList;
private StringBuilder sb;
private String ssid_selected=null;
private Context context=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wifi_list);
context=getApplicationContext();
setTitle("Choose Switchbox");
wifiDeviceList=(ListView)findViewById(R.id.listView);
mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if (!mainWifi.isWifiEnabled())
{
Toast.makeText(getApplicationContext(), "Turning WiFi ON...", Toast.LENGTH_LONG).show();
mainWifi.setWifiEnabled(true);
}
wifiDeviceList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
final String ssid = '"' + wifiList.get(position).SSID + '"';
final String mac = wifiList.get(position).BSSID;
String pass = "";
LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
View promptView = inflater.inflate(R.layout.dialog_wifi, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(CustomWifiListAcivity.this);
alertDialogBuilder.setView(promptView);
final EditText ssid_et = (EditText) promptView.findViewById(R.id.editText_ssid);
final EditText pass_et = (EditText) promptView.findViewById(R.id.editText_pswd);
ssid_et.setText(wifiList.get(position).SSID);
alertDialogBuilder
.setCancelable(true)
.setPositiveButton("Connect", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
String ssid = '"' + wifiList.get(position).SSID + '"';
String password = '"' + pass_et.getText().toString() + '"';
System.out.println("ssid: "+ssid);
System.out.println("password: "+password);
connectToHost(getApplicationContext(),ssid,password);
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
})
.create()
.show();
}
});
}
public void connectToHost(Context context,String host,String password){
mainWifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiConfiguration wc=new WifiConfiguration();
wc.SSID= host;
wc.preSharedKey = password;;
wc.status = WifiConfiguration.Status.ENABLED;
wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA); // For WPA
wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN); // For WPA2
wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
int netId=mainWifi.addNetwork(wc);
try {
mainWifi.enableNetwork(netId, true);
mainWifi.setWifiEnabled(true);
System.out.println("enabled network");
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
}
class WifiReceiver extends BroadcastReceiver {
public void onReceive(Context c, Intent intent) {
String action = intent.getAction();
if(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(action)){
sb = new StringBuilder();
wifiList = mainWifi.getScanResults();
sb.append("\n Number Of Wifi connections :"+wifiList.size()+"\n\n");
ArrayList<String> deviceList = new ArrayList<String>();
for(int i = 0; i < wifiList.size(); i++){
sb.append(new Integer(i+1).toString() + ". ");
sb.append((wifiList.get(i)).toString());
sb.append("\n\n");
deviceList.add(wifiList.get(i).SSID);
}
ArrayAdapter arrayAdapter = new ArrayAdapter(CustomWifiListAcivity.this,
android.R.layout.simple_list_item_1, deviceList.toArray());
wifiDeviceList.setAdapter(arrayAdapter);
}
}
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(receiverWifi);
}
@Override
protected void onPostResume() {
super.onPostResume();
receiverWifi = new CustomWifiListAcivity.WifiReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
registerReceiver(receiverWifi, intentFilter);
getWifi();
}
private void getWifi() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Toast.makeText(CustomWifiListAcivity.this, "version>=marshmallow", Toast.LENGTH_SHORT).show();
if (checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(CustomWifiListAcivity.this, "location turned off", Toast.LENGTH_SHORT).show();
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, 1);
}
else{
Toast.makeText(CustomWifiListAcivity.this, "location turned on", Toast.LENGTH_SHORT).show();
mainWifi.startScan();
}
}else {
Toast.makeText(CustomWifiListAcivity.this, "scanning", Toast.LENGTH_SHORT).show();
mainWifi.startScan();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == 1) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(CustomWifiListAcivity.this, "permission granted", Toast.LENGTH_SHORT).show(); mainWifi.startScan();
}else{
Toast.makeText(CustomWifiListAcivity.this, "permission not granted", Toast.LENGTH_SHORT).show();
return;
}
}
}
}
,這裏是來自清單文件的權限
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
在授予'mainWifi.startScan();'權限並檢查後,在onRequestPermissionsResult成功上添加此代碼。 – Tasneem
代碼 – Piyush
已經具有運行時權限,除了顯示敬酒之外,您在收到權限後不會執行任何操作。 –