I need to control my APP's network go through Mobile/WiFi
So I follow this post 40216514
But when I tried to turn on WiFi. And make wifi be my major network.
Then I tried to do some communication. Very high possiblity(90%) the APP crash.
How can I avoid it?
Thank you!
Here is my code.
public void SomeFunction()
{
await ForceWifiOverCellular();
try
{
app_connection_request app_connection_request = new app_connection_request
{
url = "app_con_req",
request = mode,
};
Settings.Log_Item_Scan += "91/n";
// Serialize our concrete class into a JSON String
var stringPayload = await Task.Run(() => JsonConvert.SerializeObject(app_connection_request));
// Wrap our JSON inside a StringContent which then can be used by the HttpClient class
var httpContent = new StringContent(stringPayload, Encoding.UTF8, "application/json");
var myHttpClient = new HttpClient()
{
Timeout = TimeSpan.FromSeconds(15)
};
Settings.Log_Item_Scan += "92/n"; //Crash at here
var resp = await myHttpClient.PostAsync(URL, httpContent);
Settings.Log_Item_Scan += "93/n";
if (resp.IsSuccessStatusCode)
{
Settings.Log_Item_Scan += "94/n";
var json = await resp.Content.ReadAsStringAsync();
Settings.Log_Item_Scan += "95/n";
var result = JsonConvert.DeserializeObject<RS485_Info>(resp.Content.ReadAsStringAsync().Result);
{
return 0;
}
}
return -1;
}
catch (Exception e)
{
Settings.Log_Item_Scan += "96/n";
return -1;
}
}
public async static Task<int> ForceWifiOverCellular()
{
await WiFi_Control_Center.Set_WiFI_Enable(true); //This code is to turn on wifi
await Task.Delay(TimeSpan.FromSeconds(2));
ConnectivityManager connection_manager = (ConnectivityManager)_context.GetSystemService(Context.ConnectivityService);
NetworkRequest.Builder request = new NetworkRequest.Builder();
request.AddTransportType(TransportType.Wifi);
var callback = new ConnectivityManager.NetworkCallback();
connection_manager.RegisterNetworkCallback(request.Build(), new CustomNetworkAvailableCallBack());
await Task.Delay(TimeSpan.FromSeconds(2));
return 0;
}
public static async Task Set_WiFI_Enable(bool enabled)
{
try
{
WifiManager Temp_WiFi = (WifiManager)Android.App.Application.Context.GetSystemService(Context.WifiService);
Temp_WiFi.SetWifiEnabled(enabled);
await Task.Delay(1000);
}
catch
{
}
}
Here is some log
06-12 20:47:25.355 D/Mono (14255): Assembly System.Reflection.Primitives[0xad468660] added to domain RootDomain, ref_count=1 06-12 20:47:25.356 D/Mono (14255): AOT: image 'System.Reflection.Primitives.dll.so' not found: dlopen failed: library "/data/app/com.mydeltasolar_1_0_1.app-uRHsm2P_-FnLPQC6AXJTDA==/lib/arm/libaot-System.Reflection.Primitives.dll.so" not found 06-12 20:47:25.356 D/Mono (14255): AOT: image '/Users/builder/jenkins/workspace/xamarin-android-d15-9/xamarin-android/external/mono/sdks/out/android-armeabi-v7a-release/lib/mono/aot-cache/arm/System.Reflection.Primitives.dll.so' not found: dlopen failed: library "/data/app/com.mydeltasolar_1_0_1.app-uRHsm2P_-FnLPQC6AXJTDA==/lib/arm/libaot-System.Reflection.Primitives.dll.so" not found 06-12 20:47:25.356 D/Mono (14255): Config attempting to parse: 'System.Reflection.Primitives.dll.config'. 06-12 20:47:25.356 D/Mono (14255): Config attempting to parse: '/Users/builder/jenkins/workspace/xamarin-android-d15-9/xamarin-android/external/mono/sdks/out/android-armeabi-v7a-release/etc/mono/assemblies/System.Reflection.Primitives/System.Reflection.Primitives.config'. 06-12 20:47:25.356 D/Mono (14255): Assembly Ref addref Newtonsoft.Json[0xed1995c0] -> System.Reflection.Primitives[0xad468660]: 2 06-12 20:47:25.357 D/Mono (14255): Assembly Ref addref System.Reflection.Primitives[0xad468660] -> mscorlib[0xed1991a0]: 91 Loaded assembly: System.Reflection.Primitives.dll [External] Xamarin.Android returned no custom HttpClientHandler. Defaulting to System.Net.Http.HttpClientHandler 06-12 20:47:25.502 I/mono-stdout(14255): Xamarin.Android returned no custom HttpClientHandler. Defaulting to System.Net.Http.HttpClientHandler 06-12 20:47:25.519 F/ (14255): mono_w32socket_convert_error: no translation into winsock error for (64) "Machine is not on the network" 06-12 20:47:25.534 D/ConnectivityManager(14255): setProcessDefaultNetwork CallingUid : 10532, CallingPid : 14255 06-12 20:47:25.534 D/ConnectivityManager_URSP(14255): hong blacklisted - false 06-12 20:47:26.090 F/libc (14255): Fatal signal 6 (SIGABRT), code -6 (SI_TKILL) in tid 14362 (Thread Pool Wor), pid 14255 (solar_1_0_1.app) 06-12 20:47:27.252 D/InputMethodManager(14255): HSIFW - flag : 0 Pid : 14255