This project has moved. For the latest updates, please go here.

Waiting between connecting, accessing, and disconnecting?

Mar 26, 2014 at 8:02 PM
In the Known Issues section on the Documentation page, it says:

"An exception may be thrown when connecting/disconnecting rapidly via Bluetooth on Phone and WinRT."

I get an exception when I have my connecting and disconnecting statements near each other, but I have several questions. First, what is the best workaround for this? And second, when I attempt to access the sensors between those statements, I do not get the correct values. Adding an Await System.Threading.Tasks.Task.Delay(5000) fixed the sensor part, but it did not always fix the connection problem. Do I need more delay statements between all the connecting, accessing, and disconnecting statements? I'm not quite sure what the best workaround is, and I don't want to make my code any messier than necessary if there are more efficient ways to do it. Thanks.
Coordinator
May 26, 2014 at 5:22 AM
The underlying issue here is just my crummy code. :)

It's possible to call Disconnect before the Connect code has completed, which causes a race condition and leads to "bad things happening". That said, with the code from your other thread, you're definitely allowing the Connect method to complete, so it should be due to this issue. I'm not sure why you'd be getting incorrect sensor data, though. The brick is polled for data in a loop once the connection is made to the brick, so the data should be valid no matter what as long as you're connected. Again, if you have a sample project or can provide some more details I can try to look further.