IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Java ME Discussion :

Détection de service Bluetooth sur un serveur


Sujet :

Java ME

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Inscrit en
    Mars 2005
    Messages
    15
    Détails du profil
    Informations forums :
    Inscription : Mars 2005
    Messages : 15
    Par défaut Détection de service Bluetooth sur un serveur
    Bonjour tout le monde,

    Voilà après plusieurs heures de recherche infructueuse, je tourne vers vous pour m'aider dans mon problème.

    Le but est de faire communiquer un telephone mobile avec un serveur en bluetooth pour récupérer du texte.

    J'ai développé une Midlet en utilisant la bluecove JSR82 la partie "Device discovery". C'est ok mon serveur est bien découvert, ensuite j'aimerais découvrir les SERVICES dispo sur mon serveur (développez en J2SE) sachant qu'il est bien lancé, pour cela j'utilise la méthode searchServices(...) avec ces méthodes :

    servicesDiscovered(int i, ServiceRecord[] serviceRecord) {} //lors de la découverte.
    serviceSearchCompleted(int i, int i0) {} //à la fin de la recherche.

    Mon problème est qu'aucun service n'est découvert. (servicesDiscovered n'est jamais appellée.)

    Voici mon code désolé mon code n'est pas si structuré.
    Midlet :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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];
    	    }
    	  }
    }
    Serveur :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    package ma.emsi.discover;
     
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.OutputStreamWriter;
    import java.io.PrintWriter;
     
    import javax.bluetooth.*;
    import javax.microedition.io.*;
     
     
    public class SampleSPPServer {
     
        //start server
        private void startServer() throws IOException{
     
            //Create a UUID for SPP
            UUID uuid = new UUID("1101", true);
            //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("Response String from SPP Server\r\n");
            pWriter.flush();
     
            pWriter.close();
            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

  2. #2
    Candidat au Club
    Inscrit en
    Août 2008
    Messages
    2
    Détails du profil
    Informations forums :
    Inscription : Août 2008
    Messages : 2
    Par défaut bluetooth et PDA
    moi aussije fait la méme chose que toi mais moi je dois récupéré les donné depuis une module GPS connécté au PDA via bleutooth alors je sais pas comment faire est ce que le code que ta présenté ici peu m étre utile stp

Discussions similaires

  1. Publier un web service .net sur un serveur Apache
    Par Kropernic dans le forum Services Web
    Réponses: 3
    Dernier message: 16/08/2010, 13h12
  2. Réponses: 0
    Dernier message: 19/06/2009, 14h38
  3. Services Bluetooth sur émulateurs
    Par fabrice74 dans le forum Java ME
    Réponses: 0
    Dernier message: 11/05/2009, 02h16
  4. Creer un service bluetooth sur linux en C
    Par psanchez dans le forum Linux
    Réponses: 0
    Dernier message: 05/06/2008, 11h19
  5. [MySQL] comment je peux faire un service web sur mon serveur ?
    Par jadoo dans le forum PHP & Base de données
    Réponses: 3
    Dernier message: 17/05/2006, 14h48

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo