2016-05-04 9 views
0

センサーからデータを読み取るプログラムを実行しています。私はその特性を読むことに問題があります。私はdebbugをやったし、私はonCharacteristicRead()メソッドが呼び出されていないことを確認し、私はなぜ理解していない。 誰でも私を助けることができますか? ありがとうございます!Android BLE:onCharacteristicRead()が呼び出されていない

これはクラスです:

private Inter inter; 
public final static String ACTION_DATA_AVAILABLE = 
     "com.example.bluetooth.le.ACTION_DATA_AVAILABLE"; 
public final static String EXTRA_DATA = 
     "com.example.bluetooth.le.EXTRA_DATA"; 
private static final String TAG = BluetoothLeService.class.getSimpleName(); 
//Services 
private BluetoothGattService heartService; 
//Characteristics 
private BluetoothGattCharacteristic heartCharact; 
private BluetoothGatt bluetoothGatt; 

private final BluetoothGattCallback gattCallBack = new BluetoothGattCallback() { 
    @Override 
    public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { 
     super.onCharacteristicWrite(gatt, characteristic, status); 
    } 

    @Override 
    public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { 
     super.onCharacteristicRead(gatt, characteristic, status); 
     Log.d(TAG, "Entrei no onCharacteristicRead."); 

     if (characteristic.getUuid().equals(DeviceConstants.HEART_RATE_MEASUREMENT)) { 
      int flag = characteristic.getProperties(); 
      int format = -1; 
      if ((flag & 0x01) != 0) { 
       format = BluetoothGattCharacteristic.FORMAT_UINT16; 
       Log.d(TAG, "Heart rate format UINT16."); 
      } else { 
       format = BluetoothGattCharacteristic.FORMAT_UINT8; 
       Log.d(TAG, "Heart rate format UINT8."); 
      } 
      final int heartRate = characteristic.getIntValue(format, 1); 
      Log.d(TAG, String.format("Received heart rate: %d", heartRate)); 
      getHeartRate(String.valueOf(heartRate)); 
     } 
    } 

    @Override 
    public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { 
     broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic); 
    } 

    @Override 
    public void onServicesDiscovered(BluetoothGatt gatt, int status) { 
     Log.d(TAG, "Entrou no Método: onServicesDiscovered"); 

     super.onServicesDiscovered(gatt, status); 
     //BluetoothGattCharacteristic characteristic; 

     if (status == BluetoothGatt.GATT_SUCCESS) { 

      for (int i = 0; i != bluetoothGatt.getServices().size(); i++) { 
       if (bluetoothGatt.getServices().get(i).getUuid().equals(DeviceConstants.HEART_RATE_SERVICE)) { 
        heartService = bluetoothGatt.getServices().get(i); 
       } 
      } 
      for (int j = 0; j != heartService.getCharacteristics().size(); j++) { 
       if (heartService.getCharacteristics().get(j).getUuid().equals(DeviceConstants.HEART_RATE_MEASUREMENT)) { 
        Log.d(TAG, "Entrou no if 2"); 
        heartService = bluetoothGatt.getService(DeviceConstants.HEART_RATE_SERVICE); 
        heartCharact = heartService.getCharacteristic(DeviceConstants.HEART_RATE_MEASUREMENT); 
        //heartCharact.setValue(1, BluetoothGattCharacteristic.FORMAT_UINT8, 0); 
        //bluetoothGatt.writeCharacteristic(heartCharact); 
        //bluetoothGatt.readCharacteristic(heartCharact); 
        Log.d(TAG, "WOOOOOW: " + heartCharact); 
       } 
      } 
     } 
     bluetoothGatt.readCharacteristic(heartCharact); 
    } 

    @Override 
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { 
     super.onConnectionStateChange(gatt, status, newState); 

     if (newState == BluetoothProfile.STATE_CONNECTING) { 
      Log.d(TAG, "Connecting to " + gatt.getDevice().getName() + ", please wait..."); 
     } 
     if (newState == BluetoothProfile.STATE_CONNECTED) { 
      Log.d(TAG, "Connected!!"); 
      bluetoothGatt.discoverServices(); 

     } 
    } 

    public void getHeartRate(String hr) { 
     inter.getHeartRate(hr); 
    } 

    private void broadcastUpdate(final String action, final BluetoothGattCharacteristic characteristic) { 
     final Intent intent = new Intent(action); 

     if (DeviceConstants.HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) { 
      int flag = characteristic.getProperties(); 
      int format = -1; 
      if ((flag & 0x01) != 0) { 
       format = BluetoothGattCharacteristic.FORMAT_UINT16; 
       Log.d(TAG, "Heart rate format UINT16."); 
      } else { 
       format = BluetoothGattCharacteristic.FORMAT_UINT8; 
       Log.d(TAG, "Heart rate format UINT8."); 
      } 
      final int heartRate = characteristic.getIntValue(format, 1); 
      Log.d(TAG, String.format("Received heart rate: %d", heartRate)); 
      intent.putExtra(EXTRA_DATA, String.valueOf(heartRate)); 
     } else { 
      final byte[] data = characteristic.getValue(); 
      if (data != null && data.length > 0) { 
       final StringBuilder stringBuilder = new StringBuilder(data.length); 
       for (byte byteChar : data) 
        stringBuilder.append(String.format("%02X ", byteChar)); 
       intent.putExtra(EXTRA_DATA, new String(data) + "\n" + stringBuilder.toString()); 
      } 
     } 
     sendBroadcast(intent); 
    } 
}; 

private BluetoothDevice device; 
private Context context; 

public BluetoothLeService(BluetoothDevice device, Context context, Inter inter) { 
    this.device = device; 
    this.context = context; 
    this.inter = inter; 
} 

public void connect() { 
    bluetoothGatt = device.connectGatt(context, false, gattCallBack); 
    Log.d(TAG, "Entrou no Método: connect"); 
} 

public void disconnect() { 
    bluetoothGatt.disconnect(); 
} 
+0

私はいくつかの情報が不足していて、他の情報が多すぎます。あなたは最小限の例にコードをトリミングする必要がありますし、欠けている情報は何をしようとしています。そこに心拍数だけでなく、ブルートゥース。もう少し多くの情報が遠くに行きます。ありがとう。 –

+0

ようこそスタックオーバーフロー!私はコードから不要な空白行を削除して読みやすくしました。問題を特定するために、コメントに記載されている情報を追加してください。がんばろう! – cramopy

答えて

0

いくつかの特徴が読み取れるものではありませんので、それはです。

は、このページを見ている:https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.service.heart_rate.xml

をそして、あなたは、このプロパティを読み取ることができません表示されます:「除外読みます」。

登録する必要があります。すると、onCharacteristicChangedコールバックが呼び出されます。

関連する問題