File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes btSendFile Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "btSendFile" Watch "btSendFile" New topic
Author

btSendFile

Zaafir Barahim
Ranch Hand

Joined: Dec 12, 2006
Posts: 36
I have been using BtSendFile java code and it is very good. i have removed some classes and modified the code and iv removed the interface to get an automatic detection and sending of message. i want to further modified the code to be able to send a specific text file to all the detected bluetooth devices without having to click on any button or without getting the chance to choose other fils. PLease help me out to modify the code further and i don't want a Swing interface. Thanks in advance
The directory of my text file which i will send to everyone is at "C:\BTSendFile\University mobile Guide System.txt";

Here is my modified code:

package com.btsendfile;
import javax.bluetooth.*;
import java.util.Hashtable;

import java.io.IOException;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.swing.JFileChooser;
import de.avetana.javax.obex.*;
import de.avetana.obexsolo.*;



public class BluetoothSensorSystem implements DiscoveryListener {


LocalDevicelocal;
private DiscoveryAgentdiscoveryAgent;
private HashtablebluetoothDevices= new Hashtable();
private String btAddress;


public void printString(String s) {

System.out.println(s);

}

public void bluetoothDiscovery() {

LocalDevice localDevice;

try {

localDevice = LocalDevice.getLocalDevice();

} catch (BluetoothStateException e) {

printString("BluetoothStateException: " + e);
return;

}

discoveryAgent = localDevice.getDiscoveryAgent();

RemoteDevice devices[] = null;

try {

if (devices != null) {

for (int i = 0; i < devices.length; i++) {
bluetoothDevices.put(devices[i].getFriendlyName(false), devices[i]);

printString("Device (cached): " + devices[i].getFriendlyName(false) + " " + devices[i].getBluetoothAddress());
}
}


if (devices != null) {

for (int i = 0; i < devices.length; i++) {

bluetoothDevices.put(devices[i].getFriendlyName(false), devices[i]);

printString("Device (cached): " + devices[i].getFriendlyName(false) + " " + devices[i].getBluetoothAddress());

}
}

} catch (IOException e) {
printString("Exception (b2): " + e);
}

try {

discoveryAgent.startInquiry(DiscoveryAgent.GIAC, this);

} catch (BluetoothStateException e) {
printString("Exception (b3): " + e);
}
printString("___________________________________________________________________________");
printString("***UNIVERSITY MOBILE GUIDE SENSOR SYSTEM.......... SCANNING...***");
printString("***Please Wait.***");
printString("***Please Wait..***");
printString("***Please Wait...***");
printString("___________________________________________________________________________");
//printString("return from bluetoothDiscovery()");

}


// ************************************************
// Implement BT DiscoveryListener

public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {

try {
printString("___________________________________________________________________________");
printString("***FOUND***");
printString("___________________________________________________________________________");
printString("***Device Name: " + btDevice.getFriendlyName(true)+"***");
printString("***Device Address: " + btDevice.getBluetoothAddress()+"***");
btAddress = btDevice.getBluetoothAddress();


} catch (Exception e) {
printString("Exception (b4): " + e);
}

}


public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
}


public void serviceSearchCompleted(int transID, int respCode) {
}


public void inquiryCompleted(int discType) {
discType=discType+1;

printString("***inquiryCompleted! " + discType+"***");
printString("___________________________________________________________________________");
OBEXSendFile();
}


public String getBtAddress() {
return btAddress;
}


public void setBtAddress(String btAddress) {
this.btAddress = btAddress;
}




// ************************************************
// OBEX

public void OBEXSendFile() {

try {
JFileChooser fc = new JFileChooser();
fc.addChoosableFileFilter(new TxtMyFilter());

int result = fc.showOpenDialog(null);
//System.out.println("result "+result + ":");

String adr = btAddress;
printString("___________________________________________________________________________");
System.out.println("***Bt adr : "+ adr+"***");
System.out.println("File: "+fc.getSelectedFile()+"");
printString("___________________________________________________________________________");


switch (result) {
case JFileChooser.APPROVE_OPTION:

File selFile = fc.getSelectedFile();
//String selFile = "C:\BTSendFile\University mobile Guide System.txt";
//System.out.println("selFile path \n"+ selFile);
//System.out.println("selFile name \n"+ selFile.getName());

String adrProto = "btgoep://"+ adr +":9";
//System.out.println("adrProto :\n"+ adrProto);



ClientSession cs = (ClientSession) OBEXConnector.open(adrProto);
//System.out.println("opening");
HeaderSet hs = cs.connect(cs.createHeaderSet());

//System.out.println("created header set");
byte text[] = getBytesFromFile(selFile);
hs.setHeader(HeaderSet.NAME, selFile.getName());
hs.setHeader(HeaderSet.TYPE, "text");

//System.out.println("putting....");
Operation po = cs.put(hs);


printString("___________________________________________________________________________");
System.out.println("***Sent text message successfully...***");
printString("___________________________________________________________________________");
po.openOutputStream().write(text);
po.close();
cs.disconnect(null);
cs.close();

System.out.println("***closed...***");
printString("___________________________________________________________________________");
// Approve (Open or Save) was clicked
break;


case JFileChooser.CANCEL_OPTION:
// Cancel or the close-dialog icon was clicked
break;


case JFileChooser.ERROR_OPTION:
// The selection process did not complete successfully
break;
}
}


catch (Throwable e) {
e.printStackTrace();
}
}


// Returns the contents of the file in a byte array.
public static byte[] getBytesFromFile(File file) throws IOException {
InputStream is = new FileInputStream(file);

// Get the size of the file
long length = file.length();

// You cannot create an array using a long type.
// It needs to be an int type.
// Before converting to an int type, check
// to ensure that file is not larger than Integer.MAX_VALUE.

if (length > Integer.MAX_VALUE) {
System.out.println("File is too large\n");
// File is too large
}

// Create the byte array to hold the data
byte[] bytes = new byte[(int)length];

// Read in the bytes
int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
offset += numRead;
}

// Ensure all the bytes have been read in
if (offset < bytes.length) {
throw new IOException("Could not completely read file "+file.getName());
}

// Close the input stream and return bytes
is.close();
return bytes;
}

class TxtMyFilter extends javax.swing.filechooser.FileFilter {
public boolean accept(File file) {
String filename = file.getName();
return filename.endsWith(".txt");
}

public String getDescription() {
return "*.txt";
}
}



// ************************************************
//MAIN

public static void main(String[] args) {

BluetoothSensorSystem bluetoothDevices = new BluetoothSensorSystem();
bluetoothDevices.bluetoothDiscovery();
//bluetoothDevices.OBEXSendFile();

}

}
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39547
    
  27
i want to further modified the code to be able to send a specific text file to all the detected bluetooth devices without having to click on any button or without getting the chance to choose other fils.


The code is too long to read and try to figure out what it's doing, epecially as it doesn't use CODE tags, which makes it hard to read. Tell us what it does, what you want it to do, and at which point you're stuck implementing it.


Ping & DNS - updated with new look and Ping home screen widget
Zaafir Barahim
Ranch Hand

Joined: Dec 12, 2006
Posts: 36
This code does send the message. iwhen i run this code i get a graphical interface to select the file which i want to send and after selecting the file thefile can be send but i don't want to send it like this. i want to write a code which already select a specific file and when i run the code which detects bluetooth device, it sends the text message which iv selected before to the bluetooth device automatically. i don't want the user to select a file.


the code for sending the file is as follows and the following classes are used and bear in mind that im using the Stack of BTSendFile.



// OBEX

public void OBEXSendFile() {

try {
JFileChooser fc = new JFileChooser();
fc.addChoosableFileFilter(new TxtMyFilter());

int result = fc.showOpenDialog(null);
//System.out.println("result "+result + ":");

String adr = btAddress;
printString("___________________________________________________________________________");
System.out.println("*** Bt adr : "+ adr+" ***");
System.out.println("File: "+fc.getSelectedFile()+" ");
printString("___________________________________________________________________________");


switch (result) {
case JFileChooser.APPROVE_OPTION:

File selFile = fc.getSelectedFile();
//String selFile = "C:\BTSendFile\University mobile Guide System.txt";
//System.out.println("selFile path \n"+ selFile);
//System.out.println("selFile name \n"+ selFile.getName());

String adrProto = "btgoep://"+ adr +":9";
//System.out.println("adrProto :\n"+ adrProto);



ClientSession cs = (ClientSession) OBEXConnector.open(adrProto);
//System.out.println("opening");
HeaderSet hs = cs.connect(cs.createHeaderSet());

//System.out.println("created header set");
byte text[] = getBytesFromFile(selFile);
hs.setHeader(HeaderSet.NAME, selFile.getName());
hs.setHeader(HeaderSet.TYPE, "text");

//System.out.println("putting....");
Operation po = cs.put(hs);


printString("___________________________________________________________________________");
System.out.println("*** Sent text message successfully... ***");
printString("___________________________________________________________________________");
po.openOutputStream().write(text);
po.close();
cs.disconnect(null);
cs.close();

System.out.println("*** closed... ***");
printString("___________________________________________________________________________");
// Approve (Open or Save) was clicked
break;


case JFileChooser.CANCEL_OPTION:
// Cancel or the close-dialog icon was clicked
break;


case JFileChooser.ERROR_OPTION:
// The selection process did not complete successfully
break;
}
}


catch (Throwable e) {
e.printStackTrace();
}
}


// Returns the contents of the file in a byte array.
public static byte[] getBytesFromFile(File file) throws IOException {
InputStream is = new FileInputStream(file);

// Get the size of the file
long length = file.length();

// You cannot create an array using a long type.
// It needs to be an int type.
// Before converting to an int type, check
// to ensure that file is not larger than Integer.MAX_VALUE.

if (length > Integer.MAX_VALUE) {
System.out.println("File is too large\n");
// File is too large
}

// Create the byte array to hold the data
byte[] bytes = new byte[(int)length];

// Read in the bytes
int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
offset += numRead;
}

// Ensure all the bytes have been read in
if (offset < bytes.length) {
throw new IOException("Could not completely read file "+file.getName());
}

// Close the input stream and return bytes
is.close();
return bytes;
}

class TxtMyFilter extends javax.swing.filechooser.FileFilter {
public boolean accept(File file) {
String filename = file.getName();
return filename.endsWith(".txt");
}

public String getDescription() {
return "*.txt";
}
}
Sainese Huang
Greenhorn

Joined: Jan 31, 2007
Posts: 12
Hi,Did you run with BTSendFile successfully? I failed to send any file from PC to SonyEriccsson W700c,The operation system on my PC is Windows XP SP2. and I traced the exception as follows :

return from bluetoothDiscovery()
deviceDisc: W700c
deviceAddress: 001813ca3d7f
btDevices :
001813ca3d7f
inquiryCompleted! 0
result 0:
Bt adr :
001813ca3d7f
C:\response.txt
selFile path
C:\response.txt
selFile name
response.txt
adrProto :
btgoep://001813ca3d7f:9
[WARNING] You are using BlueCove Connector [WARNING]
java.io.IOException: Failed to connect socket
at com.intel.bluetooth.BluetoothPeer.connect(Native Method)
at com.intel.bluetooth.BluetoothConnection.<init>(BluetoothConnection.ja
va:52)
at javax.microedition.io.Connector.open(Connector.java:125)
at de.avetana.obexsolo.OBEXConnector.open(OBEXConnector.java:103)
at com.btsendfile.OBEXSendFile.<init>(OBEXSendFile.java:39)
at com.btsendfile.JListBtDevices.jButton2ActionPerformed(JListBtDevices.
java:89)
at com.btsendfile.JListBtDevices.access$100(JListBtDevices.java:13)
at com.btsendfile.JListBtDevices$2.actionPerformed(JListBtDevices.java:5
2)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:18
49)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.jav
a:2169)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel
.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258
)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonL
istener.java:234)
at java.awt.Component.processMouseEvent(Component.java:5488)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3093)
at java.awt.Component.processEvent(Component.java:5253)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212
)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)

at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1766)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchTh
read.java:234)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

Can you help me ?

Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

"xparticlePP PPL"

Welcome to JavaRanch!

Please update your display name so it complies with our Naming Policy. You can do this here.

Thanks!


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
hidayah Ishak
Greenhorn

Joined: Jan 31, 2007
Posts: 9
hi,can I ask you something?actually I am already download the Java sourcecode for Bluetooth Send File. The problem is,when I click button 'Discover Device' from the interface, it can't detect any bluetooth device in range. Can you teach me how to make that program fully function,is it needed to add anything to make that program fully function? I hope you can help me
Sainese Huang
Greenhorn

Joined: Jan 31, 2007
Posts: 12
Hi,hidayah Ishak ,You should have a usb dongle and your bluetooth stack should be windows stack,then

1. Plug USB bluetooth dongle supported by MS Windows XP SP2.
2. Copy the file lib\intelbth.dll to your C:\Windows\System32 directory
3. Run app by command : %BTSENDFILE_HOME%\runBTSendFile.bat
4. Bluetooth application discover bluetooth devices and choose a device to send file to

I can discovery devices but failed to send file to the found device.Somebody can help me ?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

I'm thinking Sainese is the one who used to be called "xparticle, etc." I'm sorry, but your display name is still not acceptable: we need a real-sounding first and last name, with a space between. Please give it another go -- thanks.


[Jess in Action][AskingGoodQuestions]
hidayah Ishak
Greenhorn

Joined: Jan 31, 2007
Posts: 9
hi Sainese , thank you for reply my message.. actually I am already follow that step before this but I still cannot discover device, is it possible that the type of bluetooth dongle that I have used cause the problem? when I click the 'Discover Device' button, this line 'return from bluetooth discovery() inquiryCompleted! 7' was appear at MSDOS.. please, I really need your help
hidayah Ishak
Greenhorn

Joined: Jan 31, 2007
Posts: 9
hi Sainese , thank you for reply my message.. actually I am already follow that step before this but I still cannot discover device, is it possible that the type of bluetooth dongle that I have used cause the problem? when I click the 'Discover Device' button, this line 'return from bluetooth discovery() inquiryCompleted! 7' was appear at MSDOS.. please, I really need your help
hidayah Ishak
Greenhorn

Joined: Jan 31, 2007
Posts: 9
hi Sainese , thank you for reply my message.. actually I am already follow that step before this but I still cannot discover device, is it possible that the type of bluetooth dongle that I have used cause the problem? when I click the 'Discover Device' button, this line 'return from bluetooth discovery() inquiryCompleted! 7' was appear at MSDOS.. please, I really need your help
Sainese Huang
Greenhorn

Joined: Jan 31, 2007
Posts: 12
Hi,it is possible that the type of bluetooth dongle that you have used not supported by WindowsXP. you can try to send a file through the bluetooth driver provided by windows XP SP2,if you send successfully ,your dongle and stack is in right position.so,I wander if you have installed other bluetooth driver such as widcomm or others,you can uninstall other bluetooth driver or try with BTSendFile on other computer without any other thirdparty bluetooth driver.I met the same problem when I run on my computer with widcomm installed.I run successfully,when I run again on another computer without widcomm.good luck
hidayah Ishak
Greenhorn

Joined: Jan 31, 2007
Posts: 9
hi..thank you for your reply, I am using bluetooth USB adapter ES-338 and before this I dont have installed any bluetooth adapter so, this is consider as first bluetooth adapter installed in my computer and I think there are no redundancy here. I am also try to run this program at my friend's laptop, but it also failed to discover devices..is it you modified or change any code from this program to make it run?
Sainese Huang
Greenhorn

Joined: Jan 31, 2007
Posts: 12
No,I didn't change any file in BTSendFile,there are two reason that make it failed to discover devices ,first ,your bluetooth adapter is not supported by winxp ,then ,perhaps your bluetooth device...I used SonyEricsson W700c to test.all the result displayed in the upper reply.It discovered normally but failed to send file to mobile
Sainese Huang
Greenhorn

Joined: Jan 31, 2007
Posts: 12
I think both of us need the auther's help,If you mail to the author and get some response from him ,please let me know how to send a file .I posted a question on sourceforge's forum,and hope that the author can answer to me.
Sainese Huang
Greenhorn

Joined: Jan 31, 2007
Posts: 12
I think both of us need the author's help,If you mail to the author and get some response from him ,please let me know how to send a file .I posted a question on sourceforge's forum,and hope that the author can answer to me.
hidayah Ishak
Greenhorn

Joined: Jan 31, 2007
Posts: 9
hi..i am really sorry to bother you again.i am sorry to say that i still stuck .i followed as per mention in readme.txt yet i faced the same problem.it stated there-->return from bluetoothDiscovery<> inquiryCompleted!7..(it looks like the software do not discover the device at all..)these are the step that i do:
1. Plug USB bluetooth dongle supported by MS Windows XP SP2.
2. Copy the file lib\intelbth.dll to your C:\Windows\System32 directory
3. Run app by command : %BTSENDFILE_HOME%\runBTSendFile.bat (rename runBTSendFile.bat1 -> runBTSendFile.bat)

is that okay if i just click on runBTSendFile.bat provided in BTSendFile folder?what i understand with number 3 step is you just run batch file provided

i already tried with motorola L6, sony ericsson K510i, samsung SGH D510
if you don't mind,could you give me a step by step explaination? i am looking forward for your instance reply.thank you
Sainese Huang
Greenhorn

Joined: Jan 31, 2007
Posts: 12
Hi,I did just as you did,the third step is meanless!
Sorry,my English is very poor.Now I show all step I have followed:
1,I plug a USB dongle to my computer,but got the wrong result just as you have got.
2,I search for reason by google and other search site,but have no solution.
3,I found I installed widcomm driver on my computer and uninstalled it ,but failed again.
4,I tested on another computer,Now it discovery devices sucessfully,and list the device on the List object of BTSendFile.
That's all I've done ,I think don't know what's wrong with your testing,Can you send your BTSendFile archive in zip to me?let try on my computer.my Email:sainese@sina.com.cn
I don't know what can I do for you ,I don't think BTSendFile can send a file to mobile!
Sainese Huang
Greenhorn

Joined: Jan 31, 2007
Posts: 12
Hi,I did just as you did,the third step is meanless!
Sorry,my English is very poor.Now I show all step I have followed:
1,I plug a USB dongle to my computer,but got the wrong result just as you have got.
2,I search for reason by google and other search site,but have no solution.
3,I found I installed widcomm driver on my computer and uninstalled it ,but failed again.
4,I tested on another computer,Now it discovery devices sucessfully,and list the device on the List object of BTSendFile.
That's all I've done ,I think don't know what's wrong with your testing,Can you send your BTSendFile archive in zip to me?let try on my computer.my Email:sainese@sina.com.cn
I don't know what can I do for you ,I don't think BTSendFile can send a file to mobile!
Sainese Huang
Greenhorn

Joined: Jan 31, 2007
Posts: 12
Hi,All,I'v got the solution by changing the service from 9 to 5,or you can test from 1 to 10 .I sent the file successfully with service 5 at last.just to change bteogp://btaddress:9 to bteogp://btaddress:5!
Sainese Huang
Greenhorn

Joined: Jan 31, 2007
Posts: 12
HI,hidayah Ishak,
you can search by keyword BTSendFile in http://forum.vietcovo.com/index.php?topic=43.0 you can find many questions and answers about btsendfile.the author answer questions frequently!
hidayah Ishak
Greenhorn

Joined: Jan 31, 2007
Posts: 9
hi.. I have send the zip file to you and I am waiting for your reply and anyway thank you very much because you are really helping me
hidayah Ishak
Greenhorn

Joined: Jan 31, 2007
Posts: 9
hi Sainese Huang.. thank you so much because now i can send file using a BTSendFile.. I am really happy because you are giving an instruction step by step until I can send the file.. thank you so much!
Sainese Huang
Greenhorn

Joined: Jan 31, 2007
Posts: 12
It's great! then what's wrong with your testing in the last two days?
hidayah Ishak
Greenhorn

Joined: Jan 31, 2007
Posts: 9
hi Sainese Huang ..actually when I buy the bluetooth dongle last time, I am installed the driver that come together with the bluetooth dongle, I dont know that the bluetooth dongle can be use without the software that come together so when the software was unistalled, that program can detect all the device in range.. But I want to ask you something, is it all the list bluetooth in range only will appear in MSDOS and at the java interface only one address will be appear? Is it possible to make all Bluetooth device listed at Java interface
Sainese Huang
Greenhorn

Joined: Jan 31, 2007
Posts: 12
Sorry,I don't try that now,But I reviewed the source file,I think this is a programming problem,You can change some code in the file to do that ,It should be simple to do that.After I sent file successfully,I continue to programming for my another project with no relation to BTSendFile.
Sweta Shah
Greenhorn

Joined: May 22, 2008
Posts: 5
Hi all,

I tried out this btsendFile code and after some mind boggling, i am able to list all bluetooth devices in range at a time and also send a specific selected file to all the devices one by one serially without user intervention. Everything done by the program...! It was fun and nice experience to work with this code...Thanks to the authors...!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36482
    
  16
Welcome to JavaRanch

Unfortunately it's such an old post, some of the original authors are no longer active on JavaRanch to read your reply. Look at this dreadfully-named FAQ.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: btSendFile
 
Similar Threads
unable to preserve newline
Cannot Figure Out This Syntax Error
Writing Hex Values to a file
PERIODIC BLUETOOTH SCAN and OBEX send
how to insert carriage return