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

API standards et tierces Android Discussion :

Comprendre un fichier source Java (connexion Bluetooth ?)


Sujet :

API standards et tierces Android

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Futur Membre du Club
    Homme Profil pro
    Webmaster
    Inscrit en
    Janvier 2015
    Messages
    3
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 51
    Localisation : France, Marne (Champagne Ardenne)

    Informations professionnelles :
    Activité : Webmaster

    Informations forums :
    Inscription : Janvier 2015
    Messages : 3
    Par défaut Comprendre un fichier source Java (connexion Bluetooth ?)
    Bonjour,

    N'étant pas capable de lire (et donc d'interpréter) le Java Android, j'aimerais néanmoins obtenir quelques explications sur le code suivant :
    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
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    package xxxxxxxxxxxxxx;
     
    import android.bluetooth.BluetoothAdapter;
    import android.bluetooth.BluetoothDevice;
    import android.bluetooth.BluetoothGatt;
    import android.bluetooth.BluetoothGattCallback;
    import android.bluetooth.BluetoothGattCharacteristic;
    import android.bluetooth.BluetoothGattDescriptor;
    import android.bluetooth.BluetoothGattService;
    import android.bluetooth.BluetoothManager;
    import android.content.Context;
    import android.os.Handler;
    import android.util.Log;
    import java.util.HashSet;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Set;
    import java.util.Timer;
    import java.util.TimerTask;
    import java.util.UUID;
     
    class Ble
    {
        static interface BleObserver
        {
            public abstract void onBleConnected();
     
            public abstract void onBleDisconnected();
     
            public abstract void onBleReceiveText(String s, int i);
     
            public abstract void onBleTimeout();
        }
     
        private static final int BLE_TIMEOUT = 4000;
        private static final int REQUEST_TYPE_CONNECT = 1;
        private BluetoothAdapter bAdapter;
        private BluetoothGatt bGatt;
        private BluetoothManager bManager;
        private BluetoothGatt boardGatt;
        private Context context;
        private Set deviceSet;
        private Set differDeviceSet;
        private final BluetoothGattCallback gattCallback = new BluetoothGattCallback() {
     
        private static final String CHARACTERISTIC_ID = "442f1571-8a00-9a28-cbe1-e1d4212d53eb";
        private static final String CLIENT_CHARACTERISTIC_CONFIG = "00002902-0000-1000-8000-00805f9b34fb";
        private static final String SERVICE_ID = "442f1570-8a00-9a28-cbe1-e1d4212d53eb";
     
        private StringBuilder sb;
        final Ble this$0;
     
            public void onCharacteristicChanged(BluetoothGatt bluetoothgatt,  BluetoothGattCharacteristic bluetoothgattcharacteristic)
            {
                String s = bluetoothgattcharacteristic.getStringValue(0).trim();
                kawalog((new StringBuilder("onCharacteristicChanged:")).append(s).toString());
                sb.append(s);
                if (!s.endsWith("@"))
                {
                    observer.onBleReceiveText(sb.toString(), 1);
                    return;
                } else
                {
                    observer.onBleReceiveText(sb.toString(), 0);
                    sb = new StringBuilder();
                    return;
                }
            }
     
            public void onCharacteristicRead(BluetoothGatt bluetoothgatt,  BluetoothGattCharacteristic bluetoothgattcharacteristic, int i)
            {
                kawalog((new StringBuilder("onCharacteristicRead:")).append(i).toString());
            }
     
            public void onConnectionStateChange(BluetoothGatt bluetoothgatt, int i, int j)
            {
                kawalog((new StringBuilder("onConnectionStateChangea:")).append(i).toString());
                kawalog((new StringBuilder("newState:")).append(j).toString());
                if (j == 2)
                {
                    kawalog("connected");
                    bGatt.discoverServices();
                } else
                if (j == 0)
                {
                    kawalog("disconnected");
                    if (boardGatt == bluetoothgatt)
                    {
                        Boolean boolean1 = isConnected;
                        disConnect();
                        if (boolean1.booleanValue())
                        {
                            (new Thread(new Runnable() {
     
                                final _cls1 this$1;
     
                                public void run()
                                {
                                    try
                                    {
                                        Thread.sleep(500L);
                                        startScan();
                                        return;
                                    }
                                    catch (InterruptedException interruptedexception)
                                    {
                                        interruptedexception.printStackTrace();
                                    }
                                }
     
     
                {
                    this$1 = _cls1.this;
                    super();
                }
                            })).start();
                            return;
                        }
                    }
                }
            }
     
            public void onServicesDiscovered(BluetoothGatt bluetoothgatt, int i)
            {
                kawalog((new StringBuilder("onServicesDiscovered:")).append(i).toString());
                Iterator iterator = bluetoothgatt.getServices().iterator();
                BluetoothGattCharacteristic bluetoothgattcharacteristic;
    label0:
                do
                {
                    BluetoothGattService bluetoothgattservice;
                    do
                    {
                        if (!iterator.hasNext())
                        {
                            kawalog("\u9055\u3046\u30C7\u30D0\u30A4\u30B9");
                            differDeviceSet.add(bGatt.getDevice());
                            nextDevice();
                            return;
                        }
                        bluetoothgattservice = (BluetoothGattService)iterator.next();
                        kawalog((new  StringBuilder("service.getUuid().toString():")).append(bluetoothgattservice.getUuid().toString()).toString());
                    } while (bluetoothgattservice == null ||  bluetoothgattservice.getUuid() == null ||  !bluetoothgattservice.getUuid().toString().equals("442f1570-8a00-9a28-cbe1-e1d4212d53eb"));
                    Iterator iterator1 = bluetoothgattservice.getCharacteristics().iterator();
                    do
                    {
                        if (!iterator1.hasNext())
                        {
                            continue label0;
                        }
                        bluetoothgattcharacteristic = (BluetoothGattCharacteristic)iterator1.next();
                    } while  (!bluetoothgattcharacteristic.getUuid().toString().equalsIgnoreCase("442f1571-8a00-9a28-cbe1-e1d4212d53eb"));
                    break;
                } while (true);
                BluetoothGattDescriptor bluetoothgattdescriptor;
                if (bluetoothgatt.setCharacteristicNotification(bluetoothgattcharacteristic, true))
                {
                    kawalog("okNotification");
                    boardGatt = bluetoothgatt;
                    bAdapter.stopLeScan(scanCallback);
                    connected();
                } else
                {
                    kawalog("status: failNotification");
                }
                bluetoothgattdescriptor =  bluetoothgattcharacteristic.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
                bluetoothgattdescriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
                bluetoothgatt.writeDescriptor(bluetoothgattdescriptor);
            }
     
     
     
                {
                    this$0 = Ble.this;
                    super();
                    sb = new StringBuilder();
                }
        };
        private Boolean isActive;
        private Boolean isConnected;
        private BleObserver observer;
        private int requestType;
        private final android.bluetooth.BluetoothAdapter.LeScanCallback  scanCallback = new android.bluetooth.BluetoothAdapter.LeScanCallback() {
     
            final Ble this$0;
     
            public void onLeScan(BluetoothDevice bluetoothdevice, int i, byte abyte0[])
            {
                kawalog("onLeScan");
                if (bluetoothdevice.getName() == null)
                {
                    kawalog("nullDevice");
                    return;
                }
                kawalog((new StringBuilder("onLeScan:")).append(bluetoothdevice.getName()).toString());
                kawalog((new StringBuilder("scanRecord:")).append(abyte0.length).toString());
                kawalog("connectGatt");
                if (!differDeviceSet.contains(bluetoothdevice))
                {
                    deviceSet.add(bluetoothdevice);
                }
                if (bGatt != null)
                {
                    kawalog("\u691C\u7D22\u4E2D");
                    return;
                } else
                {
                    nextDevice();
                    return;
                }
            }
     
     
                {
                    this$0 = Ble.this;
                    super();
                }
        };
        private Timer timer;
     
        public Ble(Context context1, BleObserver bleobserver)
        {
            isConnected = Boolean.valueOf(false);
            isActive = Boolean.valueOf(false);
            deviceSet = new HashSet();
            differDeviceSet = new HashSet();
            context = context1;
            observer = bleobserver;
            bManager = (BluetoothManager)context1.getSystemService("bluetooth");
            bAdapter = bManager.getAdapter();
        }
     
        private void closeGatt()
        {
            if (bGatt != null)
            {
                kawalog("bGattClose");
                bGatt.disconnect();
                bGatt.close();
                bGatt = null;
            }
        }
     
        private void connected()
        {
            stopTimer();
            isConnected = Boolean.valueOf(true);
            observer.onBleConnected();
        }
     
        private void kawalog(String s)
        {
            Log.d("kawa", (new StringBuilder("kawalog:")).append(s).toString());
        }
     
        private void nextDevice()
        {
            if (deviceSet.isEmpty())
            {
                return;
            } else
            {
                closeGatt();
                final BluetoothDevice device = (BluetoothDevice)deviceSet.iterator().next();
                kawalog((new StringBuilder("nextDevice:")).append(device.getName()).toString());
                deviceSet.remove(device);
                (new Handler(context.getMainLooper())).post(new Runnable() {
     
                    final Ble this$0;
                    private final BluetoothDevice val$device;
     
                    public void run()
                    {
                        bGatt = device.connectGatt(context, false, gattCallback);
                    }
     
     
                {
                    this$0 = Ble.this;
                    device = bluetoothdevice;
                    super();
                }
                });
                return;
            }
        }
     
        private void onTime()
        {
            Log.d("kawalog", "onTime");
            isConnected = Boolean.valueOf(false);
            observer.onBleTimeout();
            closeGatt();
            stopScan();
        }
     
        private void startScan()
        {
            isActive = Boolean.valueOf(true);
            if (isConnected.booleanValue())
            {
                kawalog("\u63A5\u7D9A\u6E08\u307F");
                observer.onBleConnected();
                stopTimer();
                return;
            }
            startTimer();
            closeGatt();
            deviceSet = new HashSet();
            differDeviceSet = new HashSet();
            kawalog((new StringBuilder("isEnable:")).append(bAdapter.isEnabled()).toString());
            if (!bAdapter.isEnabled())
            {
                bAdapter.enable();
            }
            bAdapter.startLeScan(scanCallback);
        }
     
        private void startTimer()
        {
            stopTimer();
            Log.d("kawalog", "startTimer");
            timer = new Timer();
            timer.schedule(new TimerTask() {
     
                final Ble this$0;
     
                public void run()
                {
                    onTime();
                }
     
     
                {
                    this$0 = Ble.this;
                    super();
                }
            }, 4000L);
        }
     
        private void stopScan()
        {
            kawalog("stopScan");
            stopTimer();
            isActive = Boolean.valueOf(false);
            if (bAdapter == null)
            {
                return;
            } else
            {
                bAdapter.cancelDiscovery();
                bAdapter.stopLeScan(scanCallback);
                return;
            }
        }
     
        private void stopTimer()
        {
            kawalog("stopTimer");
            if (timer != null)
            {
                timer.cancel();
                timer = null;
            }
        }
     
        public void disConnect()
        {
            kawalog("-------disconnect-------");
            stopTimer();
            stopScan();
            if (!isConnected.booleanValue())
            {
                return;
            } else
            {
                isConnected = Boolean.valueOf(false);
                observer.onBleDisconnected();
                closeGatt();
                return;
            }
        }
     
        public void onPause()
        {
            bAdapter.stopLeScan(scanCallback);
        }
     
        public void onResume()
        {
            kawalog("---------------onResume!!!!!!");
            if (isActive.booleanValue()) goto _L2; else goto _L1
    _L1:
            kawalog("not active");
            if (!isConnected.booleanValue()) goto _L2; else goto _L3
    _L3:
            observer.onBleConnected();
            stopTimer();
    _L5:
            return;
    _L2:
            if (!isConnected.booleanValue())
            {
                kawalog("startScan");
                startScan();
                return;
            }
            if (true) goto _L5; else goto _L4
    _L4:
        }
     
        public void tryConnect()
        {
            requestType = 1;
            stopScan();
            startScan();
        }
    }
    Concernant plus particulièrement les lignes 46 à 48, je pense qu'il s'agit de code pour se connecter à un périphérique BLE (Bluetooth Low Energy) et que dans ces lignes on trouve en autre l'ID (adresse) du périphérique en question ?
    Est-ce bien cela ?

    Merci d'avance pour votre aide.

  2. #2
    Modérateur
    Avatar de kolodz
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2008
    Messages
    2 209
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 209
    Billets dans le blog
    52
    Par défaut
    Bonjour,

    Pour les 3 propriétés concernées, celles-ci ne sont pas utilisées dans la classe que tu nous montres. Sachant que ces propriétés sont private, il est très peu probable que celles-ci soit appelées dans une autre classe.
    Cela m'a l'air d'être des variables pour indiquer la version via des identifiants uniques.

    Il faudrait peut-être regarder ce que fait la classe mère de la classe anonyme (BluetoothGattCallback)
    Sinon :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
                kawalog((new StringBuilder("onConnectionStateChangea:")).append(i).toString());
                kawalog((new StringBuilder("newState:")).append(j).toString());
    kawalog ? C'est quoi l'origine de ce source ?
    Si une réponse vous a été utile pensez à
    Si vous avez eu la réponse à votre question, marquez votre discussion
    Pensez aux FAQs et aux tutoriels et cours.

  3. #3
    Futur Membre du Club
    Homme Profil pro
    Webmaster
    Inscrit en
    Janvier 2015
    Messages
    3
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 51
    Localisation : France, Marne (Champagne Ardenne)

    Informations professionnelles :
    Activité : Webmaster

    Informations forums :
    Inscription : Janvier 2015
    Messages : 3
    Par défaut
    Merci Patrick pour ta réponse.

    En fait pour être plus précis ce code est issue de fichiers source d'une application Android qui se connecte en automatique (sans détection manuelle, ni appairage) à un device dédié, via BLE (Bluetooth Low energy).

    L'idée finale serait de pouvoir connecter ce device équipé d'un module BLE depuis un autre terminal, par exemple un PC.
    Je suis donc à la recherche dans les sources de cette application Android des identifiants du device, du procédé de connexion et éventuellement du format des données échangées pour pouvoir le transposer sur PC.

    Je ne sais pas si je suis assez claire ?

  4. #4
    Membre Expert

    Homme Profil pro
    Consultant informatique
    Inscrit en
    Janvier 2004
    Messages
    2 301
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Suisse

    Informations professionnelles :
    Activité : Consultant informatique
    Secteur : Finance

    Informations forums :
    Inscription : Janvier 2004
    Messages : 2 301
    Par défaut
    Citation Envoyé par Nano_51 Voir le message
    Je suis donc à la recherche dans les sources de cette appli
    mouais plutôt dans le code compilé...

    admet que c'est du close-source que tu reverse engineer (jamais vu de projet qui contient des label avec des continue-to-label, ou de stringbuilder pour concaténer 2 strings, ou encore qui déclare les références synthétiques vers les classes mères dans les classes anonymes)...

    En plus, les constantes privées ne sont pas inutilisées, elles ont été inlinées par le compilateur (on retrouve les valeur dans le code)...

  5. #5
    Modérateur
    Avatar de kolodz
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2008
    Messages
    2 209
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 209
    Billets dans le blog
    52
    Par défaut
    L'appairage est utilisé pour qu'un matériel ne soit pas utilisé par n'importe qui. Il faudrait regarder le code de plus près, mais je n'ai rien vu de tel dans ce code. Ou ce n'est pas géré à ce niveau.

    D'après ce que je vois le nœud du scan est là :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
            bManager = (BluetoothManager)context1.getSystemService("bluetooth");
            bAdapter = bManager.getAdapter();
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    bAdapter.startLeScan(scanCallback);
    Après le code n'est pas super lisible. Il y beaucoup de ligne de "bruit"... en plus d'être "mal écrit" et d'avoir des classes dans des classes...

    Le mieux serait de faire passer en débug dans l'application que tu veux reproduire pour comprendre les différents mécanismes. Tu peux faire cela avec un câble USB et un environnement correctement configuré (si t'as les sources).
    Si une réponse vous a été utile pensez à
    Si vous avez eu la réponse à votre question, marquez votre discussion
    Pensez aux FAQs et aux tutoriels et cours.

  6. #6
    Futur Membre du Club
    Homme Profil pro
    Webmaster
    Inscrit en
    Janvier 2015
    Messages
    3
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 51
    Localisation : France, Marne (Champagne Ardenne)

    Informations professionnelles :
    Activité : Webmaster

    Informations forums :
    Inscription : Janvier 2015
    Messages : 3
    Par défaut
    Merci pour vos réponses... même si je n'ai pas tout compris !

Discussions similaires

  1. Réponses: 2
    Dernier message: 15/12/2014, 22h14
  2. Fichiers sources JAVA
    Par sybil dans le forum NetBeans
    Réponses: 0
    Dernier message: 08/01/2010, 01h56
  3. ça se passe comment l'organisation des fichiers source en java
    Par razily dans le forum Entrée/Sortie
    Réponses: 4
    Dernier message: 04/05/2009, 09h45
  4. Réponses: 0
    Dernier message: 30/01/2008, 23h45
  5. compilation d'un fichier source java à partir de DOS
    Par lion000 dans le forum Langage
    Réponses: 8
    Dernier message: 25/07/2007, 14h27

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