If the code inside your while loops runs more than a few seconds, then your app becomes unresponsive and will crash
Remove the while loops, use a global variable to store your latest command and use a Clock and its Timer event to send the latest comnand via Bluetooth
Taifun