Я не могу обработать несколько характерных данных в форме уведомления в методе onCharacteristicChanged (). Можем ли мы получить те же данные в методе onCharacteristicRead()
?
В моем случае есть 9 характеристик, а 6,7,8 - это характеристика уведомления, во-первых, мы должны уведомить все эти (6,7,8) характеристики.
после этого я получаю значение 1 в 6, 7, 8 характеристиках. И тогда я записал текстовое значение в признаке 1, снова признак 6, уведомил и получил значение 3. После этого я записал значение Unicode в признаке 3, затем мы получили 2 в форме уведомления в признаке 6. В этом сценарии все значения приходит в форме уведомления. Как я могу справиться за ту же процедуру для 4,5 в onCharacteristicChanged()
?
Я также делюсь приведенным ниже кодом метода 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());
}
}