Hello members,
I'm writing a bluetooth mobile advertisement system as my dissertation. In fact I need to retrieve the phone's information and then return it to a PC using sockets. First of all I'm using an Object Oriented approach to solve this problem. When I try to instantiate and object of the class and launch a method of the latter e.g BluetoothServer.Start() this returns null. Bolded characters indicate where exceptions are usually returned.an exception returned by NetBeans is:
TRACE: <at java.lang.NullPointerException: 0>, startApp threw an Exception
java.lang.NullPointerException: 0
at BluetoothServer.sendMessage(BluetoothServer.java:75)
at PhoneInfo.startApp(PhoneInfo.java:63)
at javax.microedition.midlet.MIDletTunnelImpl.callStartApp(), bci=1
at com.sun.midp.midlet.MIDletPeer.startApp(), bci=7
at com.sun.midp.midlet.MIDletStateHandler.startSuite(), bci=269
at com.sun.midp.main.AbstractMIDletSuiteLoader.startSuite(), bci=52
at com.sun.midp.main.CldcMIDletSuiteLoader.startSuite(), bci=8
at com.sun.midp.main.AbstractMIDletSuiteLoader.runMIDletSuite(), bci=161
at com.sun.midp.main.AppIsolateMIDletSuiteLoader.main(), bci=26
java.lang.NullPointerException: 0
at BluetoothServer.sendMessage(BluetoothServer.java:75)
at PhoneInfo.startApp(PhoneInfo.java:63)
at javax.microedition.midlet.MIDletTunnelImpl.callStartApp(), bci=1
at com.sun.midp.midlet.MIDletPeer.startApp(), bci=7
at com.sun.midp.midlet.MIDletStateHandler.startSuite(), bci=269
at com.sun.midp.main.AbstractMIDletSuiteLoader.startSuite(), bci=52
at com.sun.midp.main.CldcMIDletSuiteLoader.startSuite(), bci=8
at com.sun.midp.main.AbstractMIDletSuiteLoader.runMIDletSuite(), bci=161
at com.sun.midp.main.AppIsolateMIDletSuiteLoader.main(), bci=26
My Source code:
BluetoothServer.java:
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.io.StreamConnectionNotifier;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
public class BluetoothServer
{
private String UUID;
private String conProtocol;
private String conTarget;
private String serviceName;
private String serverConnection;
private StreamConnectionNotifier scn;
private StreamConnection sc;
private Thread t1;
private Alert alert;
private DataOutputStream out;
public BluetoothServer()
{
UUID = "12345678901234567890123456789012";
conProtocol = "btspp"; //service profile protocol
conTarget = "localost"; //since this is the server, localhost is our best bet
serviceName = "TopSecret";
serverConnection = conProtocol+"://"+conTarget+":"+UUID+";"+"name="+serviceName;
}
public void startServer()
{
try
{
scn = (StreamConnectionNotifier) Connector.open(serverConnection);
sc = (StreamConnection) scn.acceptAndOpen();
t1 = new Thread((Runnable) this);
t1.start();
}
catch (IllegalThreadStateException threadEx)
{
showAlert("Thread exception");
}
catch (Exception e)
{
showAlert("StreamConnectionNotifier failed!");
}
}
public void stopServer()
{
try
{
if (scn != null)
scn.close();
sc.close();
}
catch (Exception e)
{
showAlert("Could not stop server");
}
}
public void startStreaming()
{
}
public void sendMessage(String msg) throws IOException
{
try {
out = sc.openDataOutputStream();
out.writeChars(msg);
out.flush();
out.close();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
public void showAlert(String message)
{
alert = new Alert("Alert",message,null,AlertType.INFO);
}
}
--------------------------------------------------------------
PhoneInfo.java (Main MIDLet)
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.IOException;
import javax.microedition.location.LocationException;
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);
BluetoothServer myServer = new BluetoothServer();
myServer.startServer();
try {
myServer.sendMessage("hello");
} catch (IOException ex) {
ex.printStackTrace();
}
}
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)
{
}
}
}
Please help me out there. I need some guidance pros.

Reply With Quote


