2017-04-21 1 views
0

Ich bin derzeit auf ein Projekt entwickeln eine Android-App mit dem Ziel, Daten von Armbändern zu erwerben und verarbeiten, um etwas wie eine Gesundheits-Bewertung Zeug zu bekommen. Und ich bin auf dieses Problem fest, dass ich nicht die onCharacteristicRead() Funktion bekommen zu arbeiten ... Es wird einfach nicht aufgerufen ...android ble kann nicht anrufenCharacteristicRead()

Hier sind die Codes in Bezug auf diesen Teil:

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

     runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 
       String status; 
       switch (newState) { 
        case BluetoothGatt.STATE_CONNECTED: 
         lianjiezhuangtai.setText("connection succeed"); 
         bluetoothAdapter.stopLeScan(callback); 
         bluetoothGatt.discoverServices(); 
         break; 
        case BluetoothGatt.STATE_CONNECTING: 
         lianjiezhuangtai.setText("trying to connect"); 
         break; 
        case BluetoothGatt.STATE_DISCONNECTED: 
         lianjiezhuangtai.setText("connection lost"); 
         break; 
        case BluetoothGatt.STATE_DISCONNECTING: 
         lianjiezhuangtai.setText("disconnecting"); 
         break; 
       } 
       //pd.dismiss(); 
      } 
     }); 
    } 

    @Override 
    public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { 
     jibu.setText("run: trying to get steps"); 
     super.onCharacteristicRead(gatt, characteristic, status); 

     if (status == bluetoothGatt.GATT_SUCCESS) { 
      final int sum = 100;//characteristic.getValue()[0]; 

      runOnUiThread(new Runnable() { 
       @Override 
       public void run() { 
        jibu.setText("walked" + sum + "steps"); 
       } 
      }); 

      Log.e(TAG, "onCharacteristicRead: " + characteristic.getValue()[0]); 

     } 

    }   
    @Override 
    public void onServicesDiscovered(final BluetoothGatt gatt,final int status) { 
     super.onServicesDiscovered(gatt, status); 
     Log.i(TAG, "run: trying to get steps"); 
     if (status == bluetoothGatt.GATT_SUCCESS) { 
      final List<BluetoothGattService> services = bluetoothGatt.getServices(); 
      runOnUiThread(new Runnable() { 
       @Override 
       public void run() { 
        //List<String> serlist = new ArrayList<>(); 
        for (final BluetoothGattService bluetoothGattService : services) { 
         bluetoothGattServices = bluetoothGattService; 

         Log.i(TAG, "onServicesDiscovered: " + bluetoothGattService.getUuid()); 

         List<BluetoothGattCharacteristic> charc = bluetoothGattService.getCharacteristics(); 

         for (BluetoothGattCharacteristic charac : charc) { 
          Log.i(TAG, "run: " + charac.getUuid()); 
          // 00002a06-0000-1000-8000-00805f9b34fb 
        //  bluetoothGatt.setCharacteristicNotification(characteristic_zd,true); 
          if (charac.getUuid().toString().equals("0000ff06-0000-1000-8000-00805f9b34fb")) { 
           characteristic_zd = charac; 

          } else if (charac.getUuid().toString().equals("00002a06-0000-1000-8000-00805f9b34fb")) { 

           characteristic_jb = charac; 

           // bluetoothGatt.setCharacteristicNotification(characteristic_jb,true); 
           bluetoothGatt.readCharacteristic(characteristic_jb); 
           //sum = charac.getValue()[0]; 
           gattcallback.onCharacteristicRead(gatt, characteristic_jb, status); 
           Log.i(TAG, "run: trying to get steps"); 
          } else if (charac.getUuid().toString().equals("")) { 
          } 
         } 


         serviceslist.add(bluetoothGattService.getUuid().toString()); 

        } 
//      ArrayAdapter<String> adapter = new ArrayAdapter<String>(
//        MainActivity.this, android.R.layout.simple_expandable_list_item_1, serviceslist); 
        //list.setAdapter(adapter); 
       } 
      }); 
     } 

    } 



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


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



    } 

}; 

Kann mir also jemand mit diesem Problem helfen? Für Lösungen für eine Weile suchen und noch nicht lösen kann diesem ... Vielen Dank

Antwort

0

Drei Dinge:

  1. Sie brauchen nicht super.method() in jedem Rückruf zu nennen.
  2. Sie sollten nichtCharacteristicRead aufrufen, der Bluetooth-Stack wird das tun, sobald eine Leseantwort abgerufen wird, nachdem Sie die readCharacteristic-Methode aufgerufen haben.
  3. Wenn mehr als ein Merkmal vorhanden ist, funktioniert Ihr Code nicht, da GATT api von Android immer nur eine ausstehende Operation ausführen kann. Das bedeutet, dass Sie auf den Callback onCharacteristicRead warten müssen, bevor Sie einen anderen readCharacteristic ausführen können. (Wenn Sie den Rückgabewert von "readCharacteristic" drucken, sehen Sie, dass der Ausdruck "true" beim ersten Mal, aber falsch bei den folgenden Zeiten erfolgt.)
Verwandte Themen