2017-10-12 24 views
0

Je suis en train de développer une application Android qui doit connecter +/- 120 serveurs GATT BLE par jour, dont au moins 3 sont en service exactement avec les mêmes services et caractéristiques. J'ai été experiecing toutes sortes de problèmes avec Android BLE, même si suivant toutes les normes et les modèles par défaut.Android BLE onCharacteristicLire le mauvais état qui fait que Bluetooth a cessé de fonctionner

Il m'arrive de devoir, au moins une fois, réinitialiser la tablette aux paramètres par défaut, car «Bluetooth a cessé de fonctionner», dont je ne trouve pas la cause. Cela a été très frustrant dans lequel je ne peux pas supporter ce type de maintenance tous les jours (hard reset). Au début, j'ai essayé BLE pour Android SDK 18, dans lequel se comportait de manière très étrange, montrant de mauvais résultats avec juste une journée d'utilisation. J'ai donc commencé à utiliser la version Lollipop de la manipulation d'Android BLE, qui montre quelques améliorations.

Je rencontre ces problèmes depuis des jours maintenant et tous parce que j'obtiens des résultats inattendus, par exemple sur des retours sur CharacterRead mais les données sont nulles. Ou par exemple, j'appelle discoverServices() après onConnectionStateChanged avec STATE_CONNECTED, donc après quelque temps onServicesDiscovered() est appelé mais getServices() renvoie null. Tous ces problèmes se produisent uniquement après la déconnexion du premier serveur. Le premier serveur qu'il connecte lit et écrit tout correctement, mais ensuite je déconnecte() et onConnectionStateChanged(), donc je ferme(). Alors je me connecte au serveur suivant, et je commence à avoir ces problèmes (toutes les opérations du GATT sont sur le thread principal, toutes les connexions sont faites correctement, et correctement éliminées), donc je dois me déconnecter et me connecter attributs correctement.

J'ai 3 caractéristiques à lire, et après avoir lu la 3ème, je commence à écrire 3 caractéristiques.

J'ai parcouru les journaux bluetooth et je peux voir des choses inattendues se produire. Ce journal est lorsque l'application lit tout correctement (il est toujours et seul le premier serveur GATT l'application se connecte à):

10-11 09:08:05.350 15989-15989/efp.android.sample D/BluetoothAdapter: STATE_ON 
10-11 09:08:05.350 15989-15989/efp.android.sample D/BluetoothAdapter: STATE_ON 
10-11 09:08:05.350 15989-15989/efp.android.sample D/BluetoothLeScanner: Start Scan 
10-11 09:08:05.351 15989-15989/efp.android.sample D/BluetoothAdapter: STATE_ON 
10-11 09:08:05.352 15989-15989/efp.android.sample D/BluetoothAdapter: STATE_ON 
10-11 09:08:05.353 15989-15989/efp.android.sample D/BluetoothAdapter: STATE_ON 
10-11 09:08:05.354 15989-15989/efp.android.sample D/BluetoothAdapter: STATE_ON 
10-11 09:08:05.362 15989-15994/efp.android.sample I/art: Do partial code cache collection, code=48KB, data=60KB 
10-11 09:08:05.362 15989-15994/efp.android.sample I/art: After code cache collection, code=48KB, data=60KB 
10-11 09:08:05.362 15989-15994/efp.android.sample I/art: Increasing code cache capacity to 256KB 
10-11 09:08:05.457 15989-16002/efp.android.sample D/BluetoothLeScanner: onClientRegistered() - status=0 clientIf=5 mClientIf=0 
10-11 09:08:05.513 15989-15989/efp.android.sample W/IInputConnectionWrapper: getCursorCapsMode on inactive InputConnection 
10-11 09:08:05.681 15989-16023/efp.android.sample D/ScanRecord: parseFromBytes 
10-11 09:08:05.681 15989-15989/efp.android.sample D/LAURASTAR_BLE: onScanResult 
10-11 09:08:05.682 15989-15989/efp.android.sample D/BluetoothAdapter: STATE_ON 
10-11 09:08:05.683 15989-15989/efp.android.sample D/BluetoothAdapter: STATE_ON 
10-11 09:08:05.684 15989-15989/efp.android.sample D/BluetoothAdapter: STATE_ON 
10-11 09:08:05.685 15989-15989/efp.android.sample D/BluetoothAdapter: STATE_ON 
10-11 09:08:05.685 15989-15989/efp.android.sample D/BluetoothLeScanner: Stop Scan 
10-11 09:08:05.692 15989-15989/efp.android.sample D/BluetoothGatt: connect() - device: F5:8E:C1:91:DC:CC, auto: false 
10-11 09:08:05.692 15989-15989/efp.android.sample D/BluetoothAdapter: isSecureModeEnabled 
10-11 09:08:05.699 15989-15989/efp.android.sample D/BluetoothGatt: registerApp() 
10-11 09:08:05.699 15989-15989/efp.android.sample D/BluetoothGatt: registerApp() - UUID=ebf41986-8aad-4796-a537-7319c00f0f46 
10-11 09:08:05.802 15989-16002/efp.android.sample D/BluetoothGatt: onClientRegistered() - status=0 clientIf=5 
10-11 09:08:06.332 15989-16001/efp.android.sample D/LAURASTAR_BLE: onConnectionStateChange: 
10-11 09:08:06.332 15989-16001/efp.android.sample D/BluetoothGatt: discoverServices() - device: F5:8E:C1:91:DC:CC 
10-11 09:08:06.343 15989-16002/efp.android.sample D/BluetoothGatt: onSearchComplete() = Device=F5:8E:C1:91:DC:CC Status=0 
10-11 09:08:06.343 15989-16002/efp.android.sample D/LAURASTAR_BLE: onServicesDiscovered: 
10-11 09:08:07.143 15989-16002/efp.android.sample W/BluetoothGatt: onCharacteristicRead() - Device=F5:8E:C1:91:DC:CC handle=51 Status=0 
10-11 09:08:07.233 15989-16002/efp.android.sample W/BluetoothGatt: onCharacteristicRead() - Device=F5:8E:C1:91:DC:CC handle=14 Status=0 
10-11 09:08:07.324 15989-16023/efp.android.sample W/BluetoothGatt: onCharacteristicRead() - Device=F5:8E:C1:91:DC:CC handle=23 Status=0 
10-11 09:08:08.136 15989-16023/efp.android.sample D/BluetoothGatt: cancelOpen() - device: F5:8E:C1:91:DC:CC 
10-11 09:08:08.146 15989-16001/efp.android.sample D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=5 device=F5:8E:C1:91:DC:CC 
10-11 09:08:08.151 15989-16001/efp.android.sample D/LAURASTAR_BLE: onConnectionStateChange: 
10-11 09:08:08.265 15989-15989/efp.android.sample D/BluetoothGatt: close() 
10-11 09:08:08.266 15989-15989/efp.android.sample D/BluetoothGatt: unregisterApp() - mClientIf=5 

Après la déconnexion du premier serveur du GATT pour se connecter au deuxième serveur du GATT, je dois constamment déconnecter et reconnecter pour lire les caractéristiques, bien qu'il lise toujours correctement la première caractéristique.

Voici le journal des problèmes de lecture ... Si la lecture comporte des erreurs, je dois me déconnecter et me reconnecter (et même si la lecture a des erreurs après la reconnexion, elle se ferme, analyse et réessaie).

10-12 08:03:48.133 30409-30409/efp.android.sample D/BluetoothAdapter: STATE_ON 
10-12 08:03:48.133 30409-30409/efp.android.sample D/BluetoothAdapter: STATE_ON 
10-12 08:03:48.134 30409-30409/efp.android.sample D/BluetoothLeScanner: Start Scan 
10-12 08:03:48.134 30409-30409/efp.android.sample D/BluetoothAdapter: STATE_ON 
10-12 08:03:48.137 30409-30409/efp.android.sample D/BluetoothAdapter: STATE_ON 
10-12 08:03:48.260 30409-31121/efp.android.sample D/BluetoothLeScanner: onClientRegistered() - status=0 clientIf=6 mClientIf=0 
10-12 08:03:48.391 30409-30422/efp.android.sample D/ScanRecord: parseFromBytes 
10-12 08:03:48.395 30409-30409/efp.android.sample D/LAURASTAR_BLE: onScanResult 
10-12 08:03:48.397 30409-30409/efp.android.sample D/BluetoothAdapter: STATE_ON 
10-12 08:03:48.397 30409-30409/efp.android.sample D/BluetoothAdapter: STATE_ON 
10-12 08:03:48.398 30409-30409/efp.android.sample D/BluetoothAdapter: STATE_ON 
10-12 08:03:48.399 30409-30409/efp.android.sample D/BluetoothAdapter: STATE_ON 
10-12 08:03:48.399 30409-30409/efp.android.sample D/BluetoothLeScanner: Stop Scan 
10-12 08:03:48.410 30409-30409/efp.android.sample D/BluetoothGatt: connect() - device: F5:8E:C1:91:DC:CC, auto: false 
10-12 08:03:48.410 30409-30409/efp.android.sample D/BluetoothAdapter: isSecureModeEnabled 
10-12 08:03:48.411 30409-30409/efp.android.sample D/BluetoothGatt: registerApp() 
10-12 08:03:48.411 30409-30409/efp.android.sample D/BluetoothGatt: registerApp() - UUID=ce6919e5-e4f7-4180-a313-557e168a7c5a 
10-12 08:03:48.514 30409-30422/efp.android.sample D/BluetoothGatt: onClientRegistered() - status=0 clientIf=6 
10-12 08:03:48.745 30409-31121/efp.android.sample D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=6 device=F5:8E:C1:91:DC:CC 
10-12 08:03:48.755 30409-31121/efp.android.sample D/BluetoothGatt: discoverServices() - device: F5:8E:C1:91:DC:CC 
10-12 08:03:48.915 30409-30414/efp.android.sample I/art: Do partial code cache collection, code=19KB, data=28KB 
10-12 08:03:48.916 30409-30414/efp.android.sample I/art: After code cache collection, code=19KB, data=28KB 
10-12 08:03:48.916 30409-30414/efp.android.sample I/art: Increasing code cache capacity to 128KB 
10-12 08:03:48.982 30409-30422/efp.android.sample D/BluetoothGatt: onClientConnParamsChanged() - Device=F5:8E:C1:91:DC:CC interval=6 status=0 (I DONT THINK THIS SHOULD HAPPEN) 
10-12 08:03:49.688 30409-30421/efp.android.sample D/BluetoothGatt: onSearchComplete() = Device=F5:8E:C1:91:DC:CC Status=0 
10-12 08:03:49.689 30409-30421/efp.android.sample D/LAURASTAR_BLE: onServicesDiscovered: 
10-12 08:03:49.711 30409-30422/efp.android.sample D/BluetoothGatt: onClientConnParamsChanged() - Device=F5:8E:C1:91:DC:CC interval=36 status=0 
10-12 08:03:50.467 30409-31121/efp.android.sample W/BluetoothGatt: onCharacteristicRead() - Device=F5:8E:C1:91:DC:CC handle=51 Status=0 
10-12 08:03:50.557 30409-30422/efp.android.sample W/BluetoothGatt: onCharacteristicRead() - Device=F5:8E:C1:91:DC:CC handle=14 Status=5 (status NOT SUCCESS ...) 
10-12 08:03:50.830 30409-30422/efp.android.sample D/BluetoothGatt: onClientConnParamsChanged() - Device=F5:8E:C1:91:DC:CC interval=6 status=0 
10-12 08:03:51.533 30409-31121/efp.android.sample W/BluetoothGatt: onCharacteristicRead() - Device=F5:8E:C1:91:DC:CC handle=14 Status=137 (status NOT SUCCESS ...) 
10-12 08:03:51.533 30409-31121/efp.android.sample D/LAURASTAR_BLE: Data is empty. (null) 
10-12 08:03:51.533 30409-31121/efp.android.sample D/BluetoothGatt: cancelOpen() - device: F5:8E:C1:91:DC:CC 
10-12 08:03:51.561 30409-30422/efp.android.sample D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=6 device=F5:8E:C1:91:DC:CC 
10-12 08:03:51.565 30409-30422/efp.android.sample D/LAURASTAR_BLE: onConnectionStateChange: 
10-12 08:03:51.565 30409-30422/efp.android.sample D/BluetoothAdapter: isSecureModeEnabled 
10-12 08:03:51.609 30409-31121/efp.android.sample D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=6 device=F5:8E:C1:91:DC:CC 
10-12 08:03:51.610 30409-31121/efp.android.sample D/LAURASTAR_BLE: onConnectionStateChange: 
10-12 08:03:51.610 30409-31121/efp.android.sample D/BluetoothGatt: discoverServices() - device: F5:8E:C1:91:DC:CC 
10-12 08:04:01.091 30409-31121/efp.android.sample D/BluetoothGatt: onSearchComplete() = Device=F5:8E:C1:91:DC:CC Status=0 
10-12 08:04:01.091 30409-31121/efp.android.sample D/LAURASTAR_BLE: onServicesDiscovered: 
10-12 08:04:02.048 30409-31121/efp.android.sample W/BluetoothGatt: onCharacteristicRead() - Device=F5:8E:C1:91:DC:CC handle=51 Status=0 
10-12 08:04:02.438 30409-30421/efp.android.sample W/BluetoothGatt: onCharacteristicRead() - Device=F5:8E:C1:91:DC:CC handle=14 Status=0 
10-12 08:04:02.829 30409-30421/efp.android.sample W/BluetoothGatt: onCharacteristicRead() - Device=F5:8E:C1:91:DC:CC handle=23 Status=0 
10-12 08:04:02.898 30409-30414/efp.android.sample I/art: Do partial code cache collection, code=48KB, data=61KB 
10-12 08:04:02.899 30409-30414/efp.android.sample I/art: After code cache collection, code=48KB, data=61KB 
10-12 08:04:02.899 30409-30414/efp.android.sample I/art: Increasing code cache capacity to 256KB 
10-12 08:04:04.389 30409-30422/efp.android.sample D/BluetoothGatt: cancelOpen() - device: F5:8E:C1:91:DC:CC 
10-12 08:04:04.402 30409-30421/efp.android.sample D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=6 device=F5:8E:C1:91:DC:CC 
10-12 08:04:04.406 30409-30421/efp.android.sample D/LAURASTAR_BLE: onConnectionStateChange: 
10-12 08:04:04.502 30409-30409/efp.android.sample V/InputMethodManager: Starting input: [email protected] nm : efp.android.sample [email protected] 
10-12 08:04:04.502 30409-30409/efp.android.sample I/InputMethodManager: [IMM] startInputInner - mService.startInputOrWindowGainedFocus 
10-12 08:04:04.503 30409-30409/efp.android.sample D/InputTransport: Input channel constructed: fd=70 
10-12 08:04:04.503 30409-30409/efp.android.sample D/InputTransport: Input channel destroyed: fd=73 
10-12 08:04:04.519 30409-30409/efp.android.sample D/BluetoothGatt: close() 
10-12 08:04:04.520 30409-30409/efp.android.sample D/BluetoothGatt: unregisterApp() - mClientIf=6 

Dans ce journal, cela est visible, après avoir lu la première caractéristique succčs:

W/BluetoothGatt: onCharacteristicRead() - Device=F5:8E:C1:91:DC:CC handle=14 Status=5 

Et puis onClientConnParamsChanged est appelée après discoverServices() est appelée, et avant onServicesDiscovered() est appelée.

Tout mon code semble être correct ... Donc, la logique est la suivante:

Commencez par la réception souhaitée l'adresse MAC du serveur de l'utilisateur, de sorte que l'application peut analyser et trouver.

J'ai un TextWatcher implémenté dans un EditText, donc quand l'utilisateur insère une adresse MAC, l'application recherche des périphériques BLE avec l'adresse insérée.Après l'insertion d'une adresse MAC par l'utilisateur, startScan() est démarré.

String macToFind = ""; 
private class ScanIronFieldsTextWatcher implements TextWatcher { 

    View view; 
    public ScanIronFieldsTextWatcher(View view) { this.view = view; } 

    @Override 
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {} 

    @Override 
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {} 


    public void afterTextChanged(Editable editable) { 

     switch (view.getId()) { 
      case R.id.txt_mac_address: { 
       String originalText = txtMacAddress.getText().toString().toUpperCase(); 
       String macToFind = Tools.getValidMacAddress(originalText); 
       if(mConnectedGatt != null && mConnectedGatt.getDevice().getAddress().equals(macToFind)) { 
        mConnectedGatt.connect(); 
       } 
       else { 
        if(mConnectedGatt != null) { 
         mConnectedGatt.close(); 
         mConnectedGatt = null; 
        } 

        //Search mac address 
        writeToActivityLog("Looking for " + macToFind +"...", LogAdapter.MESSAGE_TYPE_NORMAL); 
        hideKeyboard(); 
        startScan(); 
       } 
      } 
     } 
    } 
} 

startScan()

private void startScan() { 
     if (!mBluetoothAdapter.isEnabled()) { 
      writeToActivityLog("Bluetooth is off!", LogAdapter.MESSAGE_TYPE_ERROR); 
      return; 
     } 
     try { 
      ScanFilter scanFilter = new ScanFilter.Builder().setDeviceAddress(macToFind).build(); 
      ArrayList<ScanFilter> filters = new ArrayList<ScanFilter>(); 
      filters.add(scanFilter); 

      ScanSettings settings = new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_BALANCED).build(); 
      mBluetoothAdapter.getBluetoothLeScanner().startScan(filters, settings, mScanCallback); 


      retryingHandler = new Handler(); 
      retryingHandler.postDelayed(new Runnable() { 
       @Override 
       public void run() { 
        showProgress(false); 
        stopScan(); 

        if (mDevices.size() == 0) { 
         writeToActivityLog("Server not found.", LogAdapter.MESSAGE_TYPE_ERROR); 
        } else { 
         mDevices.clear(); 
        } 
       } 
      }, SCAN_TIME); 
     } catch (IllegalArgumentException e) { 
      writeToActivityLog("Invalid Address!", LogAdapter.MESSAGE_TYPE_ERROR); 
     } 
    } 

mScanCallback:

private ScanCallback mScanCallback = new ScanCallback() { 
     @Override 
     public void onScanResult(int callbackType, ScanResult result) { 
      Log.d(TAG, "onScanResult"); 
      processResult(result); 
     } 

     @Override 
     public void onBatchScanResults(List<ScanResult> results) { 
      Log.d(TAG, "onBatchScanResults: "+results.size()+" results"); 
      for (ScanResult result : results) { 
       processResult(result); 
      } 
     } 

     @Override 
     public void onScanFailed(int errorCode) { Log.w(TAG, "LE Scan Failed: "+errorCode); } 

     private void processResult(final ScanResult result) { 
      runOnUiThread(new Runnable() { 
       @Override 
       public void run() { 
        if(mConnectedGatt == null) { 
         stopScan(); 
         final BluetoothDevice device = result.getDevice(); 
         mDevices.put(device.hashCode(), device); 

         writeToActivityLog("Server found!", LogAdapter.MESSAGE_TYPE_NORMAL); 
         writeToActivityLog("Connecting to " + macToFind + "...", LogAdapter.MESSAGE_TYPE_NORMAL); 

         mConnectedGatt = device.connectGatt(NewDeviceControlActivity.this, false, mGattCallback); 
         retryingHandler.removeCallbacksAndMessages(null); 
        } 
       } 
      }); 
     } 
    }; 

mGattCallback

private BluetoothGattCallback mGattCallback = new BluetoothGattCallback() { 
     @Override 
     public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { 
      super.onConnectionStateChange(gatt, status, newState); 

      if (newState == BluetoothProfile.STATE_CONNECTED) { 
       writeToActivityLog("Connected.", LogAdapter.MESSAGE_TYPE_SUCCESS); 
       mConnected = true; 

       if(!gatt.discoverServices()) { 
        writeToActivityLog("Impossible discoverServices().", LogAdapter.MESSAGE_TYPE_ERROR); 
        if(!retrying) 
         retrying = true; 
        mConnectedGatt.disconnect(); 
       } 
      } 
      else if(newState == BluetoothProfile.STATE_DISCONNECTED) { 

       writeToActivityLog("Disconnected.", LogAdapter.MESSAGE_TYPE_NORMAL); 
       if(!currentIron.hasCompletedSetup()) { 
        showProgress(true); 

        if(retrying) { 
         writeToActivityLog("Trying again...", LogAdapter.MESSAGE_TYPE_NORMAL); 
         if(mConnectedGatt != null) 
          mConnectedGatt.connect(); 
        } 
        else { 
         writeToActivityLog("Trying again but also scanning again... (r)", LogAdapter.MESSAGE_TYPE_NORMAL); 
         retrying = false; 
         if(mConnectedGatt != null) 
          mConnectedGatt.close(); 
         mHandler.postDelayed(new Runnable() { 
          @Override 
          public void run() { 
           startScan(); 
          } 
         }, 2000); 
        } 
       } 
      } 
     } 

     @Override 
     public void onServicesDiscovered(final BluetoothGatt gatt, int status) { 
      super.onServicesDiscovered(gatt, status); 
      Log.d(TAG, "onServicesDiscovered:"); 

      mHandler.postDelayed(new Runnable() { 
       @Override 
       public void run() { 
        startObtainingIronData(gatt); 
       } 
      }, 700); 
     } 

     @Override 
     public void onCharacteristicRead(BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic, final int status) { 
      super.onCharacteristicRead(gatt, characteristic, status); 


      byte[] data = characteristic.getValue(); 
      if(status != BluetoothGatt.GATT_SUCCESS) { 
       runOnUiThread(new Runnable() { @Override public void run() {Toast.makeText(NewDeviceControlActivity.this, ("BluetoothGatt: Response != GATT_SUCCESS ("+status+")."), Toast.LENGTH_SHORT).show();}}); 
      } 

      if (characteristic.getUuid().toString().contains(Iron.IRON_CODES[4])) { 
       //First characteristic read succesfully 
       final String version = new String(data); 
       runOnUiThread(new Runnable() { @Override public void run() {Toast.makeText(NewDeviceControlActivity.this, ("Version "+version), Toast.LENGTH_SHORT).show();}}); 

       //Request to read second characteristic 
       BluetoothGattCharacteristic characteristic2 = getCharacteristic(gatt, Iron.IRON_CODES[0]); 
       gatt.readCharacteristic(characteristic2); 
      } 
      else if (characteristic.getUuid().toString().contains(Iron.IRON_CODES[0])) { 
       //Read second characteristic 
       if(data == null || data.length == 0) { //returns with status=137 
        Log.d(TAG, "Data is empty " + (data == null ? "(null)" : data));//This ALWAYS happens except the first server the app connects too 
        writeToActivityLog("Data is empty. "+ (data == null ? "(null)" : data), LogAdapter.MESSAGE_TYPE_ERROR); 
        if(!retrying) 
         retrying = true; 
        mConnectedGatt.disconnect(); 
        return; 
       } 
       else { 
        if (data[2] == 4) { 
         //Request to read third characteristic 
         BluetoothGattCharacteristic characteristic2 = getCharacteristic(gatt, Iron.IRON_CODES[1]); 
         if(characteristic2 != null) 
          gatt.readCharacteristic(characteristic2); 
         else { 
          writeToActivityLog("Impossible readCharacteristic Machine Type" , LogAdapter.MESSAGE_TYPE_ERROR); 
          if(!retrying) 
           retrying = true; 

          mConnectedGatt.disconnect(); 
         } 
        } 
       } 
      } 
      else if (characteristic.getUuid().toString().contains(Iron.IRON_CODES[1])) { 
       //Read third characteristic 
       if(data[0] == 0) { 
        BluetoothGattCharacteristic characteristic2 = getCharacteristic(gatt, Iron.IRON_CODES[2]); 
        long now = System.currentTimeMillis(); 
        byte[] nowBytes = new byte[]{(byte) ((int) (now & 255L)), (byte) ((int) (now >> 8)), (byte) ((int) (now >> 16)), (byte) ((int) (now >> 24))}; 
        characteristic2.setValue(nowBytes); 

        writeToActivityLog("Read all information succesfully.", LogAdapter.MESSAGE_TYPE_NORMAL); 

        //Request to write first characteristic 
        if (!gatt.writeCharacteristic(characteristic2)){ 
         writeToActivityLog("Impossible to write timestamp.", LogAdapter.MESSAGE_TYPE_ERROR); 
         if(!retrying) 
          retrying = true; 
         mConnectedGatt.disconnect(); 
        } 

       } 
      } 
     } 

     @Override 
     public void onCharacteristicWrite(final BluetoothGatt gatt, BluetoothGattCharacteristic characteristic2, int status) { 
      super.onCharacteristicWrite(gatt, characteristic2, status); 

      if(status == BluetoothGatt.GATT_SUCCESS) { 
       showProgress(false); 

       if(characteristic2.getUuid().toString().contains(Iron.IRON_CODES[2])) { 

        //Wrote first characteristic 
        String intToWrite = "0x01"; 
        BluetoothGattCharacteristic characteristic = getCharacteristic(gatt, Iron.IRON_CODES[1]); 
        characteristic.setValue(Integer.decode(intToWrite), BluetoothGattCharacteristic.FORMAT_UINT8, 0); 

        //Request to write second characteristic 
        if (!gatt.writeCharacteristic(characteristic)) { 
         writeToActivityLog("Impossible to write first characteristic!.", LogAdapter.MESSAGE_TYPE_ERROR); 
         if(!retrying) 
          retrying = true; 
         mConnectedGatt.disconnect(); 
        } 
       } 
       else if(characteristic2.getUuid().toString().contains(Iron.IRON_CODES[1])) { 

         //Wrote second characteristic 
         service.sendData(), new Callback<String>() { 
            @Override 
            public void onResponse(Call<String> call, Response<String> response) { 

              //Write third characteristic 
              BluetoothGattCharacteristic characteristic = getCharacteristic(gatt, Iron.IRON_CODES[3]); 
              characteristic.setValue(0x40, android.bluetooth.BluetoothGattCharacteristic.FORMAT_UINT8, 0); 
              gatt.writeCharacteristic(characteristic); 


            } 
            @Override 
            public void onFailure(Call<String> call, Throwable t) { 

             if(mConnectedGatt != null) 
              mConnectedGatt.disconnect(); 
            } 
           }); 
         } 
       } 
       else if(characteristic2.getUuid().toString().contains(Iron.IRON_CODES[3])) { 
        //Wrote third characteristic 
        writeToActivityLog("Data written successfully.", LogAdapter.MESSAGE_TYPE_SUCCESS); 

        if(mConnectedGatt != null) 
         mConnectedGatt.disconnect(); 
       } 

      } 
     } 
    }; 

J'ai tri ed exécutant toutes les opérations mConnectedGatt dans runOnUIThread, mais avec les mêmes résultats. Toutes les caractéristiques UUID sont correctes, et les caractéristiques étant écrites NE SONT PAS les mêmes en cours de lecture.

Je n'ai pas le problème du serveur du GATT qui "marche" loin de l'application, ni d'être trop loin. Je cours le logiciel sur trois Samsung Galaxy Tab A 10.1 2016, qui tous renvoient les mêmes résultats.

donc mes résultats sont essentiellement ceux-ci:

attendu (ce qui se produit uniquement avec le premier serveur de l'application se connecte à):

  • Vous recherchez MAC
  • serveur trouvé!
  • Connexion à MAC
  • Connecté.
  • (lecture première caractéristique) Version:
  • (lecture seconde caractéristique)
  • (lecture troisième caractéristique)
  • (écrire première caractéristique)
  • (écrire seconde caractéristique)
  • (écrire troisième caractéristique)
  • Déconnecté.

Qu'arrive-t- (tente de se connecter à un autre serveur):

  • Vous recherchez MAC
  • serveur trouvé!
  • Connexion à MAC
  • Connecté.
  • (lire la première caractéristique) Version:
  • Les données sont vides! (null)
  • Déconnecté.
  • Essayer à nouveau ...
  • Connecté.
  • (lecture première caractéristique) Version:
  • (lecture seconde caractéristique)
  • (lecture troisième caractéristique)
  • (écrire première caractéristique)
  • (écrire seconde caractéristique)
  • (écrire troisième caractéristique)
  • Déconnecté.

Et je devine à beaucoup de reconnexions est ce qui cause le «Bluetooth a cessé de fonctionner». Je n'arrive pas à comprendre pourquoi cela arrive!

S'il vous plaît, si quelqu'un peut passer un peu de temps pour me aider à comprendre ce qui se passe, je serais tellement apprécié ... Merci

+1

Avez-vous ajouté de nouveaux services sur votre projet? Si c'est le cas, vous pouvez essayer d'effacer le cache bluetooth. Aller à paramètres-> applications cliquez sur l'icône plus en haut à droite. cliquez sur les applications du système. sélectionnez Bluetooth dans la liste. Effacer le stockage. (parfois il ne vide pas le cache à la première fois, cliquez deux fois sur ce bouton) J'espère que cela résoudra le problème – hasbi

+0

@hasbi Que voulez-vous dire par de nouveaux services? – Firecat

+0

Je veux dire ajouter une nouvelle caractéristique – hasbi

Répondre