我无法以onCharacteristicChanged()方法中的通知形式处理多个特征数据。我们可以通过onCharacteristicRead()
方法获得相同的数据吗?
在我的情况下,有9个特征,而6,7,8是通知特征,首先我们必须通知所有这些(6,7,8)特征。
之后,我得到6、7、8特征值1。然后,我再次在特征1中写入文本值,在特征6中再次获取值3。然后,在特征3中写入Unicode值,然后在特征6中以通知的形式得到2。在这种情况下,所有值出现在表单通知中。如何为onCharacteristicChanged()
中的4,5处理相同的过程?
我还将在下面共享onCharacteristicChanged()方法代码和活动处理代码,以便更好地理解。
onCharacteristicChanged()方法代码-
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
byte[] messageBytes = characteristic.getValue();
String messageString = null;
try {
messageString = new String(messageBytes);
String hexvalue =toHex(messageString);
RemoveZeroFromString removeZeroFromString = new RemoveZeroFromString(mContext);
String firstValue = removeZeroFromString.removeLeadingZeroes(hexvalue);
String secondValue = removeZeroFromString.removeTrailingZeroes(firstValue);
int bleResponce = Integer.parseInt(secondValue);
GlobalVariable.bleStatus = bleResponce;
} catch (Exception e) {
DebugLogUtil.log(Thread.currentThread().getStackTrace(), e.getMessage());
}
}
还有处理所有这些通知值的活动代码-
@Override
public void onStartButtonClick() {
try {
mButtonStart1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (mButtonStart1.getTag().toString()) {
case Constants.BLE_START:
if (mConnGatt != null) {
GlobalVariable.bleStatus = 0;
mConnGatt.setCharacteristicNotification(characteristic_6, true);
BluetoothGattDescriptor descriptor = characteristic_6.getDescriptor(UUID.fromString(BleUuid.DESCRIPTOR_CONFIG_UUID));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mConnGatt.writeDescriptor(descriptor);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (GlobalVariable.bleStatus == 1) {
System.out.println("***button click*****" + GlobalVariable.bleStatus);
mChargingConnectionPresenter.startCharging(mConnGatt, characteristic_1, getString(R.string.dialog_title_ble_authentication), getString(R.string.dialog_message_ble_authentication), true, 1, true);
} else if (GlobalVariable.bleStatus == 4) {
mChargingConnectionPresenter.startCharging(mConnGatt, characteristic_1, getString(R.string.dialog_title_ble_authentication), getString(R.string.dialog_message_ble_authentication), true, 1, true);
} else {
DialogUtil.showOkDialog(getActivity(), getString(R.string.dialog_title_alert), getString(R.string.dialog_message_ble_connection), getString(R.string.dialog_action_ok), false);
}
}
}, 1000);
} else {
DialogUtil.showOkDialog(getActivity(), getString(R.string.dialog_title_error), getString(R.string.dialog_message_ble_connection), getString(R.string.dialog_action_ok), true);
}
break;
case Constants.STOP:
if (mConnGatt != null) {
GlobalVariable.bleStatus = 0;
mConnGatt.setCharacteristicNotification(characteristic_6, true);
BluetoothGattDescriptor descriptor = characteristic_6.getDescriptor(UUID.fromString(BleUuid.DESCRIPTOR_CONFIG_UUID));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mConnGatt.writeDescriptor(descriptor);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (GlobalVariable.bleStatus == 2) {
System.out.println("***button click*****" + GlobalVariable.bleStatus);
mChargingConnectionPresenter.startCharging(mConnGatt, characteristic_1, getString(R.string.dialog_title_ble_authentication), getString(R.string.dialog_message_ble_authentication), true, 1, false);
} else if (GlobalVariable.bleStatus == 4) {
mChargingConnectionPresenter.startCharging(mConnGatt, characteristic_1, getString(R.string.dialog_title_ble_authentication), getString(R.string.dialog_message_ble_authentication), true, 1, false);
} else {
DialogUtil.showOkDialog(getActivity(), getString(R.string.dialog_title_alert), getString(R.string.dialog_message_ble_connection), getString(R.string.dialog_action_ok), false);
}
}
}, 1000);
} else {
DialogUtil.showOkDialog(getActivity(), getString(R.string.dialog_title_error), getString(R.string.dialog_message_ble_connection), getString(R.string.dialog_action_ok), true);
}
break;
}
}
});
mButtonStart2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (mButtonStart2.getTag().toString()) {
case Constants.BLE_START:
if (mConnGatt != null) {
GlobalVariable.bleStatus = 0;
mConnGatt.setCharacteristicNotification(characteristic_7, true);
BluetoothGattDescriptor descriptor = characteristic_7.getDescriptor(UUID.fromString(BleUuid.DESCRIPTOR_CONFIG_UUID));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mConnGatt.writeDescriptor(descriptor);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (GlobalVariable.bleStatus == 1) {
System.out.println("***button click*****" + GlobalVariable.bleStatus);
mChargingConnectionPresenter.startCharging(mConnGatt, characteristic_1, getString(R.string.dialog_title_ble_authentication), getString(R.string.dialog_message_ble_authentication), true, 2, true);
} else if (GlobalVariable.bleStatus == 4) {
mChargingConnectionPresenter.startCharging(mConnGatt, characteristic_1, getString(R.string.dialog_title_ble_authentication), getString(R.string.dialog_message_ble_authentication), true, 2, true);
} else {
DialogUtil.showOkDialog(getActivity(), getString(R.string.dialog_title_alert), getString(R.string.dialog_message_ble_connection), getString(R.string.dialog_action_ok), false);
}
}
}, 1000);
} else {
DialogUtil.showOkDialog(getActivity(), getString(R.string.dialog_title_error), getString(R.string.dialog_message_ble_connection), getString(R.string.dialog_action_ok), true);
}
break;
case Constants.STOP:
if (mConnGatt != null) {
GlobalVariable.bleStatus = 0;
mConnGatt.setCharacteristicNotification(characteristic_7, true);
BluetoothGattDescriptor descriptor = characteristic_7.getDescriptor(UUID.fromString(BleUuid.DESCRIPTOR_CONFIG_UUID));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mConnGatt.writeDescriptor(descriptor);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (GlobalVariable.bleStatus == 2) {
System.out.println("***button click*****" + GlobalVariable.bleStatus);
mChargingConnectionPresenter.startCharging(mConnGatt, characteristic_1, getString(R.string.dialog_title_ble_authentication), getString(R.string.dialog_message_ble_authentication), true, 2, false);
} else if (GlobalVariable.bleStatus == 4) {
mChargingConnectionPresenter.startCharging(mConnGatt, characteristic_1, getString(R.string.dialog_title_ble_authentication), getString(R.string.dialog_message_ble_authentication), true, 2, false);
} else {
DialogUtil.showOkDialog(getActivity(), getString(R.string.dialog_title_alert), getString(R.string.dialog_message_ble_connection), getString(R.string.dialog_action_ok), false);
}
}
}, 1000);
} else {
DialogUtil.showOkDialog(getActivity(), getString(R.string.dialog_title_error), getString(R.string.dialog_message_ble_connection), getString(R.string.dialog_action_ok), true);
}
break;
}
}
});
mButtonStart3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (mButtonStart3.getTag().toString()) {
case Constants.BLE_START:
if (mConnGatt != null) {
GlobalVariable.bleStatus = 0;
mConnGatt.setCharacteristicNotification(characteristic_8, true);
BluetoothGattDescriptor descriptor = characteristic_8.getDescriptor(UUID.fromString(BleUuid.DESCRIPTOR_CONFIG_UUID));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mConnGatt.writeDescriptor(descriptor);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (GlobalVariable.bleStatus == 1) {
System.out.println("***button click*****" + GlobalVariable.bleStatus);
mChargingConnectionPresenter.startCharging(mConnGatt, characteristic_1, getString(R.string.dialog_title_ble_authentication), getString(R.string.dialog_message_ble_authentication), true, 3, true);
} else if (GlobalVariable.bleStatus == 4) {
mChargingConnectionPresenter.startCharging(mConnGatt, characteristic_1, getString(R.string.dialog_title_ble_authentication), getString(R.string.dialog_message_ble_authentication), true, 3, true);
} else {
DialogUtil.showOkDialog(getActivity(), getString(R.string.dialog_title_alert), getString(R.string.dialog_message_ble_connection), getString(R.string.dialog_action_ok), false);
}
}
}, 1000);
} else {
DialogUtil.showOkDialog(getActivity(), getString(R.string.dialog_title_error), getString(R.string.dialog_message_ble_connection), getString(R.string.dialog_action_ok), true);
}
break;
case Constants.STOP:
if (mConnGatt != null) {
GlobalVariable.bleStatus = 0;
mConnGatt.setCharacteristicNotification(characteristic_8, true);
BluetoothGattDescriptor descriptor = characteristic_8.getDescriptor(UUID.fromString(BleUuid.DESCRIPTOR_CONFIG_UUID));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mConnGatt.writeDescriptor(descriptor);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (GlobalVariable.bleStatus == 2) {
mChargingConnectionPresenter.startCharging(mConnGatt, characteristic_1, getString(R.string.dialog_title_ble_authentication), getString(R.string.dialog_message_ble_authentication), true, 3, false);
} else if (GlobalVariable.bleStatus == 4) {
mChargingConnectionPresenter.startCharging(mConnGatt, characteristic_1, getString(R.string.dialog_title_ble_authentication), getString(R.string.dialog_message_ble_authentication), true, 3, false);
} else {
DialogUtil.showOkDialog(getActivity(), getString(R.string.dialog_title_alert), getString(R.string.dialog_message_ble_connection), getString(R.string.dialog_action_ok), false);
}
}
}, 1000);
} else {
DialogUtil.showOkDialog(getActivity(), getString(R.string.dialog_title_error), getString(R.string.dialog_message_ble_connection), getString(R.string.dialog_action_ok), true);
}
break;
}
}
});
}catch (Exception e){
DebugLogUtil.log(Thread.currentThread().getStackTrace(), e.getMessage());
}
}