public class MyBluetoothMIDlet extends MIDlet implements CommandListener,DiscoveryListener {
Form mainForm = null;
Command exitCmd = null;
Command doneCmd = null;
Command discoverCmd = null;
Display display = null;
List deviceList = null;
public void startApp()
{
display = Display.getDisplay(this);
mainForm = new Form("Bluetooth App");
deviceList = new List("Devices Found",List.IMPLICIT);
exitCmd = new Command("Exit",Command.EXIT,0);
discoverCmd = new Command("Search",Command.SCREEN,0);
mainForm.setCommandListener(this);
mainForm.addCommand(exitCmd);
mainForm.addCommand(discoverCmd);
doneCmd = new Command("Done",Command.EXIT,0);
deviceList.addCommand(doneCmd);
deviceList.setCommandListener(this);
StringItem item = new StringItem("Bluetooth Version:",LocalDevice.getProperty("bluetooth.api.version"));
mainForm.append(item);
display.setCurrent(mainForm);
}
public void pauseApp()
{
}
public void destroyApp(boolean flag)
{
System.out.println("Exited App");
}
public void commandAction(Command c, Displayable d)
{
if (c.getCommandType() == Command.EXIT)
{
destroyApp(true);
notifyDestroyed();
}
else
{
try {
DiscoveryAgent discoverAgent = LocalDevice.getLocalDevice().getDiscoveryAgent();
discoverAgent.startInquiry(DiscoveryAgent.GIAC, this);
display = Display.getDisplay(this);
display.setCurrent(deviceList);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
public void serviceSearchCompleted(int a, int b)
{
}
public void deviceDiscovered(RemoteDevice remoteDevice, DeviceClass dev)
{
try
{
String btAddress = remoteDevice.getBluetoothAddress();
String fName = remoteDevice.getFriendlyName(true);
System.out.println("Bluetooth Address:"+btAddress);
System.out.println("Friendly Name:"+fName);
String msg = "";
if(fName==null || fName.equals(""))
{
msg = btAddress;
}
else
{
msg = fName;
}
deviceList.append(msg, null);
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void inquiryCompleted(int type)
{
Alert alert = new Alert("Information","Scan Completed!",null,null);
display.setCurrent(alert, deviceList);
System.out.println("Enquiry Completed."+type);
}
public void servicesDiscovered(int c, ServiceRecord servRec[])
{
}
}
0 comments:
Post a Comment