Skip to main content

Bluetooth help

2 replies [Last post]
rohandvora
Offline
Joined: 2010-01-04

Hello.I am a new developer and trying to learn Bluetooth functions.I intend to create a bluetooth chat application between 3 or more devices.Can anyone help me to start??

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
rohandvora
Offline
Joined: 2010-01-04

The devices are never searched in the first instance.I konw that I am supposed to use synchronized() function,but where?

rohandvora
Offline
Joined: 2010-01-04

I have used LWUIT to create form and add components.I have created the basic strucure.The problem is that it does not search for any devices.Look at the code and help me.

import javax.microedition.midlet.*;
import javax.bluetooth.*;
import com.sun.lwuit.Form;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.List;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.Label;
import com.sun.lwuit.Command;
import com.sun.lwuit.Display;
import com.sun.lwuit.geom.Dimension;
import com.sun.lwuit.TabbedPane;
import com.sun.lwuit.util.Resources;
import com.sun.lwuit.plaf.UIManager;
import com.sun.lwuit.animations.CommonTransitions;
import java.util.Vector;

public class BluetoothMidlet extends MIDlet
{
//Variables
private Form f;
private Label lb;
private Label lb2;
private Command ExitCom = new Command("Exit",1);
private Command Search = new Command("Search",2);

//Bluetooth Variables
private LocalDevice dvc;
Vector device = new Vector();
private String[] BlueNames;
public void startApp()
{
Display.init(this);
theme("Black");
f=new Form();
createLabel(lb,"Welcome",Label.CENTER,new Dimension(300,20));
CommandListener(f);
CommandAdd(ExitCom);
CommandAdd(Search);
f.show();
}

public void theme(String thm)
{
try
{
Resources r = Resources.open("/res/myresources.res");
UIManager.getInstance().setThemeProps(r.getTheme(thm));
Display.getInstance().getCurrent().refreshTheme();
}
catch(Exception e)
{
}
}

public void createLabel(Label label,String body,int allign,Dimension size)
{
label=new Label();
label.setText(body);
label.setAlignment(allign);
label.setPreferredSize(size);
if(!f.contains(label))
f.addComponent(label);
}

public void CommandListener(Form form)
{
form.addCommandListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
if(evt.getCommand().equals(ExitCom))
{
destroyApp(true);
notifyDestroyed();
}
else
if(evt.getCommand().equals(Search))
{
bluetooth();
}
}
});
}

public void CommandAdd(Command cmd)
{
f.addCommand(cmd);
}

public void bluetooth()
{
try
{
Dialog.show("Searching","Nearby bluetooth devices are being searched.","Ok",null);
dvc = LocalDevice.getLocalDevice();
DiscoveryAgent agent = dvc.getDiscoveryAgent();
agent.startInquiry(DiscoveryAgent.GIAC, new DiscoveryListener(){
public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod)
{
try
{
device.addElement(btDevice.getFriendlyName(true));
}
catch(Exception e)
{
}
}
public void servicesDiscovered(int transID, ServiceRecord[] servRecord)
{
}
public void serviceSearchCompleted(int transID, int respCode)
{
}
public void inquiryCompleted(int discType)
{
switch(discType)
{
case DiscoveryListener.INQUIRY_COMPLETED:Dialog.show("Note", "Search completed", "Ok", null);
break;
case DiscoveryListener.INQUIRY_ERROR:Dialog.show("Note", "Search error!!", "ok", null);
break;
case DiscoveryListener.INQUIRY_TERMINATED:Dialog.show("Note", "Search terminated", "Ok", null);
break;
}
}
});
}
catch(Exception e)
{
}
if(device.size()!=0)
{
BlueNames=new String[device.size()];
for(int i=0;i {
BlueNames[i]=device.elementAt(i).toString();
createLabel(lb2,BlueNames[i],Label.CENTER,new Dimension(300,20));
}
}
else
Dialog.show("Results", "No device found", "Ok", null);
}

public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
}