This project has moved and is read-only. For the latest updates, please go here.

Find the bluetooth COMM port automatically

Jan 2, 2014 at 6:11 PM
We have to provide the COMM port of the Bluetooth connection to the application to connect to the EV3 brick. However, mindstorm EV3 software has a search function which will find if a brick is connected via Bluetooth. So there must be a way to scan the ports and figure out what port EV3 is using. I figured this out for the Wi-Fi connection since EV3 broadcast a message and that can be used to find the ip address. Does anyone know how this could be done so the users do not have to figure out the COMM port and enter into the application ?

Thanks
Jan 2, 2014 at 8:53 PM
The LEGO MINDSTORMS software uses the 32feet.NET library for Bluetooth communication which, in turn, talks to Bluetooth devices at a lower level than we do. Therefore, it can query the Bluetooth stack and find the appropriate LEGO device and open a channel to it. We talk at a higher layer using the generic SerialPort class which, unfortunately, requires us to know the port in advance. We could try to open each serial port found, send a LEGO command to it, and wait for the proper response to see if it's a LEGO device, but that kind of port scan usually takes too much time to execute.

So, overall, this probably isn't a feature we're likely to add anytime soon to this version of the library, but we realize it's a gap... Sorry!
Jan 2, 2014 at 9:46 PM
I will look into 32feet.NET library. May be we can use that library to discover the port and then use this library to make the connection.
Jan 2, 2014 at 11:09 PM
I have not tried anything I am about to suggest myself. They are theoretical ideas that come to mind.
  1. Just trying to connect one COM Port at a time (a for loop from 1 to x (e.g. 10)) blindly. Then checking if the Firmware method returns something.
    (Of course you would have to account for timeouts and connection exceptions)
  2. One might use WMI to enumerate all available COM Ports and possibly associated properties. This might help in identifiying the correct COM Port that is used for the Bluetooth communication.
Again, I have not tried them but I think they might be viable solutions although they might not be the best.
Jan 3, 2014 at 12:02 AM
hummigbird1 wrote:
I have not tried anything I am about to suggest myself. They are theoretical ideas that come to mind.
  1. Just trying to connect one COM Port at a time (a for loop from 1 to x (e.g. 10)) blindly. Then checking if the Firmware method returns something.
    (Of course you would have to account for timeouts and connection exceptions)
This is what I was mentioning above. You can get a list of available ports, but you do have to open/query/close/timeout each one, which you probably wouldn't want to do every time you run your application as it'd take too much time.
  1. One might use WMI to enumerate all available COM Ports and possibly associated properties. This might help in identifiying the correct COM Port that is used for the Bluetooth communication.
The WMI data on COM ports will just list which ones are available, but it won't have any additional information on what is connected on each port. I looked into using WMI to query the bluetooth connections and that appeared to be a dead end as well. Without going to the Bluetooth layer itself, I'm not sure how you can efficiently get what's required here. That's what the 32feet.NET library does.
Again, I have not tried them but I think they might be viable solutions although they might not be the best.
Both are viable, but not something you'd want to run on each application execution, but something you'd run once and then save off the data for future usages.
Jan 3, 2014 at 12:04 AM
Oh, and the 32feet.NET library is located here for those that want to give it a try: https://32feet.codeplex.com/
Jan 3, 2014 at 8:51 AM
Edited Jan 3, 2014 at 8:53 AM
Here is an article that describes in detail how to find the correct bt serial ports using WMI (with pros and cons as usual)

http://botbench.com/blog/2011/09/23/finding-bluetooth-paired-nxts-with-wmi/


And here is also an approach using the Windows registry (FYI)
http://stackoverflow.com/questions/5988469/how-to-identify-which-com-ports-are-connected-to-bluetooth-from-a-c-sharp-progra