Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

PERIODIC BLUETOOTH SCAN and OBEX send

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
hi i have been able to detect bluetooth device and send the bluetooth device a specific message. Now i want it to permantly do these tasks, that is for every 1 minute the code is executed again and again . Please help me out and its vvvvvvvvvvvvvvvvvvvvvvvvv urgent for my final year project. Thanks in advance.
my code are as follows:



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

}

}
 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi,
Use java.util.TimerTask and schedule your program to execute at desired intervals.

Hope this helps
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
This reads very much like a duplicate. In which case I'm going to close this one. Please continue any discusion in your other post.
[ January 08, 2007: Message edited by: Paul Sturrock ]
 
When you have exhausted all possibilities, remember this: you haven't - Edison. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic