Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5963513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:15:59+00:00 2026-05-22T19:15:59+00:00

I have an application that needs to scan for bluetooth devices every 10 minutes

  • 0

I have an application that needs to scan for bluetooth devices every 10 minutes for a duration set by the user.

I have two javax.swing.Timer (not java.util.Timer) – one controls invoking the Scanning method every 10 minutes, the other is stopping the first timer (therfore stopping the scanning) once the duration limit has been hit.

The durationTimer is created and started within an actionListener.

My problem is that because durationTimer is created within an ActionListener, i cannot stop the timer from another ActionListener, as the program cannot “see” the variable name “durationTimer”.

The reduced code is shown below….

public class mainGui extends JFrame

{



public mainGui()

  {

    final ActionListener timerActionEvent = new ActionListener(){

        public void actionPerformed(ActionEvent evt){

            //start a task here

            Timer myTimer2 = (Timer) evt.getSource();

            //Invoke BluetoothScan method

            BluetoothScan(myTimer2);

        }

    };







    final Timer timerDuration;

    final Timer myTimer = new Timer(5000, timerActionEvent); 



    final ActionListener timerDurationActionEvent = new ActionListener(){

        public void actionPerformed(ActionEvent evt){

            //Stops myTimer, so that Bluetooth Stops scanning every 10mins. 

            myTimer.stop();

        }

    };






    ActionListener btnScanAction = new ActionListener(){

    //Action listener for reading data from db

    public void actionPerformed(ActionEvent e){

        int roomID = 0;

        int lecturer = 0;

        int unit;

        int roomIDIndex;

        int lectIDIndex;

        int yearIDIndex;

        int unitIDIndex;

        String[] roomArray;

        String[] lecturerArray;

        String[] unitArray = null;

        int durationIndex;

        String DURATION;

        int durationInt;





        //System.out.println(unitArray.length);

        durationIndex = durCB.getSelectedIndex();

        DURATION = itemDuration[durationIndex];

        durationInt = Integer.parseInt(DURATION);



        //User Selected Duration converted to Milliseconds

        int durationMilliSec = (int)(durationInt*60000);



        ArrayList<String[]> unitYear = null;



        //Store the index ID of the JComboBox Selections

        roomIDIndex = roomCB.getSelectedIndex();

        lectIDIndex = lectCB.getSelectedIndex();

        unitIDIndex = unitCB.getSelectedIndex();

        yearIDIndex = yearCB.getSelectedIndex();



        switch(yearIDIndex)

        {

        case 1: unitYear = Units1; break;

        case 2: unitYear = Units2; break;

        case 3: unitYear = Units3; break;

        case 4: unitYear = UnitsMasters; break;

        }



        //Get the Array contents at index location

        roomArray = rooms.get(roomIDIndex);

        lecturerArray = Lecturers.get(lectIDIndex);

        unitArray = unitYear.get(unitIDIndex);





        if(unitArray==null){

            System.out.println("Please select a unit");

            System.exit(0);

        }



        roomID = Integer.parseInt(roomArray[0]);

        lecturer = Integer.parseInt(lecturerArray[0]);

        unit = Integer.parseInt(unitArray[0]);



        populateComboBoxes pcb = new populateComboBoxes();

        pcb.LabSessionInfo(roomID, lecturer, unit);



        myTimer.start();

        Timer timerDuration = new Timer(durationMilliSec, timerDurationActionEvent);

        timerDuration.start();





        }

    };



public void BluetoothScan(Timer myTimer) {

BluetoothDeviceDiscovery scan = new BluetoothDeviceDiscovery();

try {

    myTimer.stop();

    scan.main();

} catch (IOException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

} catch (InterruptedException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

}

myTimer.start();



};



  }

Thankyou in advance for any help

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T19:16:00+00:00Added an answer on May 22, 2026 at 7:16 pm

    You can declare the durationTimer at the class level and still construct it in the ActionListener. This way it should be visible throughout the class. Just be sure to check that it’s not null before trying to call stop() on it.

    Another option is to have it stop itself:

    final ActionListener timerDurationActionEvent = new ActionListener(){
        public void actionPerformed(ActionEvent evt){
            //Stops myTimer, so that Bluetooth Stops scanning every 10mins. 
            myTimer.stop();
            ((Timer)evt.getSource()).stop();
        }
    };
    

    And another (and perhaps the best) option is to simply make it non-repeating:

        Timer timerDuration = new Timer(durationMilliSec, timerDurationActionEvent);
        timerDuration.setRepeats(false);
        timerDuration.start();
    

    This last one is the way to go.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that needs to hit the ActiveDirectory to get user permission/roles
We have an application that needs to know the path that it is executed
I have an application that needs to send a moderately high volume of messages
I have an application that needs to poll a webservice to see if the
we have an application that needs to send out various different types of template
I have a flex application that needs the ability to generate and execute JavaScript.
I have a mapping application that needs to draw a path, and then display
I have C# winforms application that needs to start an external exe from time
I have a web application that needs to take a file upload from the
I have a XULRunner application that needs to copy image data to the clipboard.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.