我正在做一个从传感器读取数据的程序。我在阅读这个特征时遇到了问题。我已经完成了debbug,我检查了方法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)
这是因为某些特征不可读。
您将看不到此属性:"读取排除"。
您应该注册,然后将调用您的onCharacteristicChanged回调。