You Are Here:

Community: Developer Discussion Boards

#1 Old Bluetooth discovery loop (log nearby devices) - 2008-02-21, 11:02

Join Date: Feb 2008
Posts: 11
mortenlindeberg
Offline
Registered User
Hi,

I am trying to create an MIDlet that logs the friendly names of nearby devices constantly, lets say each 30th second. The problem I encounter is that when the DiscoveryAgent has finished its inquiry, it seems I am unable of starting it over again. Even though I am killing the thread and creating a new on, I get an exception:

Quote:
javax.bluetooth.BluetoothStateException: The previous device discovery is running...
Does any one of you know how to get past this problem?

Any thoughts, are welcome!
Morten Lindeberg
PhD student
University of Oslo
Reply With Quote

#2 Old Re: Bluetooth discovery loop (log nearby devices) - 2008-02-21, 13:47

Join Date: Feb 2008
Posts: 13
sohamsengupta@yahoo.com
Offline
Registered User
Go through the code below....
===============================
/*
* HelloMidlet.java
*
* Created on February 21, 2008, 5:53 PM
*/

package hello;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.bluetooth.*;
/**
*
* @author sohamsengupta
*/
public class HelloMidlet extends MIDlet implements CommandListener,DiscoveryListener {
private DiscoveryAgent agent=null;
private boolean running=false;
/** Creates a new instance of HelloMidlet */
public HelloMidlet() {
}

private Form helloForm;
private StringItem helloStringItem;
private Command exitCommand;
private Command cmdSrch=new Command("Search",Command.SCREEN,2);


/** This method initializes UI of the application.
*/
private void initialize() {
// Insert pre-init code here
getDisplay().setCurrent(get_helloForm());
// Insert post-init code here
}

/** Called by the system to indicate that a command has been invoked on a particular displayable.
* @param command the Command that ws invoked
* @param displayable the Displayable on which the command was invoked
*/
public void commandAction(Command command, Displayable displayable) {
// Insert global pre-action code here
if (displayable == helloForm) {
if (command == exitCommand) {
// Insert pre-action code here
exitMIDlet();
// Insert post-action code here
}
}
if(command==cmdSrch){
if(null!=agent && running==true){
agent.cancelInquiry(this);
}else{
try{
agent=LocalDevice.getLocalDevice().getDiscoveryAgent();
agent.startInquiry(DiscoveryAgent.GIAC,this);
running=true;
}catch(Throwable t){
showAlert(t);
}
}
}
}

private void showAlert(Throwable t){
Alert al=new Alert("Error",t.toString(),null,AlertType.ERROR);
getDisplay().setCurrent(al);
}
/**
* This method should return an instance of the display.
*/
public Display getDisplay() {
return Display.getDisplay(this);
}

/**
* This method should exit the midlet.
*/
public void exitMIDlet() {
getDisplay().setCurrent(null);
destroyApp(true);
notifyDestroyed();
}

/** This method returns instance for helloForm component and should be called instead of accessing helloForm field directly.
* @return Instance for helloForm component
*/
public Form get_helloForm() {
if (helloForm == null) {
// Insert pre-init code here
helloForm = new Form(null, new Item[] {get_helloStringItem()});
helloForm.addCommand(get_exitCommand());
helloForm.setCommandListener(this);
// Insert post-init code here
helloForm.addCommand(cmdSrch);
}
return helloForm;
}

/** This method returns instance for helloStringItem component and should be called instead of accessing helloStringItem field directly.
* @return Instance for helloStringItem component
*/
public StringItem get_helloStringItem() {
if (helloStringItem == null) {
// Insert pre-init code here
helloStringItem = new StringItem("Hello", "Hello, World!");
// Insert post-init code here
}
return helloStringItem;
}

/** This method returns instance for exitCommand component and should be called instead of accessing exitCommand field directly.
* @return Instance for exitCommand component
*/
public Command get_exitCommand() {
if (exitCommand == null) {
// Insert pre-init code here
exitCommand = new Command("Exit", Command.EXIT, 1);
// Insert post-init code here
}
return exitCommand;
}

public void startApp() {
initialize();
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

public void deviceDiscovered(RemoteDevice remoteDevice, DeviceClass deviceClass) {
try{
helloForm.append(remoteDevice.getFriendlyName(true));
}catch(Throwable t){
// show some alert here
showAlert(t);
}

}

public void inquiryCompleted(int i) {
running=false;
}

public void servicesDiscovered(int i, ServiceRecord[] serviceRecord) {
}

public void serviceSearchCompleted(int i, int i0) {
}

}
======================================================
The above code searches for devices in proximity whenever Search button is pressed... but shall not give the Exception you got...let me explain why....
You run this yourself on Simulator... now you consider the code fragment...
if(command==cmdSrch){
if(null!=agent && running==true){
agent.cancelInquiry(this);
}else{
try{
agent=LocalDevice.getLocalDevice().getDiscoveryAgent();
agent.startInquiry(DiscoveryAgent.GIAC,this);
running=true;
}catch(Throwable t){
showAlert(t);
}
}
}
===========
It checks whether any previously initiated Desicovery is in progress, and if yes, cancels it and restarts it on the next click. In your case, each 30 seconds you write the code as I did.
should any query arise further, contact me at sohamsengupta@yahoo.com

Thanks
Soham Sengupta
Sr. Lecturer, Dept of Information Technology,
JIS College of Enginering, Kalyani, India
Formerly, Systems Engineer, TCS
Author, dW, IBM,US
Reply With Quote

#3 Old Re: Bluetooth discovery loop (log nearby devices) - 2008-02-21, 17:13

Join Date: Feb 2008
Posts: 11
mortenlindeberg
Offline
Registered User
Thank you Soham Sengupta!

My code was originally not far from your code, only that I did not realize that calling cancelInquiry actually leads to inquiryCompleted being called.. This has been caused one of the problem I have encounted. (which thanks to you are solved!)

Additionally, in order to get my application up and running, it seems I needed to wait a bit between cancelInquiry and the startInquiry method call. BluetoothStateException appears without the sleep for some reason..

Quote:
while (true) {
Thread.sleep(BLUETOOTH_SLEEP);

if (running == false) {
discoveryAgent.cancelInquiry(this);
Thread.sleep(5000); // My little wonder hack..
discoveryAgent.startInquiry(DiscoveryAgent.GIAC, this);
running = true;
}
}
And it works..

Best regards, and happy wishes!
Morten Lindeberg
PhD student
University of Oslo
Reply With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump
Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in transfer data with Bluetooth in real devices tortoisevn Bluetooth Technology 1 2006-11-07 12:35
How many bluetooth devices can Nokia phones support simultaneously? soulshagga Bluetooth Technology 0 2006-10-30 03:24
How to get a device's bluetooth address before I connect to it. doglovecat527 Symbian Networking & Messaging 0 2005-09-02 08:50
3rd Party Bluetooth Devices with Nokia Connection Manager Dmore Bluetooth Technology 1 2002-06-13 09:32
Bluetooth simultaneous connection to 2 devices Nokia_Archive Bluetooth Technology 1 2002-05-27 20:01

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia