PFE : APPLICATION MOBILE

cs_ANGELA87 Messages postés 7 Date d'inscription vendredi 20 février 2009 Statut Membre Dernière intervention 24 avril 2009 - 24 avril 2009 à 01:03
cs_ANGELA87 Messages postés 7 Date d'inscription vendredi 20 février 2009 Statut Membre Dernière intervention 24 avril 2009 - 24 avril 2009 à 01:11
Salut,
Je suis une étudiante et dans le cadre de mon PFE, je suis entrain de réaliser une application mobile qui sert à piloter une présentation power point a partir du téléphone via la technologie Bluetooth.
J’ai réalisé les deux classe client et serveur pour le client c’est bon j’arrive trouver le nom de mon pc sur mon écran de téléphone mais pour le serveur il reste toujours en attente. Est-ce que je dois lui passer l’UUID du téléphone ? Est ce que je peux exécuter le serveur tous simplement à partir de l’invite de commande ?
Merci

3 réponses

cs_ANGELA87 Messages postés 7 Date d'inscription vendredi 20 février 2009 Statut Membre Dernière intervention 24 avril 2009
24 avril 2009 à 01:04
Salut,
Voilà mon code serveur :
import javax.bluetooth.DataElement;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.ServiceRecord;
import javax.bluetooth.UUID;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.io.StreamConnectionNotifier;
import java.io.*;
import java.util.Vector;

public class BtServer implements Runnable
{
private static final UUID SERVER_UUID = new UUID(0x8080);
private static final int SERV_ATTRIBUTE_ID = 0x9999;
private LocalDevice localDevice;
private StreamConnectionNotifier notifier;
private ServiceRecord record;
private boolean isClosed;
private Thread accepterThread;
public Vector lstCli;
/** Creates a new instance of BtServer */
public BtServer() {
accepterThread = new Thread(this);
accepterThread.start();
}
public void stop()
{
isClosed=true;
}
public void close()
{ Thread tache=new Thread(this);
tache.stop();
}
public void run() {
boolean isBTReady = false;
try {
// create/get a local device
localDevice = LocalDevice.getLocalDevice();
// set we are discoverable
if (!localDevice.setDiscoverable(DiscoveryAgent.GIAC)) {
throw new IOException("Can't set discoverable mode...");
}
// prepare a URL to create a notifier
StringBuffer url = new StringBuffer("btspp://");
// indicate this is a server
url.append("localhost").append(':');
// add the UUID to identify this service
url.append(SERVER_UUID.toString());
// add the name for our service
url.append(";name=btserver;master=true");
// request all of the client to be authorized
url.append(";authorize=true");
// create notifier now
notifier = (StreamConnectionNotifier) Connector.open(url.toString());
// and remember the service record for the later updates
record = localDevice.getRecord(notifier);
localDevice.getDeviceClass();
// create a special attribute
DataElement base = new DataElement(DataElement.DATSEQ);
record.setAttributeValue(SERV_ATTRIBUTE_ID, base);
record.setDeviceServiceClasses(SERV_ATTRIBUTE_ID);
// remember we've reached this point.
isBTReady = true;
System.out.println(url);
} catch (Exception e) {
System.err.println("Can't initialize bluetooth: " + e);
}
// nothing to do if no bluetooth available
if (!isBTReady)
{
return;
}

// ok, start processor now
String n=null;
StreamConnection conn = null;
DataInputStream in=null;
DataOutputStream out=null;
System.out.println(localDevice.getFriendlyName()+" est en écoute");
isClosed=false;
// ok, start accepting connections then
int i=1;
try {
conn = notifier.acceptAndOpen();
System.out.println("Connexion accepter");
int x=0;
in= conn.openDataInputStream();
while(true)
{
n=in.readUTF();
x=Integer.parseInt(n);
System.out.println(n);
//selon la valeur de n
switch(x)
{
case 1 : System.out.println("stop");break;
case 2 : System.out.println("play");break;
case 3 : System.out.println("pause");break;
}
}
} catch (Exception e) {
System.out.println("Msg : "+e.toString());
//ignorer
}
return;
}
void destroy() {
isClosed = true;
// finilize notifier work
if (notifier != null) {
try {
notifier.close();
} catch (IOException e) {} // ignore
}
// wait for acceptor thread is done
try {
accepterThread.join();
} catch (InterruptedException e) {} // ignore.
}
}
Merci
0
cs_ANGELA87 Messages postés 7 Date d'inscription vendredi 20 février 2009 Statut Membre Dernière intervention 24 avril 2009
24 avril 2009 à 01:05
Salut,
Pour le code client :
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 java.util.Vector;
import java.io.*;
public class BtClient implements Runnable, DiscoveryListener {
private static final UUID SERVER_UUID = new UUID(0x8080);
private static final int SERV_ATTRIBUTE_ID = 0x9999;
private static final int READY = 0;
private static final int DEVICE_SEARCH = 1;
private static final int SERVICE_SEARCH = 2;
private int state = READY;
private DiscoveryAgent discoveryAgent;
private ControlMedia parent;
private boolean isClosed;
private Thread processorThread;
private Vector devices = new Vector();
private Vector records = new Vector();
private RemoteDevice rmt;
private int discType;
private int[] searchIDs;
private UUID[] uuidSet;
private int[] attrSet;
public StreamConnection cc;
public String login;
DataOutputStream dataout;
/** Creates a new instance of BtClient */
public BtClient(ControlMedia parent) {
this.parent = parent;
processorThread = new Thread(this);
processorThread.start();
}
public void run() {
// initialize bluetooth first
boolean isBTReady = false;
try {
// create/get a local device and discovery agent
LocalDevice localDevice = LocalDevice.getLocalDevice();
discoveryAgent = localDevice.getDiscoveryAgent();
// remember we've reached this point.
isBTReady = true;
} catch (Exception e) {
System.err.println("Can't initialize bluetooth: " + e);
}
// nothing to do if no bluetooth available
if (!isBTReady) {
return;
}
// initialize some optimization variables
uuidSet = new UUID[2];
// ok, we are interesting in btspp services only
uuidSet[0] = new UUID(0x1101);
// and only known ones, that alllows pictures
uuidSet[1] = SERVER_UUID;
// we need an only service attribute actually
attrSet = new int[1];
// it's one
attrSet[0] = SERV_ATTRIBUTE_ID;
// start processing the search
processSearch();
// userConnect();
}
public void nbdev()
{
parent.getList().append("Nb Dev="+String.valueOf(devices.size()),parent.onImage);
}
public void nbserv()
{
parent.getList().append("Nb Serv="+String.valueOf(records.size()),parent.onImage);
}
public void envoiMsg(String msg)
{
try
{
dataout.writeUTF(msg);
dataout.flush();
}
catch(Exception e)
{}
}
public void userConnect() {
String url = null;
// String n=null;
// String p=null;
try
{
rmt=(RemoteDevice)devices.elementAt(parent.getList().getSelectedIndex());
ServiceRecord sr = (ServiceRecord) records.elementAt(parent.getList().getSelectedIndex());
url = sr.getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT,false);
cc= (StreamConnection)Connector.open(url);
dataout = cc.openDataOutputStream();
parent.getStringItem().setText(parent.getList().getString(parent.getList().getSelectedIndex()));
}
catch(Exception e)
{} }
public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
// same device may found several times during single search
if (devices.indexOf(btDevice) == -1) {
devices.addElement(btDevice);
try
{
String nm=btDevice.getFriendlyName(false);
parent.getList().append(nm,parent.onImage);
}
catch(Exception e)
{} } }
/** Invoked by system when device discovery is done.
*
* Use a trick here - just remember the discType
* and process its evaluation in another thread.
*/
public void inquiryCompleted(int discType) {
this.discType = discType;
synchronized (this) {
notify(); }}
public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
for (int i = 0; i < servRecord.length; i++) {
records.addElement(servRecord[i]); } }
public void serviceSearchCompleted(int transID, int respCode) {
// first, find the service search transaction index
int index = -1;
for (int i = 0; i < searchIDs.length; i++) {
if (searchIDs[i] == transID) {
index = i;
break; }}
// error - unexpected transaction index
if (index == -1) {
System.err.println("Unexpected transaction index: " + transID);
// FIXME: process the error case
} else { searchIDs[index] = -1; }
/ * Actually, we do not care about the response code -
* if device is not reachable or no records, etc.
*/
// make sure it was the last transaction
for (int i = 0; i < searchIDs.length; i++) {
if (searchIDs[i] != -1) {
return; } }
// ok, all of the transactions are completed
synchronized (this) {
notify(); } }
/** Cancel's the devices/services search. */
void cancelSearch() {
synchronized (this) {
if (state == DEVICE_SEARCH) {
discoveryAgent.cancelInquiry(this);
} else if (state == SERVICE_SEARCH) {
for (int i = 0; i < searchIDs.length; i++) {
discoveryAgent.cancelServiceSearch(searchIDs[i]); } } } }
void destroy() {
synchronized (this) {
isClosed = true;
notify();
// FIXME: implement me
}
// wait for acceptor thread is done
try {
processorThread.join();
} catch (InterruptedException e) {} // ignore }
/** * Processes images seach/download until component is closed
* or system error has happen.
*/
private synchronized void processSearch() {
if (isClosed) {
return; }
// search for devices
if (!searchDevices()) {
return;
} else if (devices.size() == 0) { }
// search for services now
if (!searchServices()) {
return;
} else if (records.size() == 0) {}//} }
/* * Search for bluetooth devices.
* @return false if should end the component work.
*/
public boolean searchDevices() {
// ok, start a new search then
System.out.println("Search devices.....");
state = DEVICE_SEARCH;
devices.removeAllElements();
RemoteDevice btDevice;
try {
discoveryAgent.startInquiry(DiscoveryAgent.GIAC, this);
} catch (BluetoothStateException e) {
System.err.println("Can't start inquiry now: " + e);
return true;}
try {
wait();// until devices are found
System.out.println("Nombre de devices trouvé :");
int nb=devices.size();
/* if (nb==1)
parent.get_textField1().setString("1");
else
parent.get_textField1().setString("0");*/
//System.out.println(devices.size());
} catch (InterruptedException e) {
System.err.println("Unexpected interuption: " + e);
return false;
}
// this "wake up" may be caused by 'destroy' call
if (isClosed) {
return false; }
// no?, ok, let's check the return code then
switch (discType) {
case INQUIRY_ERROR:
System.out.println("INQUIRY_ERROR");
// fall through
case INQUIRY_TERMINATED:
// make sure no garbage in found devices list
System.out.println("INQUIRY_TERMINATED");
devices.removeAllElements();
// nothing to report - go to next request
break;
case INQUIRY_COMPLETED:
if (devices.size() == 0) { }
//System.out.println("INQUIRY_COMPLETED");

// go to service search now
break;
default:
// what kind of system you are?... :(
System.err.println("system error: unexpected device discovery code: " + discType);
destroy();
return false; }
return true;}
private boolean searchServices() {
state = SERVICE_SEARCH;
records.removeAllElements();
searchIDs = new int[devices.size()];
boolean isSearchStarted = false;
for (int i = 0; i < devices.size(); i++) {
RemoteDevice rd = (RemoteDevice) devices.elementAt(i);
try {
searchIDs[i] = discoveryAgent.searchServices(attrSet, uuidSet,rd, this);
} catch (BluetoothStateException e) {
System.err.println("Can't search services for: "+rd.getBluetoothAddress() + " due to " + e);
searchIDs[i] = -1;
continue;}
isSearchStarted = true;}
// at least one of the services search should be found
if (!isSearchStarted) {
return true;}
try { wait(); // until services are found } catch (InterruptedException e) { System.err.println("Unexpected interuption: " + e);
return false; }
// this "wake up" may be caused by 'destroy' call
if (isClosed) {
return false; }
// actually, no services were found
if (records.size() == 0) {}
return true;} }

Merci
0
cs_ANGELA87 Messages postés 7 Date d'inscription vendredi 20 février 2009 Statut Membre Dernière intervention 24 avril 2009
24 avril 2009 à 01:11
Salut,
je suis désolé pour la male organisation du code.
Merci.
0
Rejoignez-nous