在使用wifi管理器连接Esp8266时,我突然收到了一个连接错误。输出已经说明了问题。我能够扫描并找到正确的SSID,但是在连接时要么拒绝连接,要么就是无法连接。查看文档后发现,Wifi Manager即将退出历史舞台,并且应该使用WifiNetworkSpecifier?但是这只适用于使用API29及以上版本的手机。我需要在所有手机上都能正常工作。
我已经从我的电脑连接到了Esp8266,并且得到了回应-没有Esp8266的连接问题。
我已经从我的电脑连接到了Esp8266,并且得到了回应-没有Esp8266的连接问题。
public class ChooseDevice extends AppCompatActivity {
private WifiManager wifiManager;
private ListView listView;
private ArrayList<String> arrayList = new ArrayList<>();
private ArrayAdapter adapter;
TextView TV_noDevicesFound;
BroadcastReceiver wifiReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
List<ScanResult> results = wifiManager.getScanResults();
unregisterReceiver(this);
for (ScanResult scanResult : results) {
Log.d("Here!!", scanResult.SSID);
if (scanResult.SSID.startsWith("Cessabit")) {
arrayList.add(scanResult.SSID);
adapter.notifyDataSetChanged();
}
}
if (arrayList.size()==0){
TV_noDevicesFound.setVisibility(View.VISIBLE);
}else{
TV_noDevicesFound.setVisibility(View.INVISIBLE);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_choose_device);
TV_noDevicesFound = findViewById(R.id.TV_noDevicesFound);
listView = findViewById(R.id.deviceList);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String Device_SSID = listView.getItemAtPosition(position).toString();
connectToDevice(Device_SSID);
Intent intent = new Intent(ChooseDevice.this, ChooseWifi.class);
startActivity(intent);
}
});
wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
if (!wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(true);
}
adapter = new ArrayAdapter<>(this, R.layout.layout_list_item, R.id.DeviceTxtView, arrayList);
listView.setAdapter(adapter);
}
private void connectToDevice(String SSID) {
WifiInfo connection;
Log.d("Connecting To SSID: ", SSID);
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + SSID + "\"";
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
int netID = wifiManager.addNetwork(conf);
Log.d("netID", ""+netID);
wifiManager.disconnect();
wifiManager.enableNetwork(netID, true);
wifiManager.reconnect();
connection = wifiManager.getConnectionInfo();
String ConnectedSSID = connection.getSSID();
Log.d("Connected To SSID : ", ConnectedSSID);
}
@Override
protected void onStop(){
super.onStop();
try{
unregisterReceiver(wifiReceiver);
}catch(final Exception exception){
Log.d("Receiver try catch","cannot unregister receiver");
}
}
@Override
protected void onStart(){
super.onStart();
arrayList.clear();
registerReceiver(wifiReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
wifiManager.startScan();
Toast.makeText(this, "Scanning for Devices ..", Toast.LENGTH_SHORT).show();
}
}
D/Connecting To SSID:: Cessabit-1111
I/zygote: Do partial code cache collection, code=107KB, data=80KB
I/zygote: After code cache collection, code=107KB, data=80KB
Increasing code cache capacity to 512KB
D/netID: -1
V/NativeCrypto: Read error: ssl=0xec4b4768: I/O error during system call, Software caused connection abort
V/NativeCrypto: Write error: ssl=0xec4b4768: I/O error during system call, Broken pipe
V/NativeCrypto: SSL shutdown failed: ssl=0xec4b4768: I/O error during system call, Success
D/Connected To SSID :: <unknown ssid>