Hi,
Can someone guide for transferring files mainly images and jar through bluetooth. I think one may have to use javax.obex. I know basic bluetooth programming using JSR 82.
Any sort of help would be appreciated.
Hi,
Can someone guide for transferring files mainly images and jar through bluetooth. I think one may have to use javax.obex. I know basic bluetooth programming using JSR 82.
Any sort of help would be appreciated.
Sunil
Mobile Application Developer
I used the following code but it is giving exception when we start searching for service. I hope some one will reply as early as possible. I am giving the Underline and bold effect on the line which used to execute before exception.
import javax.bluetooth.*;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;
import javax.microedition.io.Connector;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Image;
import javax.microedition.midlet.MIDlet;
import javax.obex.ClientSession;
import javax.obex.HeaderSet;
import javax.obex.Operation;
public class BluetoothBrowser extends MIDlet implements DiscoveryListener
{
//Diomond midlet = null;
LocalDevice localdevice = null;
DiscoveryAgent agent = null;
boolean flag=true;
boolean keyflag=true;
boolean StringFlag[];
boolean disconnectFlag[];
String keyArray[];
TimerWork tw = null;
Timer t = null;
int counter=0;
int alertcount = 0;
int totalDevFound = 0;
String disconnectDevName = null;
String addr[]=new String[4];
int keyIndex=0;
String mainData;
private static String connectionURL=null;
private static Vector vecDevices=new Vector();
//private static Object lock = new Object();
public void startApp()
{
}
public void destroyApp(boolean a)
{
}
/* public void notifyDestroyed(boolean a)
{
}*/
public void pauseApp()
{
}
public BluetoothBrowser()
{
try
{
LocalDevice localDevice = LocalDevice.getLocalDevice();
System.out.println("Address: "+localDevice.getBluetoothAddress());
System.out.println("Name: "+localDevice.getFriendlyName());
//find devices
DiscoveryAgent agent = localDevice.getDiscoveryAgent();
System.out.println("Starting device inquiry...");
agent.startInquiry(DiscoveryAgent.GIAC, this);
int i;
synchronized(this)
{
this.wait();
}
int deviceCount=vecDevices.size();
if(deviceCount <= 0){
System.out.println("No Devices Found .");
}
else{
//print bluetooth device addresses and names in the format [ No. address (name) ]
System.out.println("Bluetooth Devices: ");
for (i = 0; i <deviceCount; i++) {
RemoteDevice remoteDevice=(RemoteDevice)vecDevices.elementAt(i);
System.out.println((i+1)+". "+remoteDevice.getBluetoothAddress()+" ("+remoteDevice.getFriendlyName(false)+")");
}
}
System.out.print("Choose the device to search for Obex Push service : ");
//String chosenIndex=bReader.readLine();
int index=1;
//check for obex service1
RemoteDevice remoteDevice=(RemoteDevice)vecDevices.elementAt(index-1);
UUID[] uuidSet = new UUID[1];
uuidSet[0]=new UUID("1105",true);
System.out.println("\nSearching for service...");
if(remoteDevice==null)
{
System.out.println("null");
}
//The last line before exception
agent.searchServices(null,uuidSet,remoteDevice,this);
System.out.println("after searh");
try {
synchronized(this){
this.wait();
}
}
catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("after catch");
if(connectionURL==null){
System.out.println("Device does not support Object Push.");
}
else
{
sendData();
}
// this.midlet = midlet;
/*for(int i=0;i<addr.length;i++)
addr[i]="";*/
}
catch(Exception e)
{
System.out.println("Exception="+e.toString());
}
}
public void searchDevices()
{
//Get Local Device and start Inquiry.
try
{
agent = LocalDevice.getLocalDevice().getDiscoveryAgent();
agent.startInquiry(DiscoveryAgent.GIAC, this);
}
catch (Exception ex)
{
System.out.println("In Catch");
//display_alert("Error", ex.toString());
}
}
public void sendData()
{
byte data[]=null;
try
{
try
{
InputStream in = this.getClass().getResourceAsStream("/rail.png");
Image img=Image.createImage(in);
// mainForm.append(img);
data=new byte[in.available()];
System.out.println("after img");
in.read(data);
}
catch(Exception e)
{
System.out.println("after img in cache");
}
ClientSession conn=(ClientSession)Connector.open(connectionURL);
HeaderSet hs=conn.createHeaderSet();
conn.connect(hs);
hs.setHeader(HeaderSet.NAME,"a.jpg");
Operation op=conn.put(hs);
DataOutputStream dos=op.openDataOutputStream();
dos.write(data);
dos.flush();
dos.close();
conn.close();
}
catch(Exception e)
{
System.out.println("IN catche");
}
}
public void stopSearchingDevices()
{
agent.cancelInquiry(this);
}
public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
//add the device to the vector
if(!vecDevices.contains(btDevice)){
vecDevices.addElement(btDevice);
}
}
public void inquiryCompleted(int arg0)
{
//sendData();
synchronized(this){
this.notify();
}
}
public void serviceSearchCompleted(int transID, int respCode) {
System.out.println("service search complete");
synchronized(this)
{
System.out.println("Service search completed");
this.notifyAll();
}
}
public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
System.out.println("service discovered");
if(servRecord!=null && servRecord.length>0){
connectionURL=servRecord[0].getConnectionURL(0,false);
}
}
class TimerWork extends TimerTask
{
public void run()
{
try
{
//inquiry();
keyflag=false;
searchDevices();
}
catch(Exception e)
{
System.out.println("In Catch="+ e);
}
}
}
}
I copied that code into NetBeans, built and compiled it then sent the jar to my phone (through windows)
The start of it works fine but it tries to send a file to any phone within the Bluetooth range.
But it does not send the file, the application closes and cancels the file transfer.
I changed the location of the picture to suit my phone also, on my other phone it confirms this when it displays
"do you want to accept this file... a.gif"
but when i choose yes on the other phone the BluetoothBrowser closes.