1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
|
import java.io.*;
import java.util.*;
import javax.bluetooth.*;
import javax.bluetooth.UUID;
import javax.microedition.io.*;
import xml.*;
public class BTAnanas implements DiscoveryListener{
// Connection streams. These streams are only used with BluetoothStream connections (RFCOMN)
//private InputStream is = null;
//private OutputStream os = null;
private Connection streamCon = null;
private Connection notify=null;
private DiscoveryAgent m_agent;
private LocalDevice m_local;
private HashMap hrecord;
private Vector record;
private HashMap hdevice;
private Vector device;
private RemoteDevice[] devList;
private int maxServiceSearches = 0;
private int serviceSearchCount;
public BTAnanas(String data) throws Exception {
hdevice = new HashMap();
device = new Vector();
hrecord = new HashMap();
record = new Vector();
//super ("Avetana Bluetooth Utility");
try {
// Initialize the java stack.
initStack();
}catch(Exception ex) {
ex.printStackTrace();
System.out.println("Erreur de merde!!!" + ex.getMessage());
//System.exit(0);
}
}
public void initStack() throws Exception {
m_local=LocalDevice.getLocalDevice();
m_agent=m_local.getDiscoveryAgent();
try {
maxServiceSearches = Integer.parseInt(
LocalDevice.getProperty("bluetooth.sd.trans.max"));
} catch (NumberFormatException e) {
System.out.println("General Application Error");
System.out.println("\tNumberFormatException: " + e.getMessage());
}
}
public boolean ConnectAndSynchronize( String demande, String data ) {
DataOutputStream os = null;
DataInputStream is = null;
//InputStream s = null;
StreamConnection con = null;
String message="";
BufferedReader IN=null;
String fichier;
ServiceRecord serv = (ServiceRecord) hrecord.get(demande);
try {
String serverUrl = serv.getConnectionURL(0,false);
System.out.println ("\n"+serverUrl);
Lecture l1 = new Lecture();
fichier = l1.lire("prylosPCtoGSM2.xml");
con =(StreamConnection)Connector.open(serverUrl);
System.out.println ("\napres con");
Thread.sleep(1000);
/*
* Reception du fichier du GSM
*/
is = con.openDataInputStream();
System.out.println ("connected for receive\n");
//while(is.available()==0) Thread.yield();
boolean t= is.readBoolean();
System.out.println(t);
if (t){
String recept = is.readUTF();
FileOutputStream XML = new FileOutputStream("c:\\var\\XML.txt");
XML.write(recept.getBytes());
XML.close();
System.out.println(recept);
}
is.close();
Thread.sleep(1000);
/*
* envoie du fichier XML
*/
os = con.openDataOutputStream();
System.out.println ("connected for transfer send\n");
/*
* on se donne un petit temps avant de commencer le transfert
*/
//Thread.sleep(1000);
os.writeBoolean(true);
Thread.sleep(1000);
os.writeUTF(fichier);
os.flush();
System.out.println ("write down "+fichier.length()+" "+fichier.toString()+"\n");
os.close();
Thread.sleep(1000);
con.close();
} catch (Exception e2){
System.out.println("Failed "+e2.getMessage());
System.out.println("IOException: " + e2.getMessage());
return false;
}
return true;
}//fin initConnect
private boolean searchServices(RemoteDevice dev) {
UUID[] searchList2 = new UUID[2];
searchList2[0] = new UUID(0x0003); // RFCOMM
searchList2[1] = new UUID("846D98F827B42C69127209AB9287C3A6", false); // UUID de Service Prylos/VGO
try {
System.out.println("Starting Service Search on " + dev.getBluetoothAddress());
//recherche uniquement le service Vertigo
int trans2 = m_agent.searchServices(null, searchList2, dev,this);
System.out.println("Starting Service Search2 " + trans2);
}
catch (BluetoothStateException e) {
System.out.println("BluetoothStateException: " + e.getMessage());
}
synchronized (this) {
serviceSearchCount++;
if (serviceSearchCount == maxServiceSearches) {
System.out.println("Waiting");
try {
this.wait();
} catch (Exception e){}
}
System.out.println("Done Waiting ");
}
while (serviceSearchCount > 0) {
synchronized (this) {
try {
this.wait();
} catch (Exception e) {}
}
}
if (record != null) {
return true;
} else {
return false;
}
}
public Vector TrouveDevices() {
device.clear();
try {
m_agent.startInquiry(DiscoveryAgent.GIAC, this);
synchronized (this) {
try {
this.wait();
} catch (Exception e) {}
}
}
catch(Exception ex) {
ex.printStackTrace();
System.out.println("Unable to find devices to search");
}
return device;
}
public Vector searchService(String dev){
if (searchServices((RemoteDevice)hdevice.get(dev))) {
System.out.println("services trouvés");
return record;
}
return null;
}
public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
try {
System.out.println("New Device "+btDevice.getBluetoothAddress()+" found!");
System.out.println("Remote Name of the device is "+btDevice.getFriendlyName(true));
device.addElement(btDevice.getFriendlyName(true));
hdevice.put(btDevice.getFriendlyName(true), btDevice);
}
catch (Exception e){}
}
public void inquiryCompleted(int discType) {
System.out.println("Device Search completed!"+ discType);
synchronized (this) {
try {
this.notifyAll();
}
catch (Exception e) {}
}
}
public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
try {
//hrecord.put(servRecord[0].getHostDevice().getFriendlyName(true), servRecord[0]);
//record.add(servRecord[0].getHostDevice().getFriendlyName(true));
if (servRecord[0] != null){
hrecord.put(servRecord[0].getHostDevice().getFriendlyName(true), servRecord[0]);
record.add(servRecord[0].getHostDevice().getFriendlyName(true));
System.out.println("Service: "+servRecord[0].getConnectionURL(0,false));
}
} catch (Exception e) {}
}
public void serviceSearchCompleted(int transID, int respCode) {
System.out.println("Services search completed=" + transID + ", CodeRep=" + respCode);
serviceSearchCount--;
synchronized (this) {
this.notifyAll();
}
}
public static void main(String[] args) throws Exception {
String cmd = "toto";
BTAnanas BTAnanas= new BTAnanas(cmd);
}
} |
Partager