2017-04-21 309 views
0

我目前正在开发一款Android应用程序,旨在从腕带获取数据并处理它们以获取健康评估内容。而我被困在这个问题上,我不能让onCharacteristicRead()函数来工作...它只是不会得到所谓的...android ble无法调用onCharacteristicRead()

这里是相对于这部分代码:

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); 



    } 

}; 

所以任何人都可以帮助我解决这个问题吗?一直在寻找了一段时间的解决方案,仍然解决不了这个......非常感谢

回答

0

三件事:

  1. 你并不需要调用super.method()中每个回调。
  2. 你不应该调用onCharacteristic读你自己,一旦读取响应被调用readCharacteristic方法后,蓝牙堆栈会这样做。
  3. 如果存在多个特征,则您的代码将无法工作,因为Android的GATT api一次只能有一个未完成的操作。这意味着您需要等待onCharacteristicRead回调,然后再执行另一个readCharacteristic。 (如果您打印readCharacteristic的返回值,您将看到它在第一次打印时为真,但在以下时间为false。)