Android Question Inline Java to turn on hotspot

dragonguy

Active Member
Licensed User
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:
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?
 

OliverA

Expert
Licensed User
If you have working code for older versions and newer code that works with Oreo, why not use if statements and run the proper code depending on the version of Android running?
 

dragonguy

Active Member
Licensed User
If you have working code for older versions and newer code that works with Oreo, why not use if statements and run the proper code depending on the version of Android running?
i try before but the error occur at this line:
B4X:
public WifiManager.LocalOnlyHotspotReservation mReservation;
i dont know how to disable this line when the os below oreo.
 

OliverA

Expert
Licensed User
Declare it as Object and then cast it properly in your methods
 

dragonguy

Active Member
Licensed User
still no idea how to cast the method properly
B4X:
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 android.net.wifi.WifiManager.LocalOnlyHotspotReservation;

int sdk = android.os.Build.VERSION.SDK_INT;


//public WifiManager.LocalOnlyHotspotReservation mReservation;
public Object 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) {
      WifiManager.LocalOnlyHotspotReservation mReservation1;
      mReservation1=mReservation;
      mReservation1.close();
    }
}
error occur when compile:
B4X:
B4A Version: 8.00
Parsing code.    (0.01s)
Compiling code.    (0.10s)
    
ObfuscatorMap.txt file created in Objects folder.
Compiling layouts code.    (0.00s)
Organizing libraries.    (0.00s)
Generating R file.    (1.13s)
Compiling generated Java code.    Error
javac 1.8.0_131
src\b4a\example\main.java:467: error: incompatible types: Object cannot be converted to WifiManager.LocalOnlyHotspotReservation
      mReservation1=mReservation;
                    ^
Note: src\b4a\example\starter.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
1 error
 

OliverA

Expert
Licensed User
You just forgot about the "casting":
B4X:
mReservation1= (WifiManager.LocalOnlyHotspotReservation) mReservation;
Edit: You can also do this:
B4X:
public void turnOffHotspot() {
 if (mReservation != null) {
        ((WifiManager.LocalOnlyHotspotReservation) mReservation).close();
    }
}
 
Last edited:
Top