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 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
|
package com.ziad.bluetooth;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Vector;
import javax.microedition.io.*;
import java.io.InputStream;
import java.io.PrintStream;
import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DeviceClass;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.DiscoveryListener;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.ServiceRecord;
import javax.bluetooth.UUID;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class MyBluetoothClass extends MIDlet implements CommandListener, DiscoveryListener{
private Form form1;
private Display _display;
private Command detecter;
private LocalDevice local;
private DiscoveryAgent agent;
private List screenSearch;
Vector old_devices;
Vector devices;
long cache_len;
long last_scan;
//private static Object lock=new Object();
private String connectionURL=null;
public MyBluetoothClass() throws BluetoothStateException{
//myBlue = new MyBluetoothClass();
local = LocalDevice.getLocalDevice();
agent = local.getDiscoveryAgent();
cache_len = 30000;
last_scan = 0;
old_devices = new Vector();
devices = new Vector();
}
public RemoteDevice[] someDevs() {
RemoteDevice[] pre = preknownDevs();
RemoteDevice[] cached = cachedDevs();
int num_devs = pre.length + cached.length;
if(num_devs == 0) {
return scanDevs();
} else {
RemoteDevice[] r = new RemoteDevice[num_devs];
array_copy_unique(pre, r);
array_copy_unique(cached, r);
return r;
}
}
public RemoteDevice[] cachedDevs() {
RemoteDevice[] r = agent.retrieveDevices(DiscoveryAgent.CACHED);
if( r == null )
r = new RemoteDevice[0];
return r;
}
public RemoteDevice[] preknownDevs() {
RemoteDevice[] r = agent.retrieveDevices(DiscoveryAgent.PREKNOWN);
if( r == null )
r = new RemoteDevice[0];
return r;
}
public RemoteDevice[] lastDevs() {
RemoteDevice[] devs = new RemoteDevice[ old_devices.size() ];
old_devices.copyInto(devs);
return devs;
}
public RemoteDevice[] scanDevs() {
devices.removeAllElements();
try {
agent.startInquiry(DiscoveryAgent.GIAC, this);
synchronized(this) {
this.wait();
}
} catch(InterruptedException ie) {
ie.printStackTrace();
} catch(BluetoothStateException bse) {
bse.printStackTrace();
}
RemoteDevice[] devs = new RemoteDevice[ devices.size() ];
devices.copyInto(devs);
Vector t = old_devices;
old_devices = devices;
devices = t;
last_scan = System.currentTimeMillis();
return devs;
}
public String deviceNames () {
String res = "";
RemoteDevice[] devs = scanDevs();
for (int i=0; i<devs.length; i++) {
try {
res += "plus " + devs[i].getFriendlyName(true);
} catch (IOException e) {
e.printStackTrace();
}
}
return res;
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
protected void startApp() throws MIDletStateChangeException {
_display = Display.getDisplay(this);
form1 = new Form("Bluetooth Devices Detection");
detecter = new Command("detection", Command.OK, 0);
form1.addCommand(detecter);
form1.setCommandListener(this);
_display.setCurrent(form1);
}
public void commandAction(Command arg0, Displayable arg1) {
if (screenSearch == null) {
// write pre-init user code here
screenSearch = new List("Perif Bluetooth", Choice.IMPLICIT);
RemoteDevice[] devs = scanDevs();
for (int i=0; i<devs.length; i++) {
try {
screenSearch.append(devs[i].getFriendlyName(true), null);
}
catch (IOException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
}
_display.setCurrent(screenSearch);
// check for spp service
RemoteDevice remoteDevice=devs[0];
UUID[] uuidSet = new UUID[1];
uuidSet[0]=new UUID("1101",false);
int[] attrSet = {0x0100, 0x0003, 0x0004};
try {
agent.searchServices(attrSet,uuidSet,remoteDevice, this);
} catch (BluetoothStateException e1) {
e1.printStackTrace();
}
try {
synchronized(this){
this.wait();
}
}
catch (InterruptedException e) {
e.printStackTrace();
}
// affichage URL.
//screenSearch.append("suis la "+ connectionURL, null);
/*
//connect to the server and send a line of text
try{
StreamConnection streamConnection=(StreamConnection)Connector.open(connectionURL);
//send string
OutputStream outStream=streamConnection.openOutputStream();
OutputStreamWriter pWriter= new OutputStreamWriter(outStream);
pWriter.write("Test String from SPP Client\r\n");
pWriter.flush();
}catch(Exception e){
}
//read response
//InputStream inStream=streamConnection.openInputStream();
//BufferedReader bReader2=new BufferedReader(new InputStreamReader(inStream));
//String lineRead=bReader2.readLine();
*/
}
}
public void deviceDiscovered(RemoteDevice dev, DeviceClass cod) {
// TODO Auto-generated method stub
devices.addElement(dev);
}
public void inquiryCompleted(int arg0) {
// TODO Auto-generated method stub
synchronized(this) {
this.notifyAll();
}
}
public void serviceSearchCompleted(int arg0, int arg1) {
synchronized(this){
this.notify();
}
}
public void servicesDiscovered(int arg0, ServiceRecord[] servRecord) {
screenSearch.append("suis la servicesDiscovered", null);
if(servRecord!=null && servRecord.length>0){
connectionURL=servRecord[0].getConnectionURL(0,false);
}
synchronized(this){
this.notify();
}
}
private boolean array_contains(Object[] array, Object o) {
if(array == null)
return false;
for(int i=0; i<array.length; i++) {
if( array[i].equals(o) )
return true;
}
return false;
}
private void array_copy_unique(Object[] arr1, Object[] arr2)
throws NullPointerException {
if(arr1 == null || arr2 == null)
return;
int len1 = arr1.length;
int len2 = arr2.length;
int arr2_i;
for(arr2_i=0; arr2_i < arr2.length; arr2_i++) {
if(arr2[arr2_i] != null)
break;
}
for(int arr1_i=0; arr1_i<arr1.length; arr1_i++, arr2_i++) {
if(!array_contains(arr2, arr1[arr1_i]))
arr2[arr2_i] = arr1[arr1_i];
}
}
} |
Partager