I am writing an application in C#, to connect to a Bluetooth breakout chip.
From my understanding, Windows assign a COM port to the device; so I can connect to it. Although, I do not understand how to retrieve the name of the device associated to a specific port.
At the moment I retrieve all the COM, and then since I know which port is working for the BT device, I open the connection manually with a button.
The objective here is to turn on the device, and if the device is paired; it will connect automatically.
Unless I got it wrong; when you pair a Bluetooth device, it get assigned to a COM port; so it should be possible to retrieve the device name/BT name, interrogating the COM port? In this way I can scan all the ports; check which port is assigned to that device name and open automatically the connection.
Is this correct or there is a different way to achieve what I want to achieve?
So far, I found no quick way to do so, nor an API from Windows or .NET that allow you to do such an easy thing, like get info about a COM port. Either nobobody works with Bluetooth on serial, or it must be so horrible that not even Microsoft wanted to put efforts in allowing this info to be obtained.
Beside the very convoluted ways to get the info, raging from registry inquire to introspection on different levels; I found this add on called 32feet for .NET; which allow to actually do what I was trying to achieve.
The code is pretty simple; and the object created has the info about the friendly name used in the Bluetooth peripherals screen, and is able to return also the port that is bound to that device, once it connects.
Quite a surprise that I have to use an external solution, while I was expecting that it would be exposed somehow through the standard API in Windows. Although it works, problem solved!
IF I find a different way to do the same, and that does not require more overhead than what you really need, I will post it here.
Related
I am working on using a Raspberry Pi in an embedded project that will utilize wifi to communicate with external devices. The device should be able to act either as a standalone wifi hotspot that devices can connect to or in the case of the presence of an existing wifi network it should connect to that network so that the user does not have to give up his internet connection in order to connect to the device. I plan on making the device start up in hotspot mode, the user can then use the web interface to enter the details of a network that he wants the device to connect to, whenever the specified parameters fails to establish a connection then it defaults back to hotspot mode.
Now the technical stuff I am struggling with is that I want to implement the control software in C# running with Mono on Arch Linux on the Rapsberry Pi. I am struggling the find the Apis or libraries needed to manage the Linux wifi connection. On Windows it seems as if managedwifi.codeplex.com can be used but it does not seem to be compatible with Linux.
My last resort would obviously be to execute shell commands and then parse their outputs, but considering how crude and possibly unreliable that would be this is obviously my last resort.
Any ideas regarding what I should do?
PS. Another thing I might consider before using shell scripts, if it makes a difference is to use Raspbian or some other distro instead.
Actually calling shell commands from a managed code is not a very bad idea. They are reliable, very well tested and mostly lightweight and sometimes just a wrapper around kernel or other modules function. This is also seems to be the same method Node.js modules use when they want to access something lowlevel or related to networking. For example see this source code: node-wireless/node_modules/wireless/index.js
If you don't like it this way there is always "Interop". The same way that you can DllImport() libraries in Windows, you can do in Linux. See here: http://www.mono-project.com/Interop_with_Native_Libraries
IMHO the second solution doesn't worth the effort. Calling shell commands is elegant and neat enough.
my pi is starting into wlan0 as AP with hostapd, when a AP also a lighttpd is starting and give a web interface do change the settings. the web interface verify and write the input in a sqlite3. a second script is doing the canges according to my changes. (like add, edit or remove wifis in wpa_supplicant, reset wlan0 to be a part of an existing wlan like set to dhcp, tell wpa_supplicants...)
except the lighttpd and sqlite3 all components are already on the Raspery. you dont need any mono or c-libraries
for writing the scripts i use python but also perl is working (even php for the frontend)
I have used the LPT port before with a VB6 library called inpout32.dll. I'm much more familiar with that but it is not used in modern computers, such as laptops.
I need to control a device with on / off capabilities. This would have been very easy with LPT, but I need to work with USB and C# in this case. There are some USB2LPT cables out there on the market, but I read that they can't be programmed the same way.
Has anyone used those cables before? If yes, how can I work with them. Otherwise what are my alternatives?
Having read up on this question regarding working with USB devices, I got to wondering what method is being used by the operating system to differentiate between types of USB device?
For example, I can plug in a USB mouse and the computer "just knows" it's a mouse. If I plug in my 1GB USB drive it "just knows" it's 1GB's worth of removable storage.
What sort of code is being used by the device to 'announce' what type it is? Or is the operating system polling the device and getting some standardised response?
How would I program a device to identify itself as, say, a keyboard?
In order to build a device that works over USB, you will almost certainly have to read the USB specification available from http://www.usb.org/developers/docs/
In regards to your specific question, there are GET methods associated with various standard USB variables. These are stored in the firmware of the device and provided when requested. Eg, GET_DESCRIPTOR, GET_STATUS etc are request types that can be sent to the device.
USB has its own set of protocols that would be quite a task to implement in their entirety, so most device manufacturers buy a USB controller chip from some third party.
If you're just interested in hacking something together, then I think the specification will be enough to get you sending a few packets.
The only other option I can think of may not be suitable for your project; wire-tap a real keyboard, let the keyboard to all the complicated address resolution and device identification etc, then just have your device push data onto the wires as and when.
This is something I have managed before when I build a keyboard black-box for password sniffing at high-school, the black-box stored keypresses then regurgitated them onto the wires as if it were a keyboard. Once it had stored a few passwords, and I retrieved it, I had to plug it in attached to the keyboard, then unplug the keyboard before setting it to output the keypresses (with a handy gedit window open :P) otherwise the keyboard would interrupt the transmission with its own data.
The type (or rather, the device class) is determined by reading the device's class from the device's device and interface descriptors. You may want to read this segment about USB device descriptors from USB in a Nutshell for information. (I also recommend reading the whole thing, if you're interested)
In Device Manager open the properties of a device and select the Details tab. The various values give the information (or rather is generated from the information from the device and its driver).
I'm interested in creating a wireless network of laptops using Bluetooth developed in C#.
I want to get the list of Bluetooth activated devices in range ( preferable computers, not dongles, headsets or phones). I don't think I need to use a Bluetooth adapter specific stack coz I have seen Windows searching for Bluetooth devices in range. How can I run such a search in my C# app and list the computers in range?
Please help me to do this, also let me know if my effort is wrong. Thank you.
There is a nice C# Bluetooth library available in 32feet.NET.
If you have no need for any of the additional features it offers and you wish to do the P/Invoke on your own, documentation can be found here; specifically the BluetoothFindFirstDevice and BluetoothFindNextDevice for device discovery.
Additionally, you can use windows sockets to do device/service discovery. Information on that can be found here.
If you wish to specifically filter out types of Bluetooth devices, you will want to look at the ulClassOfDevice field in the BluetoothDeviceInfo.
I've a application that must work after another application. This second application has a bug that causes COM ports not to be closed in specific circumstances.
I would like to close all COM ports programmatically in my application to ensure, that there will be no bugs about close ports reported. Is it possible if I do not own objects that opened COM ports?
I need a solution on .NET Framework, C# .
There is no good, easy way:
A good way is to fix the other application; or if that's impossible, to write a filter device driver (similar to the parport driver but for serial ports instead of parallel ports) which would sit on top of the serial port hardware driver and which would expose more than one connection point (one used by the other application, which passes through to the underlying real driver, and another 'back door' used by your own application ... the real driver would only see one client, i.e. the filter driver wich sits on top of it)
An easy way is to nuke (forcibly terminate) the other process.
I came across this recently and have a little bit to add... Most COM port drivers "unlock" the port when the device is enabled and disabled in the device manager. This means that the (C#) way to accomplish this task is described in the solution:
Win32 API function to programmatically enable/disable device
The information that you need to know to use that solution for COM ports is:
the GUID for COM ports: {4d36e978-e325-11ce-bfc1-08002be10318} (CLSID_Ports)
the "instance path" of the port you wish to reset
Since you say that you want to reset all the ports, you would want to modify the library in that example to loop over all the ports by changing:
// Find the index of our instance. i.e. the touchpad mouse - I have 3 mice attached...
int index = GetIndexOfInstance(diSetHandle, diData, instanceId);
// Disable...
EnableDevice(diSetHandle, diData[index], enable);
to something like this:
for (int index = 0; index < diData.Length; index++)
{
EnableDevice(diSetHandle, diData[index], enable);
}