I'm writing a J2ME app for a bluetooth mobile advertisement system. Currently I need to scan nearby devices and then send the BT addresses to a PC through socket. I'm using an object oriented approach to put information on my main form which seems be throwing exceptions which I don;t understand @ all. my code is displayed below. When cmdText is executed the app should create a new thread which runs an instance of the class RemoteDeviceDiscovery.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.bluetooth.*;
import java.lang.*;
import java.util.Timer;
import java.util.TimerTask;
/**
* @author Yasir
*/
public class PhoneInfo extends MIDlet implements CommandListener
{
public Display current;
public Form form;
public StringItem str1, str2;
private Command cmdExit;
private Command cmdSend;
private Command cmdOK;
private Command cmdText;
private TextBox t;
private DiscoveryAgent agent;
private Thread t2;
List devList = new List("List of devices", List.IMPLICIT);
public PhoneInfo()
{
current = Display.getDisplay(this);
form = new Form("Device information");
cmdExit = new Command("Exit",Command.EXIT,1);
cmdOK = new Command("Ok", Command.SCREEN,2);
cmdText = new Command("Show TextBox",Command.SCREEN,3);
str1 = new StringItem("Phone Information","Press OK to send");
str2 = new StringItem("Test data","");
}
public void startApp()
{
current.setCurrent(form);
form.addCommand(cmdOK);
form.addCommand(cmdExit);
form.addCommand(cmdText);
current.setCurrent(form);
form.setCommandListener(this);
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
notifyDestroyed();
}
public void commandAction(Command c, Displayable d)
{
if (c==cmdOK)
{
}
else if (c==cmdExit)
{
destroyApp(false);
}
else if (c == cmdText)
{
Thread t1;
RemoteDeviceDiscovery rdd = new RemoteDeviceDiscovery();
t1 = new Thread((Runnable) rdd);
t1.start();
rdd.discover();
}
}
public void showAlert(String message)
{
Alert alert = new Alert("Alert",message,null,AlertType.INFO);
current.setCurrent(alert,form);
}
class RemoteDeviceDiscovery
{
DiscoveryAgent agent;
LocalDevice local = null;
private List device;
public RemoteDeviceDiscovery()
{
try
{
local = LocalDevice.getLocalDevice();
}
catch (BluetoothStateException ex)
{
Alert alert = new Alert("Error retrieving local device!Did you switch on Bluetooth?");
}
}
public void discover()
{
agent = local.getDiscoveryAgent();
try {
agent.startInquiry(DiscoveryAgent.GIAC, (DiscoveryListener) this);
current.setCurrent(form);
form.append("Discovery started");
} catch (BluetoothStateException ex)
{
showAlert("Exception when starting discovery!");
}
}
public void deviceDiscovered(RemoteDevice dev, DeviceClass cod)
{
String address = dev.getBluetoothAddress();
device.insert(0, address, null);
//form.append("Device Address:"+address+"\n");
current.setCurrent(device);
}
public void inquiryCompleted(int arg0)
{
form.append("Inquiry completed");
}
public void serviceSearchCompleted(int arg0, int arg1)
{
}
public void cancelInquiry()
{
agent.cancelInquiry((DiscoveryListener) this);
try
{
local.setDiscoverable(agent.NOT_DISCOVERABLE);
} catch (BluetoothStateException ex)
{
showAlert("Could not set device mode to hidden!");
}
}
public void servicesDiscovered(int arg0, ServiceRecord[] arg1)
{
}
}
}
Exception returned is:
TRACE: <at java.lang.ClassCastException: 0>, Exception caught in Display class java.lang.ClassCastException: 0 at PhoneInfo.commandAction(PhoneInfo.java:84) at javax.microedition.lcdui.Display$ChameleonTunnel.callScreenListener(), bci=46 at com.sun.midp.chameleon.layers.SoftButtonLayer.processCommand(), bci=74 at com.sun.midp.chameleon.layers.SoftButtonLayer.commandSelected(), bci=11 at com.sun.midp.chameleon.layers.MenuLayer.keyInput(), bci=290 at com.sun.midp.chameleon.Cwindow.keyInput(), bci=38 at javax.microedition.lcdui.Display$DisplayEventConsumerImpl.handleKeyEvent(), bci=17 at com.sun.midp.lcdui.DisplayEventListener.process(), bci=277 at com.sun.midp.events.EventQueue.run(), bci=179 at java.lang.Thread.run(Thread.java:619) javacall_lifecycle_state_changed() lifecycle: event is JAVACALL_LIFECYCLE_MIDLET_SHUTDOWNstatus is JAVACALL_OK
Can someone guide me here??

New Topic/Question
Reply



MultiQuote




|