How can I scan a One-Wire (1-Wire) bus for all connected devices and list their IDs
Maxim has an application note titled 1-Wire Search Algorithm:

The strategy is too involved to accurately describe here, but the basic principle is based on:


1-Wire is an open drain bus: multiple devices can pull the bus to GND simultaneously.
0s are encoded as long low pulses, 1s as short low pulses (by actually not pulling the bus low).  If multiple devices write the same bit to the bus, that bit appears on the bus.  If any device writes a 0 to the bus, a 0 appears.
as reply to the 1-Wire Search ROM command, all (selected) devices will, bit by bit, first write their own ID bit \$A_i\$, followed by the inverted ID bit \$\bar{A_i}\$.  Then the master writes a bit \$S_i\$, which will select the devices for the next bits.


Based on these properties, the master can detect whether


no selected devices are present: both \$A_i = 1\$ and \$\bar{A_i} = 1\$.
devices with a 0 as ID bit are present: \$A_i = 0\$
devices with a 1 as ID bit are present: \$\bar{A_i} = 0\$ 


This way you can see which bits are still available in your search space, and you progress down that binary tree.