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
| import java.util.Vector;
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.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.Choice;
public class MidletclientRecherche extends MIDlet implements CommandListener,
Runnable, DiscoveryListener {
Vector pe = new Vector(); // vecteur des dispositifs trouvés
Vector se = new Vector(); // vecteur des services trouvés
Command cmc = new Command("Exécution", Command.OK, 1);
Command Decouverte = new Command("Découverte des dispositifs", Command.SCREEN, 1);
Command ADD = new Command("Annuler découverte des dispositifs",Command.CANCEL,1) ;
Command DS = new Command("Découverte des services",Command.ITEM,1 );
Command ADS = new Command("Annuler découverte des services",Command.CANCEL,1) ;;
Command Quitter = new Command("Quitter", Command.EXIT, 2);
Command Retour = new Command("Retour", Command.BACK,0);
Form F, F2, F3; //formes utilisées
DiscoveryAgent DA;
LocalDevice LD;
private ServiceRecord[] ST;
private UUID[] uuidSet;
Ticker T = new Ticker(" ...... Recherche ...... ");
int btcount; // Compteur des dispositifs
int srcount; // Compteur des services
List L = null; // Liste d'affichage
private int transactionID; // pour garder la trace pour les services découverts
public void startApp() {
try{
F = new Form("Forme Principale");
F.addCommand(Quitter);
// F.addCommand(ADD);
F.addCommand(cmc);
F.setCommandListener(this);
Display.getDisplay(this).setCurrent(F);
}catch(Exception e){
System.out.println(e);
}
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable d) {
switch(c.getCommandType()){
case Command.SCREEN:
L.deleteAll();
run();
break;
case Command.OK:
F.deleteAll();
run1();
break;
case Command.EXIT:
notifyDestroyed();
break;
case Command.BACK:
Display.getDisplay(this).setCurrent(F);
break;
default:
F.append(" Choisir l'option que vous voulez effectuer!");
}
List down = (List)Display.getDisplay(this).getCurrent();
switch(down.getSelectedIndex()){
case 0:
L.deleteAll();
run3();
break;
case 1:
L.deleteAll();
run3();
break;
case 2:
L.deleteAll();
run3();
break;
case 3:
L.deleteAll();
run3();
break;
case 4:
L.deleteAll();
run3();
break;
case 5:
L.deleteAll();
run3();
break;
default:
L.deleteAll();
F.append("Réessayer");
break;
}
}
public void run1(){
try{
L = new List("Lancer la découverte",Choice.TEXT_WRAP_ON);
L.addCommand(Retour);
L.addCommand(ADD);
L.addCommand(Decouverte);
L.setCommandListener(this);
Display.getDisplay(this).setCurrent(L);
}catch(Exception e){
System.out.println(e);
}
}
public void run() {
try{
L = new List ("Affichage des dispositifs découverts", Choice.IMPLICIT);
L.addCommand(Retour);
L.addCommand(ADD);
L.addCommand(Decouverte);
L.setCommandListener(this);
Display.getDisplay(this).setCurrent(L);
btcount = 0;
L.setTicker(T);
LD = LocalDevice.getLocalDevice();
DA = LD.getDiscoveryAgent();
DA.startInquiry(DiscoveryAgent.GIAC, this);
}catch(Exception e){
System.out.println(e);
}
}
public void deviceDiscovered(RemoteDevice bt, DeviceClass cod) {
pe.addElement(bt);
btcount++;
}
public void servicesDiscovered(int transactionID, ServiceRecord[] serviceRecord) {
ST = serviceRecord;
se.addElement(ST);
srcount++;
}
public void serviceSearchCompleted(int transID, int respCode) {
if (se.isEmpty())
F2.setTicker(new Ticker (" Aucun service trouvé!!!"));
else
{
F2.addCommand(ADS);
F2.setTicker(new Ticker(" services trouvés"));
}
// if (pe.size() > 0){
// run3( (RemoteDevice) pe.elementAt((
// (List)Display.getDisplay(this).getCurrent()).getSelectedIndex()));
for (int i=0; i<se.size(); i++){
try{
F2.append("services : ");
}catch(Exception e){
System.out.println(e);
}
if (srcount == 1)
F2.setTicker(new Ticker(srcount + " service trouvé"));
else
F2.setTicker(new Ticker(srcount + " services trouvés"));
}
se.removeAllElements();
}
public void inquiryCompleted(int ent) {
if (pe.isEmpty())
L.setTicker(new Ticker (" Aucun dispositif trouvé!!!"));
else
{
L.addCommand(DS);
L.setTicker(new Ticker(" dispositifs trouvés"));
}
for (int i=0; i<pe.size(); i++){
try{
String FN = ((RemoteDevice) pe.elementAt(i)).getFriendlyName(true);
String BA = ((RemoteDevice) pe.elementAt(i)).getBluetoothAddress();
String s = "Dispositif "+i+" : "+ FN + " --> "+ BA +"\n";
L.append(s,null);
}catch(Exception e){
System.out.println(e);
}
if (btcount == 1)
L.setTicker(new Ticker(btcount + " Dispositif trouvé"));
else
L.setTicker(new Ticker(btcount + " Dispositifs trouvés"));
}
/** switch (ent) {
case DiscoveryListener.INQUIRY_COMPLETED:
if (pe.size() > 0){
run3( (RemoteDevice) pe.elementAt((
(List)Display.getDisplay(this).getCurrent()).getSelectedIndex()));
}
break;
case DiscoveryListener.INQUIRY_ERROR:
break;
case DiscoveryListener.INQUIRY_TERMINATED:
break;
}*/
pe.removeAllElements();
}
private void run3() {
F2 = new Form("les services présentés par le dispositif sélectionné:");
F2.addCommand(DS);
F2.addCommand(ADS);
F2.addCommand(Retour);
F2.setCommandListener(this);
Display.getDisplay(this).setCurrent(F2);
// Recherche des dispositifs
uuidSet = new UUID[1];
uuidSet[0]= new UUID(0x1002);
int[] listeAttribut = {0x100,0x101,0x102};
RemoteDevice currentDevice =
(RemoteDevice) pe.elementAt(
((List)Display.getDisplay(this).getCurrent()).getSelectedIndex());
if(currentDevice == null) {
return;
}
try {
transactionID = DA.searchServices(
listeAttribut, uuidSet, currentDevice, this);
F2.addCommand(ADS);
} catch (BluetoothStateException e) {
}
}
} |
Partager