使用 Xamarin Forms 在 Android 10+ 中启动热点
Start hotspot in Android 10+ using Xamarin Forms
希望您能提供帮助,我已尽一切努力使它正常工作。我试图将显然在 android studio 中工作的其他代码改编为在 Xamarin 中工作。我已经到了我无法通过的地步。这是我所拥有的...
理想情况下,我需要从 OnStartTetheringCallback class 创建一个代理,如 android 工作室示例 () 中所示,但该库似乎不适用于 xamarin。请帮忙!
public bool EnableTethering()
{
try
{
var context = Android.App.Application.Context;
ConnectivityManager manager = ConnectivityManager.FromContext(context);
var methods = manager.Class.GetDeclaredMethods()
.Select(m => new KeyValuePair<Java.Lang.Reflect.Method, Java.Lang.Class[]>(m, m.GetParameterTypes()));
var callbackClass = manager.Class.GetDeclaredClasses().First(c => c.Name == "android.net.ConnectivityManager$OnStartTetheringCallback");
var method = methods.First(m => m.Key.Name == "startTethering").Key; //manager.Class.GetDeclaredMethod("startTethering");//, new Java.Lang.Class[] { classFromType<int>(), classFromType<bool>(), classOnStartTetheringCallback(), classFromType<Handler>() } );
if (method == null)
{
}
else
{
method.Invoke(manager, new Java.Lang.Object[] { 0, false, callbackClass });
}
return true;
}
catch (Exception e)
{
}
return false;
}
===================================================================================================
{Java.Lang.IllegalArgumentException: method android.net.ConnectivityManager.startTethering
argument 3 has type android.net.ConnectivityManager$OnStartTetheringCallback, got java.lang.Class<android.net.ConnectivityManager$OnStartTetheringCallback>
at Java.Interop.JniEnvironment+InstanceMethods.CallObjectMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x0006e] in <8b3b636835d84984ba4604c1f57b1983>:0
at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeAbstractObjectMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x00014] in <8b3b636835d84984ba4604c1f57b1983>:0
at Java.Lang.Reflect.Method.Invoke (Java.Lang.Object obj, Java.Lang.Object[] args) [0x0004b] in <84ca7e914f6148f0b961431a9ac4287b>:0
at Millsnet.SpotUp.Services.WifiService.EnableTethering () [0x000af] in C:\Users\djmil\source\repos\Millsnet.SpotUp\Millsnet.SpotUp\Millsnet.SpotUp\Services\WifiService.cs:149
--- End of managed Java.Lang.IllegalArgumentException stack trace ---
java.lang.IllegalArgumentException: method android.net.ConnectivityManager.startTethering argument 3 has type android.net.ConnectivityManager$OnStartTetheringCallback, got java.lang.Class<android.net.ConnectivityManager$OnStartTetheringCallback>
at java.lang.reflect.Method.invoke(Native Method)
at mono.java.lang.RunnableImplementor.n_run(Native Method)
at mono.java.lang.RunnableImplementor.run(RunnableImplementor.java:30)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7711)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
}
===========================================================================================
我设法解决了这个问题。
我编译了一个 LindeIn/DexMaker 的 xamarin 库,所以现在我可以使用代理制造商了。
https://github.com/djmillsuk/LinkedIn-DexMaker-Xamarin
然后我将 android 11 作为目标,因为 starttethering 在 android 10 上不起作用。
它现在运行良好。
希望您能提供帮助,我已尽一切努力使它正常工作。我试图将显然在 android studio 中工作的其他代码改编为在 Xamarin 中工作。我已经到了我无法通过的地步。这是我所拥有的...
理想情况下,我需要从 OnStartTetheringCallback class 创建一个代理,如 android 工作室示例 (
public bool EnableTethering()
{
try
{
var context = Android.App.Application.Context;
ConnectivityManager manager = ConnectivityManager.FromContext(context);
var methods = manager.Class.GetDeclaredMethods()
.Select(m => new KeyValuePair<Java.Lang.Reflect.Method, Java.Lang.Class[]>(m, m.GetParameterTypes()));
var callbackClass = manager.Class.GetDeclaredClasses().First(c => c.Name == "android.net.ConnectivityManager$OnStartTetheringCallback");
var method = methods.First(m => m.Key.Name == "startTethering").Key; //manager.Class.GetDeclaredMethod("startTethering");//, new Java.Lang.Class[] { classFromType<int>(), classFromType<bool>(), classOnStartTetheringCallback(), classFromType<Handler>() } );
if (method == null)
{
}
else
{
method.Invoke(manager, new Java.Lang.Object[] { 0, false, callbackClass });
}
return true;
}
catch (Exception e)
{
}
return false;
}
===================================================================================================
{Java.Lang.IllegalArgumentException: method android.net.ConnectivityManager.startTethering
argument 3 has type android.net.ConnectivityManager$OnStartTetheringCallback, got java.lang.Class<android.net.ConnectivityManager$OnStartTetheringCallback>
at Java.Interop.JniEnvironment+InstanceMethods.CallObjectMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x0006e] in <8b3b636835d84984ba4604c1f57b1983>:0
at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeAbstractObjectMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x00014] in <8b3b636835d84984ba4604c1f57b1983>:0
at Java.Lang.Reflect.Method.Invoke (Java.Lang.Object obj, Java.Lang.Object[] args) [0x0004b] in <84ca7e914f6148f0b961431a9ac4287b>:0
at Millsnet.SpotUp.Services.WifiService.EnableTethering () [0x000af] in C:\Users\djmil\source\repos\Millsnet.SpotUp\Millsnet.SpotUp\Millsnet.SpotUp\Services\WifiService.cs:149
--- End of managed Java.Lang.IllegalArgumentException stack trace ---
java.lang.IllegalArgumentException: method android.net.ConnectivityManager.startTethering argument 3 has type android.net.ConnectivityManager$OnStartTetheringCallback, got java.lang.Class<android.net.ConnectivityManager$OnStartTetheringCallback>
at java.lang.reflect.Method.invoke(Native Method)
at mono.java.lang.RunnableImplementor.n_run(Native Method)
at mono.java.lang.RunnableImplementor.run(RunnableImplementor.java:30)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7711)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
}
===========================================================================================
我设法解决了这个问题。
我编译了一个 LindeIn/DexMaker 的 xamarin 库,所以现在我可以使用代理制造商了。 https://github.com/djmillsuk/LinkedIn-DexMaker-Xamarin
然后我将 android 11 作为目标,因为 starttethering 在 android 10 上不起作用。
它现在运行良好。