Pb dans la recheche des Services sur un périf Bluetooth
Bonjour tout le monde,
j'ai besoin de votre aide, mon code Java parfois marche très bien mais parfois non, plus exactement j'ai une midlet qui communique avec un serveur en bluetooth pour transférer un fichier, bref la midlet détecte sans aucun pb les perifs bluetooth mais pour les services parfois cela marche et le transfert de fichier se fait correctement mais parfois ma midlet se bloque au niveau de détection des périfs Bluetooth sans passé au services.
Midlet:
Code:
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
|
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(0x1101);
int[] attrSet = {0x0100, 0x0003, 0x0004};
try {
agent.searchServices(attrSet,uuidSet,remoteDevice, this);
screenSearch.append("Service search at :" + remoteDevice.getBluetoothAddress(), null);
} catch (BluetoothStateException e1) {
e1.printStackTrace();
}
try {
synchronized(this){
this.wait();
}
}
catch (InterruptedException e) {
e.printStackTrace();
}
// affichage URL.
screenSearch.append("URL"+ connectionURL, null);
//connect to the server and send a line of text
try{
streamConnection=(StreamConnection)Connector.open(connectionURL);
//send string
OutputStream outStream=streamConnection.openOutputStream();
OutputStreamWriter pWriter= new OutputStreamWriter(outStream);
pWriter.write("Hi Server i wana a zeph video\r\n");
pWriter.flush();
pWriter.close();
}catch(Exception e){
}
//read response
/*
InputStream inStream;
try {
inStream = streamConnection.openInputStream();
InputStreamReader bReader2 = new InputStreamReader(inStream);
int c;
StringBuffer str = new StringBuffer();
while((c=bReader2.read())!= -1){
str.append((char)c);
}
screenSearch.append("Server Response :" + str.toString(), null);
} catch (IOException e) {
e.printStackTrace();
}
//BufferedReader bReader2=new BufferedReader(new InputStreamReader(inStream));
//String lineRead=bReader2.readLine();
*/
try{
InputStream inStream = streamConnection.openInputStream();
int c;
FileConnection fc = (FileConnection)Connector.open("file:///C:/predefgallery/predefvideos/a.3gp",Connector.READ_WRITE);
if(!fc.exists()) fc.create();
OutputStream _out = fc.openOutputStream();
while((c=inStream.read())!= -1){
_out.write(c);
}
InputStream is = fc.openInputStream();
Player player = Manager.createPlayer(is,"video/3gp");
player.prefetch();
player.realize();
player.start();
fc.close();
_out.close();
inStream.close();
screenSearch.append("Transfert Terminer", null);
}catch(Exception e){
screenSearch.append("Exception "+e.getMessage(), null);
}finally{
try {
streamConnection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
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) {
if(servRecord!=null && servRecord.length>0){
connectionURL=servRecord[0].getConnectionURL(0,false);
//RemoteDevice device = servRecord[0].getHostDevice();
//String adresse = device.getBluetoothAddress();
//screenSearch.append("connecté à :"+ adresse, null);
}
synchronized(this){
this.notify();
}
} |
Server:
Code:
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
|
public class SampleSPPServer {
//start server
private void startServer() throws IOException{
//Create a UUID for SPP
UUID uuid = new UUID(0x1101);
//Create the servicve url
String connectionString = "btspp://localhost:" + uuid +";name=Sample SPP Server";
//open server url
StreamConnectionNotifier streamConnNotifier = (StreamConnectionNotifier)Connector.open( connectionString );
//Wait for client connection
System.out.println("\nServer Started. Waiting for clients to connect...");
StreamConnection connection=streamConnNotifier.acceptAndOpen();
RemoteDevice dev = RemoteDevice.getRemoteDevice(connection);
System.out.println("Remote device address: "+dev.getBluetoothAddress());
System.out.println("Remote device name: "+dev.getFriendlyName(true));
//read string from spp client
InputStream inStream=connection.openInputStream();
BufferedReader bReader=new BufferedReader(new InputStreamReader(inStream));
String lineRead=bReader.readLine();
System.out.println(lineRead);
//send response to spp client
/*
OutputStream outStream=connection.openOutputStream();
PrintWriter pWriter=new PrintWriter(new OutputStreamWriter(outStream));
pWriter.write("hi mal hadchi mabgha ikhdem walmarad\r\n");
pWriter.flush();
pWriter.close();
*/
OutputStream outStream=connection.openOutputStream();
//if(inStream != null){
FileInputStream intputstream = null;
try{
intputstream = new FileInputStream("a.3gp");
int c;
while((c=intputstream.read())!= -1){
System.out.println("suis la ");
outStream.write(c);
}
System.out.println("Transfert terminer");
}catch(Exception e){
System.out.println("Exception : "+e.getMessage());
}finally{
//streamConnNotifier.close();
}
//}
}
public static void main(String[] args) throws IOException {
//display local device address and name
LocalDevice localDevice = LocalDevice.getLocalDevice();
System.out.println("Address: "+localDevice.getBluetoothAddress());
System.out.println("Name: "+localDevice.getFriendlyName());
SampleSPPServer sampleSPPServer=new SampleSPPServer();
sampleSPPServer.startServer();
}
} |
Merci d'avance
Ziad