Unfortunately, when USB is disconnected and the device is powered from battery, there is a small amount of leakage through the battery charging circuit that leaves a residual voltage of 3.7V or so on the VUSB line. This prevents the VUSB input on the processor from being able to determine if the device is plugged in or not.
The most straightforward thing to do here is add a small pulldown resistor to bleed this current down. VUSB needs to be <1.4V for the processor to detect it as disconnected. Some quick experimentation shows that a 4.7k resistor drops the voltage to .37v, which means it creates a load of ~80uA on the battery at all times. Increasing the resistance to 11k raises the voltage to .8v but only drops the load to 72uA, so sticking with something around 4.7k is probably the best compromise between detection capability and battery life.
Unfortunately, when USB is disconnected and the device is powered from battery, there is a small amount of leakage through the battery charging circuit that leaves a residual voltage of 3.7V or so on the VUSB line. This prevents the VUSB input on the processor from being able to determine if the device is plugged in or not.
The most straightforward thing to do here is add a small pulldown resistor to bleed this current down. VUSB needs to be <1.4V for the processor to detect it as disconnected. Some quick experimentation shows that a 4.7k resistor drops the voltage to .37v, which means it creates a load of ~80uA on the battery at all times. Increasing the resistance to 11k raises the voltage to .8v but only drops the load to 72uA, so sticking with something around 4.7k is probably the best compromise between detection capability and battery life.