在使用wifi管理器连接到Esp8266时,我突然开始收到连接错误。输出说明了一切。我可以扫描并找到正确的SSID,但是当连接到它时,要么连接被拒绝,要么就无法连接。查看说明文件,似乎wifi管理器即将退出?和WifiNetworkSpecifier应该使用什么?但这仅适用于使用API29及更高版本的手机。我需要这个才能在所有手机上工作
我已从计算机连接到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>
答案 0 :(得分:0)
由于API发生了变化,您必须针对Android 10及更高版本进行单独操作。
使用WifiNetworkSpecifier发送您的请求。使用onAvailable()中提供的网络对象。
private void connectToDevice(String SSID) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
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);
} else {
WifiNetworkSpecifier.Builder builder = new WifiNetworkSpecifier.Builder();
builder.setSsid(SSID);
WifiNetworkSpecifier wifiNetworkSpecifier = builder.build();
NetworkRequest.Builder networkRequestBuilder = new NetworkRequest.Builder();
networkRequestBuilder1.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
networkRequestBuilder1.setNetworkSpecifier(wifiNetworkSpecifier);
NetworkRequest networkRequest = networkRequestBuilder.build();
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
cm.requestNetwork(networkRequest, networkCallback);
networkCallback = new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(@NonNull Network network) {
//Use this network object to Send request.
//eg - Using OkHttp library to create a service request
super.onAvailable(network);
}
};
}
}
使用Wifi接入点完成后,请
connectivityManager.unregisterNetworkCallback(networkCallback);