BT_java 1 Bluetooth orienté java : JSR82 + Android Notes de cours et mise en œu
BT_java 1 Bluetooth orienté java : JSR82 + Android Notes de cours et mise en œuvre jean-michel Douin, douin au cnam point fr version du 22 Novembre 2011 BT_java 2 Sommaire • Le contexte – Bluetooth, caractéristiques, brève présentation • Les protocoles et profils • JSR82, Principes – Deux paquetages, • Usage identique pour J2SE et J2ME • Découverte – Des périphériques BT et des services • Android, en première approche – Client • Communication • RFComm btspp:// • OBEX btgoep:// … BT_java 3 Bibliographie utilisée La doc de l’API jsr82, http://jcp.org/aboutJava/communityprocess/final/jsr082/index.html http://www.kjhole.com/Standards/BT/BTdownloads.html http://bluevirus.googlecode.com/files/bluetooth-for-java.9781590590782.22021.pdf http://www.bluecove.org/bluecove/apidocs/ Recherche google : Un exposé de Macherel Bruno ppt M2PGI – UFRIMA Une présentation de Apurva Kumar(www.research.ibm.com/people/k/kapurva) Un exposé de Camille Diou, université de Metz, http://www.usc.edu/dept/ee/scip/assets/001/57411.pdf http://www.ieee802.org/11/T utorial/90538S-WPAN-Bluetooth-T utorial.pdf http://www.cs.umu.se/kurser/TDBD16/VT07/Bluetooth-Tutorial-2001.pdf http://www.bluetooth.com http://www.newlogic.com http://javabluetooth.chris-lorenz.com/ The JavaBluetooth Stack is a 100% (no native) Java implementation of the Bluetooth Specifications Version 1.1. It currently supports HCI, L2CAP and SDP. http://code.google.com/p/btfree/downloads/list http://www.bluecove.org pour un grand nombre de JVM … http://developer.android.com/guide/topics/wireless/bluetooth.html souvent référencé BT_java 4 Préambule Le contexte J2SE 6.0, update 11 WindowsXP/SP2, Outil de développement Bluej 2.2.1 … PC/clé Iogear GBU341W6, GBU421W6 librairie bluecove www.bluecove.org 2.1.0, « on winsock » Les MIDlet Outils NetBeans 6.5, WTK 2.5.2 CLDC1.1, MIDP2.0 sur PC avec le micro-emulator http://www.microemu.org/ sur mobiles : sony ericsson S500i, W580i, Motorola K1 KRZR Note: à priori, le WTK ne permet qu’une émulation du BT HTC Galaxy Tab, 2.2 API 8, HTC Magic http://www.iogear.com/product/GBU341W6/ http://www.iogear.com/product/GBU421W6/ BT_java 5 Marketing Aux alentours de 10 à 100 mètres (BT 2.0, class 1, class 2) Source : http://www.cs.umu.se/kurser/TDBD16/VT07/Bluetooth-Tutorial-2001.pdf BT_java 6 Bluetooth • Protocole de communication sans fil peu chère, peu consommatrice d’énergie…, adapté aux mobiles • Spécifications ici http://www.bluetooth.com/bluetooth/ – Bluetooth is not a one-on-one data transmission technology so it can communicate with up to eight devices within its transmission radius at one time • Vocable « habituel » : – Découverte multicast des services • Client/serveur, serveur/serveur BT_java 7 Mesures, à vérifier… Figures extraites de http://www.licm.sciences.univ-metz.fr/IMG/pdf/Cours_Bluetooth.pdf http://www.dandu.be/article/article-bluetooth-comparaison-dongles-46.html http://www.sena.com/blog/?p=74 BT 2.0 10-100m … Performances : à vérifier … BT_java 8 Des chiffres • Ondes radios : 2400 – 2483,5 MHz • Débits annoncés : 1 Mb/s .. 2 Mb/s • Portée : 1 à plus de 100m (1 à 100mW) – Class 3 Devices 100mW plus de 100 meters – Class 2 Devices 10mW plus de 10 meters – Class 1 Devices 1mW de 0.1-10 meters • Communication par paquets – Encadrés par des blocs de données de contrôles BT_java 9 Principe : des ondes autour de nous Découverte aux alentours Plusieurs périphériques peuvent répondre à une demande de services BT_java 10 Principe : des ondes autour de nous Le périphérique possesseur du service est connu BT_java 11 Vocabulaire : Piconet • Un Maître et ses esclaves • Esclaves affranchis Maître BT_java 12 Vocabulaire : Scatternets • Un Scatternet = plusieurs piconets BT_java 13 Scatternets se font et se défont • http://www.acm.org/crossroads/xrds9-4/blue.html BT_java 14 Combien ? • 255 appareils, 7 communications simultanées BT_java 15 Protocoles et profils • L2CAP – Logical Link Control and adaptation Protocol – Envoi de paquets avec un protocole donné vers le gestionnaire approprié. • RFCOMM – Émulation de ports séries au dessus de L2CAP. • OBEX – Object Exchange Protocol. – Analogue à HTTP/GET-POST, FTP • SDP – Service Discovery Protocol (SDP). • TCS – telephony control specification BT_java 16 Où sommes nous ? • www.cs.umu.se/kurser/TDBD16/VT07/Bluetooth-Tutorial-2001.pdf BT_java 17 Profils • Profils ou Capacités d’un périphériques BT à … – ftp version bluetooth, – Téléphonie sans fil, – Imprimer des documents, – Découverte des services (SDP), – .. – Applications dédiées • -> Recensement des capacités d’un appareil BT BT_java 18 De la documentation originale BT_java 19 En résumé • www.bluetooth.com/.../Technology/Works/OBEX.htm BT_java 20 Bluetooth, existentiel • Qui suis-je ? – Je me présente • Qui êtes vous ? – Aux alentours • Parmi vous qui possède ce service ? • Avez d’autres services à proposer ? – Quels sont-ils ? • UUID ou identification d’un service … – Adéquation UUID/URL – UUID prédéfinis, UUID pour une application • Quels protocoles ? • Où suis-je, où vais-je ? … – pas facile voir blipnet ericsson » http://www.blipsystems.com/files/filer/technical_overview.pdf BT_java 21 J2SE / JSR82 • Deux paquetages • Qui suis-je ?, qui est là ?, répondez … – javax.bluetooth.*; • Nommage, • Découverte • Recherche de services • Communication – btspp://localhost:{UUID} + (flux habituels) – Un protocole répandu OBEX • java.obex.*; – btgoep://localhost:localhost:{UUID} + (flux habituels) – tcpobex://1905 – Irdaobex://localhost:1900 – Session – Transfert d’objets » put, get • http://java.sun.com/javame/reference/apis/jsr082/javax/bluetooth/package-summary.html • http://www.jsr82.com • http://www.bluecove.org/ BT_java 22 Android • android.bluetooth • Qui suis-je ?, qui est là ?, répondez … – http://developer.android.com/guide/topics/wireless/bluetooth.html BT_java 23 Qui suis-je ? • Chaque périphérique BT possède • Une adresse (physique) sur 48 bit et unique – (MAC adresse) – Un nom lui est associé (en général) BT_java 24 Premier exemple : quelle est mon adresse BT ? import javax.bluetooth.LocalDevice; public class QuiSuisJe{ public static void main(String[] args)throws Exception{ LocalDevice localDevice = LocalDevice.getLocalDevice(); System.out.println(localDevice.getBluetoothAddress() + " : " + localDevice.getFriendlyName()); } } Depuis un PC, J2SE javac -cp .;bluecove-2.1.0.jar QuiSuisJe.java java -cp .;bluecove-2.1.0.jar QuiSuisJe http://www.bluecove.org/bluecove/apidocs/ BT_java 25 Sur simulateur de mobile et sur mobile : QuiSuisJe A cette URL http://jfod.cnam.fr/SEJA/jnlp/midlet/BTQuiSuisJe/ téléchargez sur votre poste http://jfod.cnam.fr/SEJA/jnlp/midlet/BTQuiSuisJe/bluecove-2.1.0.jar http://jfod.cnam.fr/SEJA/jnlp/midlet/BTQuiSuisJe/microemulator.jar http://jfod.cnam.fr/SEJA/jnlp/midlet/BTQuiSuisJe/BTQuiSuisJe.jar http://jfod.cnam.fr/SEJA/jnlp/midlet/BTQuiSuisJe/run_microemulateur.bat Puis exécutez > run_microemulateur.bat Sur votre mobile installez cette Midlette BTQuiSuisJe.jar BT_java 26 Sur Android * QuiSuisJe ? BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter(); String name = btAdapter.getAddress(); * Sur un mobile, bluetooth non pris en compte par l’émulateur BT_java 27 Bluetooh et nommage • 0019EF0117DA • Devrons nous connaître les adresses physiques ? –Non merci … • Sauf les fixes … –Une UUID associera nom logique / adresse physique •Universal Unique IDentifier •http://www.avetana-gmbh.de/avetana-gmbh/produkte/doc/javax/bluetooth/UUID.html • -> UUID nous utiliserons BT_java 28 La suite • Initialisation de la pile (protocole) • A la recherche d’un service – Android – J2SE/JSR82 • Découverte de périphériques BT – Aux alentours – Nommage/UUID Universally Unique IDentifier (128 bits), • existe en version courte • Communication – java.io.*, etc… • Découverte des services – Au sein de chaque entité – Nommage/UUID Universally Unique IDentifier (128 bits) • http://www.handheld-basic.com/documentation/text/page_1766.html BT_java 29 Un service se trouve sur ce périphérique • Il nous faut – L’adresse MAC du périphérique • Être appairé (Android) • Ex. "00:19:EF:01:17:9C" • UUID (128bits) – Qui identifie le service • ("10203040-5060-7080-90A0-B0C0D0E0F0FF"); BT_java 30 Android • Architecture retenue – Recherche du service bluetooth au sein d’une sous-classe d’AsyncTask – Communication dans un Thread, – Synchronisation en utilisant une SynchronousQueue BT_java 31 Sous-classe d’AsyncTask • Proposition private class ConnexionBT extends AsyncTask<String,String,BluetoothSocket>{ protected void onPreExecute() { protected BluetoothSocket doInBackground( String... args) { protected void publishProgress(String... infos) { protected void onPostExecute(BluetoothSocket btSocket) { BT_java 32 Exemple private UUID MY_UUID = UUID.fromString("10203040-5060-7080-90A0-B0C0D0E0F0FF"); private class ConnexionBT extends AsyncTask<String,String,BluetoothSocket>{ private ProgressDialog dialog = null; private BluetoothDevice btDevice; protected void onPreExecute() { dialog = ProgressDialog.show(…); } protected BluetoothSocket doInBackground(String... args) { try{ this.btDevice = btAdapter.getRemoteDevice(args[0]); // @ MAC btSocket = btDevice.createRfcommSocketToServiceRecord(MY_UUID); btAdapter.cancelDiscovery(); btSocket.connect(); }catch(Exception e){} return btSocket; } protected void onPostExecute(BluetoothSocket btSocket) { os = btSocket.getOutputStream(); BT_java 33 Communication sur os : dans un Thread public class Sender extends Thread{ private BlockingQueue<byte[]> queue; public Sender(){ queue = new SynchronousQueue<byte[]>(); this.start(); } public boolean offer(byte[] cmd){ return queue.offer(cmd); } public void close(){ this.interrupt(); } public void run(){ while(!isInterrupted()){ try { byte[] cmd = queue.take(); os.write(cmd); } catch (Exception e) { } } } } } BT_java 34 J2SE/Un client // à la découverte du service … connString = agent.selectService(new UUID("102030405060708090A0B0C0D0E0F0BB", false), ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false); StreamConnection conn = (StreamConnection)Connector.open(connString); OutputStream out = conn.openOutputStream(); Reader in = new InputStreamReader(conn.openInputStream()); String str = "http://jfod.cnam.fr:8999/ds2438/\n"; out.write(str.getBytes()); out.flush(); String response = readFile(in); … // dans un Thread private static String readFile(InputStream in) throws IOException { StringWriter data = new StringWriter(); int aByte; while((aByte = in.read())!=-1){ data.write( (byte)aByte); } return data.toString(); } BT_java 35 Parmi mes voisins … • Parmi les périphériques BT aux alentours – Quel est celui qui possède ce service … • Apparenté à la découverte multicast… – Un service : un UUID • new UUID("102030405060708090A0B0C0D0E0F011" ,false) – 128 bits … pourquoi pas celui-ci … – Plusieurs BT peuvent répondre pour un service souhaité • « dépôt d’un fichier », mp3 … • redondance BT_java 36 A typical usecase BT_java 37 A la découverte de … obtention d’un agent import javax.bluetooth.LocalDevice; import javax.bluetooth.DiscoveryAgent; public class ALaDécouverteDe{ public static void main(String[] args) throws Exception{ LocalDevice local = LocalDevice.getLocalDevice(); local.setDiscoverable( DiscoveryAgent.GIAC ); // GIAC General inquire Access Code DiscoveryAgent agent = local.getDiscoveryAgent(); un agent capable de tout faire Sélection d’un service Effectuer une recherche exhaustive BT_java 38 Adéquation UUID / URL LocalDevice local = LocalDevice.getLocalDevice(); local.setDiscoverable( DiscoveryAgent.GIAC ); // General inquire Access Code DiscoveryAgent agent = local.getDiscoveryAgent(); UUID uuid = new UUID("102030405060708090A0B0C0D0E0F011", false); String connString = agent.selectService(uuid, ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false); System.out.println("connString : " + connString); Un exemple d’affichage : connString: btspp://0019EF01194C:1;authenticate=false;encrypt=false;master=false 0019EF01194C l’adresse physique :1 le port de communication BT_java 39 Un service comme un autre • Nous avons – Un service -> un UUID – un UUID -> une URL – Une URL -> une communication • Une uploads/Litterature/ cours-bluetooth-jsr82-android.pdf
Documents similaires










-
34
-
0
-
0
Licence et utilisation
Gratuit pour un usage personnel Attribution requise- Détails
- Publié le Nov 18, 2022
- Catégorie Literature / Litté...
- Langue French
- Taille du fichier 2.9093MB