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();
}
}
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();
}
}