Hi all.
Thanks for a very nice board.
I have come across a problem when i create a midlet. The midlet functions as a normal menu system, and have to call different methods, to start or stop a bluetooth server and client. I have gotten the code for the server and client from another website, but when I try to open the midlet, I get the following error:

java.lang.SecurityException: Application not authorized to access the restricted API
at com.sun.midp.security.SecurityToken.checkIfPermissionAllowed(SecurityToken.java:170)
at com.sun.midp.security.SecurityToken.checkIfPermissionAllowed(SecurityToken.java:145)
at com.sun.midp.midletsuite.MIDletSuiteImpl.checkIfPermissionAllowed(+8)
at com.sun.midp.midlet.MIDletState.<init>(+83)
at javax.microedition.midlet.MIDletProxy.<init>(MIDletProxy.java:33)
at javax.microedition.midlet.MIDlet.<init>(MIDlet.java:70)
at bluechew.BlueChewMIDlet.<init>(BlueChewMIDlet.java:12)
at bluechew.VisualMIDlet.getWelcome(VisualMIDlet.java:182)
at bluechew.VisualMIDlet.startMIDlet(VisualMIDlet.java:67)
at bluechew.VisualMIDlet.startApp(VisualMIDlet.java:504)
at javax.microedition.midlet.MIDletProxy.startApp(MIDletProxy.java:44)
at com.sun.midp.midlet.Scheduler.schedule(+270)
at com.sun.midp.main.Main.runLocalClass(+28)
at com.sun.midp.main.Main.main(+80)

package bluechew;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import org.netbeans.microedition.lcdui.SplashScreen;
import org.netbeans.microedition.lcdui.WaitScreen;
import org.netbeans.microedition.util.SimpleCancellableTask;

/**
 * @author MMA
 */
public class VisualMIDlet extends MIDlet implements CommandListener {
private my_finder finder;
private BlueChewServer server;
    private boolean midletPaused = false;

//<editor-fold defaultstate="collapsed" desc=" Generated Fields ">
private SplashScreen welcome;
private List Menu;
private WaitScreen open_client;
private WaitScreen open_server;
private WaitScreen close_client;
private WaitScreen close_server;
private SimpleCancellableTask task;
private SimpleCancellableTask task1;
private SimpleCancellableTask task2;
private Image image1;
private Image image2;
private SimpleCancellableTask task3;
private SimpleCancellableTask task4;
private SimpleCancellableTask task5;
//</editor-fold>

    /**
     * The VisualMIDlet constructor.
     */
    public VisualMIDlet() {
    }

//<editor-fold defaultstate="collapsed" desc=" Generated Methods ">
//</editor-fold>

//<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">
/**
 * Initilizes the application.
 * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
 */
private void initialize () {
        // write pre-initialize user code here
 
        // write post-initialize user code here
}
//</editor-fold>

//<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">
/**
 * Performs an action assigned to the Mobile Device - MIDlet Started point.
 */
public void startMIDlet () {
        // write pre-action user code here
switchDisplayable (null, getWelcome ());
        // write post-action user code here
}
//</editor-fold>

//<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">
/**
 * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
 */
public void resumeMIDlet () {
        // write pre-action user code here
 
        // write post-action user code here
}
//</editor-fold>

//<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">
/**
 * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
 * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
 * @param nextDisplayable the Displayable to be set
 */
public void switchDisplayable (Alert alert, Displayable nextDisplayable) {
        // write pre-switch user code here
Display display = getDisplay ();
if (alert == null) {
display.setCurrent (nextDisplayable);
} else {
display.setCurrent (alert, nextDisplayable);
}
        // write post-switch user code here
}
//</editor-fold>

//<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">
/**
 * Called by a system to indicated that a command has been invoked on a particular displayable.
 * @param command the Command that was invoked
 * @param displayable the Displayable where the command was invoked
 */
public void commandAction (Command command, Displayable displayable) {
 // write pre-action user code here
if (displayable == Menu) {
if (command == List.SELECT_COMMAND) {
 // write pre-action user code here
MenuAction ();
 // write post-action user code here
}
} else if (displayable == close_client) {
if (command == WaitScreen.FAILURE_COMMAND) {
 // write pre-action user code here
 
 // write post-action user code here
} else if (command == WaitScreen.SUCCESS_COMMAND) {
 // write pre-action user code here
 
 // write post-action user code here
}
} else if (displayable == close_server) {
if (command == WaitScreen.FAILURE_COMMAND) {
 // write pre-action user code here
 
 // write post-action user code here
} else if (command == WaitScreen.SUCCESS_COMMAND) {
 // write pre-action user code here
 
 // write post-action user code here
}
} else if (displayable == open_client) {
if (command == WaitScreen.FAILURE_COMMAND) {
 // write pre-action user code here
 
 // write post-action user code here
} else if (command == WaitScreen.SUCCESS_COMMAND) {
 // write pre-action user code here
 
 // write post-action user code here
}
} else if (displayable == open_server) {
if (command == WaitScreen.FAILURE_COMMAND) {
 // write pre-action user code here
 
 // write post-action user code here
} else if (command == WaitScreen.SUCCESS_COMMAND) {
 // write pre-action user code here
 
 // write post-action user code here
}
} else if (displayable == welcome) {
if (command == SplashScreen.DISMISS_COMMAND) {
 // write pre-action user code here
switchDisplayable (null, getMenu ());
 // write post-action user code here
}
}
 // write post-action user code here
}
//</editor-fold>

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: welcome ">
/**
 * Returns an initiliazed instance of welcome component.
 * @return the initialized component instance
 */
public SplashScreen getWelcome () {
if (welcome == null) {
 // write pre-init user code here
welcome = new SplashScreen (getDisplay ());
welcome.setTitle ("splashScreen");
welcome.setCommandListener (this);
welcome.setImage (getImage2 ());
welcome.setText ("hearing aid control application");
welcome.setTimeout (2000);
 // write post-init user code here

bluechew.BlueChewMIDlet mbluechewmidlet = new bluechew.BlueChewMIDlet();
mbluechewmidlet.startApp();

}
return welcome;
}
//</editor-fold>

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: task ">
/**
 * Returns an initiliazed instance of task component.
 * @return the initialized component instance
 */
public SimpleCancellableTask getTask () {
if (task == null) {
 // write pre-init user code here
task = new SimpleCancellableTask ();
task.setExecutable (new org.netbeans.microedition.util.Executable() {
public void execute () throws Exception {
// write task-execution user code here
}
});
 // write post-init user code here
}
return task;
}
//</editor-fold>

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: Menu ">
/**
 * Returns an initiliazed instance of Menu component.
 * @return the initialized component instance
 */
public List getMenu () {
if (Menu == null) {
 // write pre-init user code here
Menu = new List ("list", Choice.IMPLICIT);
Menu.append ("Open Server", null);
Menu.append ("Open Client", null);
Menu.append ("Close Server", null);
Menu.append ("Close Client", null);
Menu.setCommandListener (this);
Menu.setSelectedFlags (new boolean[] { false, false, false, false });
 // write post-init user code here
}
return Menu;
}
//</editor-fold>

//<editor-fold defaultstate="collapsed" desc=" Generated Method: MenuAction ">
/**
 * Performs an action assigned to the selected list element in the Menu component.
 */
public void MenuAction () {
 // enter pre-action user code here
String __selectedString = getMenu ().getString (getMenu ().getSelectedIndex ());
if (__selectedString != null) {
if (__selectedString.equals ("Open Server")) {
 // write pre-action user code here
switchDisplayable (null, getOpen_server ());
 // write post-action user code here
} else if (__selectedString.equals ("Open Client")) {
 // write pre-action user code here
switchDisplayable (null, getOpen_client ());
 // write post-action user code here
} else if (__selectedString.equals ("Close Server")) {
 // write pre-action user code here
switchDisplayable (null, getClose_server ());
 // write post-action user code here
} else if (__selectedString.equals ("Close Client")) {
 // write pre-action user code here
switchDisplayable (null, getClose_client ());
 // write post-action user code here
}
}
 // enter post-action user code here
}
//</editor-fold>

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: open_client ">
/**
 * Returns an initiliazed instance of open_client component.
 * @return the initialized component instance
 */
public WaitScreen getOpen_client () {
if (open_client == null) {
 // write pre-init user code here
open_client = new WaitScreen (getDisplay ());
open_client.setTitle ("Open Client ");
open_client.setCommandListener (this);
open_client.setImage (getImage1 ());
open_client.setText ("Client connection Opening");
open_client.setTask (getTask2 ());
 // write post-init user code here
}
return open_client;
}
//</editor-fold>

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: task1 ">
/**
 * Returns an initiliazed instance of task1 component.
 * @return the initialized component instance
 */
public SimpleCancellableTask getTask1 () {
if (task1 == null) {
 // write pre-init user code here
task1 = new SimpleCancellableTask ();
task1.setExecutable (new org.netbeans.microedition.util.Executable() {
public void execute () throws Exception {
// write task-execution user code here
}
});
 // write post-init user code here
}
return task1;
}
//</editor-fold>

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: task2 ">
/**
 * Returns an initiliazed instance of task2 component.
 * @return the initialized component instance
 */
public SimpleCancellableTask getTask2 () {
if (task2 == null) {
 // write pre-init user code here
task2 = new SimpleCancellableTask ();
task2.setExecutable (new org.netbeans.microedition.util.Executable() {
public void execute () throws Exception {
// write task-execution user code here
    finder.start();
}
});
 // write post-init user code here
}
return task2;
}
//</editor-fold>

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: image1 ">
/**
 * Returns an initiliazed instance of image1 component.
 * @return the initialized component instance
 */
public Image getImage1 () {
if (image1 == null) {
 // write pre-init user code here
try {
image1 = Image.createImage ("/bluetooth_small.jpg");
} catch (java.io.IOException e) {
e.printStackTrace ();
}
 // write post-init user code here
}
return image1;
}
//</editor-fold>

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: image2 ">
/**
 * Returns an initiliazed instance of image2 component.
 * @return the initialized component instance
 */
public Image getImage2 () {
if (image2 == null) {
 // write pre-init user code here
try {
image2 = Image.createImage ("/HighDifinitionHearing.gif");
} catch (java.io.IOException e) {
e.printStackTrace ();
}
 // write post-init user code here
}
return image2;
}
//</editor-fold>

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: open_server ">
/**
 * Returns an initiliazed instance of open_server component.
 * @return the initialized component instance
 */
public WaitScreen getOpen_server () {
if (open_server == null) {
 // write pre-init user code here
open_server = new WaitScreen (getDisplay ());
open_server.setTitle ("waitScreen");
open_server.setCommandListener (this);
open_server.setTask (getTask3 ());
 // write post-init user code here
}
return open_server;
}
//</editor-fold>

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: close_client ">
/**
 * Returns an initiliazed instance of close_client component.
 * @return the initialized component instance
 */
public WaitScreen getClose_client () {
if (close_client == null) {
 // write pre-init user code here
close_client = new WaitScreen (getDisplay ());
close_client.setTitle ("waitScreen");
close_client.setCommandListener (this);
close_client.setTask (getTask4 ());
 // write post-init user code here
}
return close_client;
}
//</editor-fold>

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: close_server ">
/**
 * Returns an initiliazed instance of close_server component.
 * @return the initialized component instance
 */
public WaitScreen getClose_server () {
if (close_server == null) {
 // write pre-init user code here
close_server = new WaitScreen (getDisplay ());
close_server.setTitle ("waitScreen");
close_server.setCommandListener (this);
close_server.setTask (getTask5 ());
 // write post-init user code here
}
return close_server;
}
//</editor-fold>

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: task3 ">
/**
 * Returns an initiliazed instance of task3 component.
 * @return the initialized component instance
 */
public SimpleCancellableTask getTask3 () {
if (task3 == null) {
 // write pre-init user code here
task3 = new SimpleCancellableTask ();
task3.setExecutable (new org.netbeans.microedition.util.Executable() {
public void execute () throws Exception {
// write task-execution user code here
    server.start();
}
});
 // write post-init user code here
}
return task3;
}
//</editor-fold>

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: task4 ">
/**
 * Returns an initiliazed instance of task4 component.
 * @return the initialized component instance
 */
public SimpleCancellableTask getTask4 () {
if (task4 == null) {
 // write pre-init user code here
task4 = new SimpleCancellableTask ();
task4.setExecutable (new org.netbeans.microedition.util.Executable() {
public void execute () throws Exception {
// write task-execution user code here
    
    finder.stop();
}
});
 // write post-init user code here
}
return task4;
}
//</editor-fold>

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: task5 ">
/**
 * Returns an initiliazed instance of task5 component.
 * @return the initialized component instance
 */
public SimpleCancellableTask getTask5 () {
if (task5 == null) {
 // write pre-init user code here
task5 = new SimpleCancellableTask ();
task5.setExecutable (new org.netbeans.microedition.util.Executable() {
public void execute () throws Exception {
// write task-execution user code here
    server.stop();
}
});
 // write post-init user code here
}
return task5;
}
//</editor-fold>

    /**
     * Returns a display instance.
     * @return the display instance.
     */
    public Display getDisplay () {
        return Display.getDisplay(this);
    }

    /**
     * Exits MIDlet.
     */
    public void exitMIDlet() {
        switchDisplayable (null, null);
        destroyApp(true);
        notifyDestroyed();
    }

    /**
     * Called when MIDlet is started.
     * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
     */
    public void startApp() {
        if (midletPaused) {
            resumeMIDlet ();
        } else {
            initialize ();
            startMIDlet ();
        }
        midletPaused = false;
    }

    /**
     * Called when MIDlet is paused.
     */
    public void pauseApp() {
        midletPaused = true;
    }

    /**
     * Called to signal the MIDlet to terminate.
     * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
     */
    public void destroyApp(boolean unconditional) {
    }

}

Hope you can help me. thanks in advance.
Michael Madsen.

Recommended Answers

All 5 Replies

Can you attach either copy of src folder or whole project as I'm not sure what changes you made to BlueChewServer. Plus you calling some "my_finder" which seems to be product of yours (if it is name of class you should get slap on hand as this is not correct way to name a class, it should be MyFinder)

Hi Peter.

Here is an attacthment of the source. I havent changed anything in the server, så this is just an implementation of a midlet, as a replacement of the midlet that was already made. The my_finder, is just my try of making an instance of the midlet. I dont know if that was the correct way to do it. thanks in advance. Michael Madsen

Sorry. The my_finder, is the finder class, that I have tried to make. It should work, but I will change the name of it, to MyFinder.

LOL, you forgot to provide source images that you use with your VisualMIDlet (no worry I got replacement).
The problem with your application is that from your VisualMIDlet you are trying to call upon another MIDlet in bad fashion. It is possible to call MIDlet from MIDlet (here is example), but it is recommended to use only one. Secondly I never did it so I will not able to advice if you push for two MIDlet option.
However if you decide to go for single MIDlet then you will need to modify one of them. I would modify VisaulMIDlet as BlueChewMIDlet is giving you solid ground to work from and it will be much easier to attach class with application GUI to it.

Ok. I will try to do that. Thanks for the help mate.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.