i like to turn on hotspot in my app using button, old code work perfect in old android version, but fail in android oreo.
i found the java code can turn on hotspot in oreo version, i compile the code in oreo version phone is work perfect, but another os version get error.
here is my test code:
error code here:
how can i disable the java code when using os below oreo?
i found the java code can turn on hotspot in oreo version, i compile the code in oreo version phone is work perfect, but another os version get error.
here is my test code:
B4X:
#Region Project Attributes
#ApplicationLabel: B4A Example
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region
#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Private rp As RuntimePermissions
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Private NativeMe As JavaObject
Private status As String=""
End Sub
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
Activity.LoadLayout("1")
NativeMe.InitializeContext
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub Activity_PermissionResult (Permission As String, Result As Boolean)
If Permission = rp.PERMISSION_ACCESS_FINE_LOCATION Then
If status="ON" Then
NativeMe.RunMethod("turnOnHotspot",Null)
else If status="OFF" Then
NativeMe.RunMethod("turnOffHotspot",Null)
End If
End If
End Sub
Sub btn_on_Click
status="ON"
rp.CheckAndRequest(rp.PERMISSION_ACCESS_FINE_LOCATION)
End Sub
Sub btn_off_Click
status="OFF"
rp.CheckAndRequest(rp.PERMISSION_ACCESS_FINE_LOCATION)
End Sub
#If java
import java.lang.*;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Build;
import android.os.Handler;
import android.content.Context;
import android.os.Message;
import android.util.Log;
import java.io.IOException;
import java.lang.reflect.Method;
int sdk = android.os.Build.VERSION.SDK_INT;
public WifiManager.LocalOnlyHotspotReservation mReservation;
public void turnOnHotspot() {
WifiManager manager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
manager.startLocalOnlyHotspot(new WifiManager.LocalOnlyHotspotCallback() {
@Override
public void onStarted(WifiManager.LocalOnlyHotspotReservation reservation) {
super.onStarted(reservation);
BA.Log("Wifi Hotspot is on now");
mReservation = reservation;
}
@Override
public void onStopped() {
super.onStopped();
BA.Log("onStopped: ");
}
@Override
public void onFailed(int reason) {
super.onFailed(reason);
BA.Log("onFailed: ");
}
}, new Handler());
}
public void turnOffHotspot() {
if (mReservation != null) {
mReservation.close();
}
}
#End If
error code here:
B4X:
java.lang.NoClassDefFoundError: Failed resolution of: Landroid/net/wifi/WifiManager$LocalOnlyHotspotReservation;
at java.lang.Class.getDeclaredFields(Native Method)
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayoutHelper(LayoutBuilder.java:368)
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayoutHelper(LayoutBuilder.java:454)
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayout(LayoutBuilder.java:148)
at anywheresoftware.b4a.objects.ActivityWrapper.LoadLayout(ActivityWrapper.java:209)
at b4a.example.main._activity_create(main.java:392)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:342)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
at b4a.example.main.afterFirstLayout(main.java:120)
at b4a.example.main.access$000(main.java:27)
at b4a.example.main$WaitForLayout.run(main.java:92)
at android.os.Handler.handleCallback(Handler.java:754)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:163)
at android.app.ActivityThread.main(ActivityThread.java:6342)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:880)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.net.wifi.WifiManager$LocalOnlyHotspotReservation" on path: DexPathList[[zip file "/data/app/b4a.example-2/base.apk"],nativeLibraryDirectories=[/data/app/b4a.example-2/lib/arm64, /system/lib64, /vendor/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
... 23 more
** Activity (main) Pause, UserClosed = false **
how can i disable the java code when using os below oreo?