You Are Here:

Community: Developer Discussion Boards

#1 Old 6600, java, bluetooth, discovery - 2004-02-19, 20:58

Join Date: Feb 2004
Posts: 2
durban1234
Offline
Registered User
Hi Guys!

I'm trying to write a simple device&service discoverer midlet, just for fun. On the Nokia J2ME emulator with bluetooth works fine: 2 emus see each other. Compliled by J2SE1.4.2 with WTK2.0, tested on emus, uploaded by Ir as JAR, installed on MMC, loading failed with an alert:
App. closed
jes-xxx-javax.microedition.lcdui1@1...
Please help me, the DeviceControl usage (lights, vibra) also failed with this jes* like messages. What is this message?

The Source:
<source>
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import java.lang.*;
import java.io.*;
import java.util.*;
import javax.microedition.io.*;
import javax.bluetooth.*;
public class UDSMidlet
extends MIDlet
implements CommandListener, ItemCommandListener,DiscoveryListener{

private Display display;
private Form mainForm;
private StringItem message;
private final static Command CMD_GO = new Command("Go", Command.ITEM, 1);
private final static Command CMD_PRESS = new Command("Press", Command.ITEM, 1);
private final static Command CMD_EXIT = new Command("Exit", Command.EXIT, 1);
// bt common
LocalDevice local = null;
DeviceClass deviceClass = null;
DiscoveryAgent agent;
ServiceRecord record;
Vector remoteDevices = new Vector();
Hashtable deviceClassesByRemoteDevices = new Hashtable();
Vector transIDs = new Vector();
Hashtable serviceRecordByTransIDs = new Hashtable();
public static long UUID_SDP = 0x0001;
public static long UUID_RFCOMM = 0x0003;
public static long UUID_OBEX = 0x0008;
public static long UUID_HTTP = 0x000C;
public static long UUID_L2CAP = 0x0100;
public static long UUID_BNEP = 0x000F;
public static long UUID_Serial_Port = 0x1101;
public static long UUID_ServiceDiscoveryServerServiceClassID = 0x1000;
public static long UUID_BrowseGroupDescriptorServiceClassID = 0x1001;
public static long UUID_PublicBrowseGroup = 0x1002;
public static long UUID_OBEX_Object_Push_Profile = 0x1105;
public static long UUID_OBEX_File_Transfer_Profile = 0x1106;
public static long UUID_Personal_Area_Networking_User = 0x1115;
public static long UUID_Network_Access_Point = 0x1116;
public static long UUID_Group_Network = 0x1117;
public static long[] UUIDS = new long[] {
UUID_SDP,UUID_RFCOMM,UUID_OBEX,UUID_HTTP,UUID_L2CAP,UUID_BNEP,
UUID_Serial_Port,UUID_ServiceDiscoveryServerServiceClassID,
UUID_BrowseGroupDescriptorServiceClassID,UUID_PublicBrowseGroup,
UUID_OBEX_Object_Push_Profile,UUID_OBEX_File_Transfer_Profile,
UUID_Personal_Area_Networking_User,UUID_Network_Access_Point,
UUID_Group_Network
};
/**
* Signals the MIDlet to start and enter the Active state.
*/
protected void startApp() {
display = Display.getDisplay(this);
//bt init
try {
local = LocalDevice.getLocalDevice();
agent = local.getDiscoveryAgent();
deviceClass = local.getDeviceClass();
} catch (BluetoothStateException e) {
//System.out.println("Failed to start Bluetooth System");
//System.out.println("\tBluetoothStateException: " + e.getMessage());
}

mainForm = new Form("UDS Midlet");
mainForm.append(getBtInfo());
Item item = null;

item = new StringItem("k¨¦kfog indulhat", "", Item.BUTTON);
item.setDefaultCommand(CMD_GO);
item.setItemCommandListener(this);
mainForm.append(item);

message = new StringItem("", "", Item.PLAIN);
mainForm.append(message);

mainForm.addCommand(CMD_EXIT);
mainForm.setCommandListener(this);
display.setCurrent(mainForm);
}

public String getBtInfo() {
return "BluetoothAddress:"+local.getBluetoothAddress() + "\n" +
"FriendlyName:"+local.getFriendlyName() + "\n" +
codToString(deviceClass) + "\n";
}
public String respCodeToString (int respCode) {
if (respCode == SERVICE_SEARCH_COMPLETED)
return "SERVICE_SEARCH_COMPLETED";
else if (respCode == SERVICE_SEARCH_TERMINATED)
return "SERVICE_SEARCH_TERMINATED";
else if (respCode == SERVICE_SEARCH_ERROR)
return "SERVICE_SEARCH_ERROR";
else if (respCode == SERVICE_SEARCH_NO_RECORDS)
return "SERVICE_SEARCH_NO_RECORDS";
else if (respCode == SERVICE_SEARCH_DEVICE_NOT_REACHABLE)
return "SERVICE_SEARCH_DEVICE_NOT_REACHABLE";
else
return "UNKNOWN";
}
public String discTypeToString (int discType) {
if (discType == INQUIRY_COMPLETED)
return "INQUIRY_COMPLETED";
else if (discType == INQUIRY_TERMINATED)
return "INQUIRY_TERMINATED";
else if (discType == INQUIRY_ERROR)
return "INQUIRY_ERROR";
else
return "UNKNOWN";
}
public String remoteDevicesToString () {
StringBuffer sb = new StringBuffer();
for(int i = 0; i<remoteDevices.size();i++) {
RemoteDevice btDevice = (RemoteDevice)remoteDevices.elementAt(i);
sb.append("BluetoothAddress:");
sb.append( btDevice.getBluetoothAddress() );
sb.append("\nFriendlyName:");
try {
sb.append( btDevice.getFriendlyName(false) );
} catch (IOException io) {
sb.append( io.toString() );
}
String cod = (String)deviceClassesByRemoteDevices.get(btDevice.getBluetoothAddress());
sb.append("\nDeviceClass:");
sb.append( cod + "\n");
}
return sb.toString();
}

public String serviceRecordsToString (ServiceRecord[] servRecord) {
StringBuffer sb = new StringBuffer();
for(int i = 0;servRecord!=null && i<servRecord.length;i++) {
ServiceRecord servRec = servRecord[i];
if (servRec != null) {
int[] ids = servRec.getAttributeIDs();
for(int j = 0;ids!=null && j<ids.length;j++) {
DataElement elem = servRec.getAttributeValue(ids[j]);
sb.append(ids[j]);
sb.append(":");
sb.append( (elem!=null? elem.getValue() : "null") );
}
}
}
return sb.toString();
}

public String codToString(DeviceClass cod) {
return cod.getMajorDeviceClass()
+"/"+cod.getMinorDeviceClass()
+"/"+cod.getServiceClasses();
}
//Called when a device is found during an inquiry.
public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
//System.out.println("BtDevice:"+btDevice);
//System.out.println("DeviceClass:"+codToString(cod));
if (!remoteDevices.contains(btDevice)) {
remoteDevices.addElement(btDevice);
}
deviceClassesByRemoteDevices.put(btDevice.getBluetoothAddress(),codToString(cod));
}
//Called when an inquiry is completed.
public void inquiryCompleted(int discType) {
//System.out.println("inquiry is completed");
message.setText(
"\n" + discTypeToString(discType)
+ "\n" + remoteDevicesToString()
);
synchronized (this) {
this.notifyAll();
}
}
//Called when service(s) are found during a service search.
public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
//System.out.println("transID:"+transID);
//System.out.println("ServiceRecord:"+(servRecord != null ? servRecord.toString() : "null"));
Integer TransID = new Integer(transID);
if (!transIDs.contains(TransID)) {
//transIDs.addElement(TransID);
}
if (servRecord != null) {
serviceRecordByTransIDs.put(TransID, servRecord);
} else {
//System.out.println("ServiceRecord is null");
}
}
//Called when a service search is completed or was terminated because of an error.
public void serviceSearchCompleted(int transID, int respCode) {
message.setText(
"\n" + respCodeToString(respCode)
+ "\n" + serviceRecordsToString((ServiceRecord[])serviceRecordByTransIDs.get(new Integer(transID)))
);
synchronized (this) {
this.notifyAll();
}
}

public void commandAction(Command c, Item item) {
if (c == CMD_GO) {
try {
remoteDevices.removeAllElements();
deviceClassesByRemoteDevices.clear();
transIDs.removeAllElements();
serviceRecordByTransIDs.clear();
//System.out.println("device discovery start");
agent.startInquiry(
DiscoveryAgent.GIAC, //General/Unlimited Inquiry Access Code
this
);
synchronized (this) {
try {
this.wait();
} catch (Exception e) {
}
}
//System.out.println("device discovery end");
UUID[] uuidSet = new UUID[UUIDS.length];
for (int z=0;z<UUIDS.length;z++) {
uuidSet[z] = new UUID(UUIDS[z]);
}
//System.out.println("service discovery start");
for(int i = 0; i<remoteDevices.size();i++) {
RemoteDevice btDevice = (RemoteDevice)remoteDevices.elementAt(i);
try {
int transID = agent.searchServices(new int[0], // only default attributes
uuidSet,
btDevice,
this);
//System.out.println("starting service search " + transID);
transIDs.addElement(new Integer(transID));
synchronized (this) {
try {
this.wait();
} catch (Exception e) {
}
}
//System.out.println("service search ended " + transID);
} catch (javax.bluetooth.BluetoothStateException btEx) {
//System.out.println("Failed using Bluetooth");
//System.out.println("\t"+btEx.toString()+"\n");
}
}
//System.out.println("service discovery end");
} catch (Exception e) {
//System.out.println("Failed using Bluetooth");
//System.out.println("\t"+e.toString()+"\n");
}
}
}

public void commandAction(Command c, Displayable d) {
destroyApp(false);
notifyDestroyed();
}

/**
* Signals the MIDlet to terminate and enter the Destroyed state.
*/
protected void destroyApp(boolean unconditional) {
}

/**
* Signals the MIDlet to stop and enter the Paused state.
*/
protected void pauseApp() {
}
}
</source>

Thank You for helping,
durban
Reply With Quote

#2 Old 2004-03-12, 20:19

Join Date: Feb 2004
Posts: 4
fgd00
Offline
Registered User
Hi, I've been having the same problem as you.... The problem seems to be in the device discovery: Works perfectly on the Emus, but this strange error message when loading the program on real device. Any developpments since then??
Reply With Quote

#3 Old 2004-03-13, 12:43

Join Date: Jan 2004
Posts: 7
kufa_reason
Offline
Registered User
You shouldn't block the user interface: you call a start inquiry in the commandAction, and you have this:

> synchronized (this) {
> try {
> this.wait();
> } catch (Exception e) {
> }

which will block the user interface...

/kUfa
Reply With Quote

#4 Old 2004-03-14, 11:09

Join Date: Feb 2004
Posts: 4
fgd00
Offline
Registered User
kufa,

Thanks, I tried that, but the program still doesn't even load.... I haven't even got the time to call the CommandAction function......

The Error is:

App. closed
jes-xxx-javax.microedition.lcdui1@1...
Reply With Quote

#5 Old 2005-03-27, 01:43

Join Date: Dec 2004
Posts: 3
sevics
Offline
Registered User
Hi,

I know it has been a while since you have posted this. I had the same problem couple months ago. At the end, when I built the jar file without including the blootooth library, it worked. So I ended up having two built files: 1 for emulator, 1 for the device itself.

I hope this helps...
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

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