Conditions for Setting the DTC
WARNING: This page does not describe the selected car, but rather 8 other vehicles, including the 2012 GMC Yukon XL, 2012 GMC Yukon, 2012 Chevrolet Tahoe, 2012 Chevrolet Suburban, and 2012 Chevrolet Avalanche. However, it is still accessible from the selected car via links, so may be relevant.
DTC B3031 will set any time the TDM enters the learn coded keys state.