How to tell a battery's ideal charging voltage with a circuit?
The problem with your scheme is that the battery isn't directly connected to the charging socket. There will be a charging controller in between:







simulate this circuit &ndash; Schematic created using CircuitLab

So from the charging port it is not possible to determine what voltage is needed to charge the device.

Only if there is communication between the power adapter and the Charging controller can your scheme work. Qualcomm Quickcharge is an example of that. If the charge controller does not support such a protocol then what you propose cannot be done.

Think about it: USB ports that are designed for 5 V can usually handle 6 V - 6.5 V at most, applying 24 V to such a port will for sure damage it severely.