Get name of bluetooth device on the Devices and Printers window - c#

I know there are questions out there referring to gathering the friendly name of a device on the Device Manager but I cannot do this as the device is simply referred to as "Stardard Serial over Bluetooth link (COM)" and I have many virtual ports with the same reference. I want the name of the device as shown on the Devices and Printers window on:
I'm doing this in C# and currently just getting a list of available COM ports on the system and selecting the one I know from memory.

I managed to get it to work using 32Feet.Net.
You can search for a device by doing
BluetoothClient client = new BluetoothClient();
devices = client.DiscoverDevicesInRange();
foreach (BluetoothDeviceInfo d in devices)
{
items.Add(d.DeviceName);
}
This will give a list of the friendly names you see on the Devices and Printers window rather than "Standard serial over Bluetooth Link".
If you want the COM port like me or any other piece of information then you can simply do a WMI query such as
System.Management.ManagementObjectSearcher Searcher = new System.Management.ManagementObjectSearcher("Select * from WIN32_SerialPort");
foreach (System.Management.ManagementObject Port in Searcher.Get())
{
//your comparison or code here
}

I managed to get the bluetooth name, address and the COM port number without using the 32feet.net library by fiddling with the registry key.
Then, you could connect the bluetooth device by using the SerialPort class by passing the COM port number.
The pseudo code to obtain the bluetooth information is below:
enumerate all the COM port available in the PNP
obtain the device classGuid
search the bluetooth address from the classGuid
when the bluetooth address is known, the bluetooth name can be obtained from - this registry SYSTEM\CurrentControlSet\Services\BTHPORT\Parameters\Devices
I have posted my code in the link below:
https://stackoverflow.com/a/36298213/2297825

I'm using my custom code using 32feet.Net Library which helps me to get the Device Friendly Name also COM port information attached with that device in C# Console Application.
I'm using below code to detect Topaz-Signature Device, and its friendly Name is "T-S460-BT2". You can replace this
string FriendlyDeviceName = "T-S460-BT2";
in the code with your device name you want to search.
using InTheHand.Net;
using InTheHand.Net.Bluetooth;
using InTheHand.Net.Bluetooth.Widcomm;
using InTheHand.Net.Sockets;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Text;
using System.Text.RegularExpressions;
namespace SearchDevice
{
class Program
{
static void Main(string[] args)
{
string FriendlyDeviceName = "T-S460-BT2";
if (BluetoothRadio.IsSupported)
{
BluetoothClient client = new BluetoothClient();
BluetoothDeviceInfo[] devices;
devices = client.DiscoverDevicesInRange();
foreach (BluetoothDeviceInfo d in devices)
{
if (Regex.IsMatch(d.DeviceName, FriendlyDeviceName, RegexOptions.IgnoreCase))
{
try
{
string query = string.Format("SELECT Name, DeviceID, PNPDeviceID from WIN32_SerialPort");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
ManagementObjectCollection osDetailsCollection = searcher.Get();
foreach (ManagementObject mo in osDetailsCollection)
{
string PNPDeviceID = (string)mo.GetPropertyValue("PNPDeviceID");
if (PNPDeviceID != null && Regex.IsMatch(PNPDeviceID, d.DeviceAddress + "", RegexOptions.IgnoreCase))
{
Console.WriteLine("{0}", ((string)mo.GetPropertyValue("DeviceId")).Replace("COM", ""));
}
}
}
catch (Exception exx)
{
}
}
}
}
else
{
Console.WriteLine("Not Supported");
}
Console.ReadLine();
}
}
}

Related

Get all connected device information

I want to get all information of connected devices as the below picture using C# (device name, location, status is required).
https://i.stack.imgur.com/8cFXs.png
Please help me with this issue.
Retrieve using WMI.
You need to install System.Management from nuget.
Computer System Hardware Classes
Here is an example of getting keyboard information.
It is unclear how Location is obtained, but it is probably determined from the DeviceID string.
using System;
using System.Management;
using System.Runtime.Versioning;
internal class Program
{
[SupportedOSPlatform("windows5.0")]
private static void Main()
{
var scope = new ManagementScope("\\\\.\\ROOT\\cimv2");
var query = new ObjectQuery("SELECT * FROM Win32_Keyboard");
using (var sercher = new ManagementObjectSearcher(scope, query))
using (var queryCollection = sercher.Get())
{
foreach (var o in queryCollection)
{
Console.WriteLine(o["Description"].ToString());
Console.WriteLine(o["Status"].ToString());
Console.WriteLine(o["DeviceID"].ToString());
o.Dispose();
}
}
}
}

How to open RJ11 cash drawer in c#

I have a cash drawer (Model = PSHOP CD4141) and POS PC (Posbank Apexa G). I can open cash drawer via XPrinter Q900 after or before printing receipt (there is an option in printer settings). But i need to open drawer without printer. There are RJ11 ports on the POS PC. I tried this code:
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.PointOfService;
namespace POS
{
public class CashDrawerClass
{
CashDrawer myCashDrawer;
PosExplorer explorer;
public CashDrawerClass()
{
explorer = new PosExplorer(this);
DeviceInfo ObjDevicesInfo = explorer.GetDevice("CashDrawer");
myCashDrawer = explorer.CreateInstance(ObjDevicesInfo);
}
public void OpenCashDrawer()
{
myCashDrawer.Open();
myCashDrawer.Claim(1000);
myCashDrawer.DeviceEnabled = true;
myCashDrawer.OpenDrawer();
myCashDrawer.DeviceEnabled = false;
myCashDrawer.Release();
myCashDrawer.Close();
}
}
}
But it doesn't work. Can anyone suggest how can i open cash drawer without printer?
I found the answer. As #kunif said, Apexa G comminucates with cash drawer on COM5 port. All i need simple Write() command. Following code works:
string port = "COM5";
using (SerialPort serialPort = new SerialPort(port))
{
serialPort.Close();
serialPort.Open();
serialPort.Write("\x001B#\x001Bp\0.}");
serialPort.Close();
}

How can i scan and list all connected devices to my network wireless, i'm getting exception?

My pc is connected to the router of the network i want to scan but the not wireless the pc is connected with a cable to the router.
But my android device is connected to the network wireless.
So in logic in this case the results in the list should be my pc and my android device.
This is what i'm using now managed wifi api:
managed wifi api
This is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NativeWifi;
namespace ScanWifi
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
WlanClient client = new WlanClient();
try
{
foreach (WlanClient.WlanInterface wlanIface in client.Interfaces)
{
Wlan.WlanBssEntry[] wlanBssEntries = wlanIface.GetNetworkBssList();
foreach (Wlan.WlanBssEntry network in wlanBssEntries)
{
int rss = network.rssi;
byte[] macAddr = network.dot11Bssid;
string tMac = "";
for (int i = 0; i < macAddr.Length; i++)
{
tMac += macAddr[i].ToString("x2").PadLeft(2, '0').ToUpper();
}
listView1.Items.Add("Found network with SSID {0}." + System.Text.ASCIIEncoding.ASCII.GetString(network.dot11Ssid.SSID).ToString());
listView1.Items.Add("Signal: {0}%."+ network.linkQuality);
listView1.Items.Add("BSS Type: {0}."+ network.dot11BssType);
listView1.Items.Add("MAC: {0}.", tMac);
listView1.Items.Add("RSSID:{0}", rss.ToString());
}
Console.ReadLine();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
When running the program i'm exception on WlanApi.cs on the line:
Wlan.ThrowIfError(
Wlan.WlanOpenHandle(Wlan.WLAN_CLIENT_VERSION_XP_SP2, IntPtr.Zero, out negotiatedVersion, out clientHandle));
System.ComponentModel.Win32Exception' occurred in ManagedWifi.dll
The service has not been started
For Windows 10, the service "WLAN AutoConfig" must be started for WlanClient to work. This service should be started automatically on a computer which has a WiFi adapter present. On a computer such as a desktop which does not have a WiFi adapter, the service startup type is probably Manual and not started; you can start it anyway and WlanClient should no longer throw any exceptions, but without a WiFi adapter, it won't see any interfaces, so you won't be able to get a list of networks.
According to the documentation of the [WlanOpenHandle ][1] function, the problem is that the Wireless Zero Configuration (WZC) service is not started on your machine:
WlanOpenHandle will return an error message if the Wireless Zero Configuration (WZC) service has not been started or if the WZC service is not responsive.
However, depending on your platform, it might also might be the case that you are simply passing the wrong parameters to the WlanOpenHandle function. Have you tried passing Wlan.WLAN_CLIENT_VERSION_LONGHORN as the first parameter?

How to get a PCIE device's link speed on Windows 7/8 programmatically

On Windows 8 when I right click on a PCIE device in Device Manager, in the Details tab, under property "PCI current link speed" I can read the PCIe link speed. The same can be done for the PCIe link width.
I'd like to access this information programmatically in a C# application. How do I do that? through WMI?
And will the same work on Windows 7?
Hope this kicks you a little:
using System;
using System.Management;
namespace PCIeSpeedExample
{
class Program
{
static void Main(string[] args)
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\cimv2", "select * from Win32_NetworkAdapter");
foreach (ManagementObject obj in searcher.Get())
{
Console.WriteLine("--------------- Adapter ----------------");
foreach (PropertyData pd in obj.Properties)
{
Console.WriteLine("{0} = {1}", pd.Name, pd.Value);
}
}
Console.Read();
}
}
}

How can I determine the SATA channel for a given disk?

Using DISKPART command line utility, I can get something called a "Location path" which appears to give me what I need, you can view this by using the command detail disk after selecting one of your disks in diskpart.
It appears I can get this information programatically via this class: MSFT_Disk
I am unsure about how to get an instance of this class. I have a few examples of using a ManagementObjectSearcher for WMI classes but that method is not working for me, I am also unsure of MSFT_Disk's availability in Windows 7 as the page mentions that this is for Windows 8.
Does anyone know of a good way to get SATA channel information or the "location path" of a disk?
If you want to not require Windows 8, I believe WMI is the way to go:
using System;
using System.Linq;
using System.Management;
namespace DiskScanPOC
{
class Program
{
static void Main()
{
var managementScope = new ManagementScope();
//get disk drives
var query = new ObjectQuery("select * from Win32_DiskDrive");
var searcher = new ManagementObjectSearcher(managementScope, query);
var oReturnCollection = searcher.Get();
//List all properties available, in case the below isn't what you want.
var colList = oReturnCollection.Cast<ManagementObject>().First();
foreach (var property in colList.Properties)
{
Console.WriteLine("Property: {0} = {1}", property.Name, property.Value);
}
//loop through found drives and write out info
foreach (ManagementObject oReturn in oReturnCollection)
{
Console.WriteLine("Name : " + oReturn["Name"]);
Console.WriteLine("Target Id: " + oReturn["SCSITargetId"]);
Console.WriteLine("Port: " + oReturn["SCSIPort"]);
}
Console.Read();
}
}
}
I didn't crack open my case to verify the SATA port numbers, but the above app looks like it gives reasonable results on my machine with 3 SATA hard drives.
If you want to get the location path, SetupDiGetDeviceRegistryProperty is the function you're looking for. Set the property value to SPDRP_LOCATION_INFORMATION.
I'm assuming you already know how to enumerate devices to get the DeviceInfoSet and DeviceInfoData.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Management;
namespace Hard_Disk_Interface
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnCheck_Click(object sender, EventArgs e)
{
WqlObjectQuery q = new WqlObjectQuery("SELECT * FROM Win32_IDEController");
ManagementObjectSearcher res = new ManagementObjectSearcher(q);
lblHDDChanels.Text = string.Empty;
foreach (ManagementObject o in res.Get())
{
string Caption = o["Caption"].ToString();
lblHDDChanels.Text += Caption + "\n\n";
if (Caption.Contains("Serial"))
{
lblInterface.Text = "S-ATA";
}
}
}
}
}
Note: First Add the reference of System.Management.dll of .net freamwork 4.0

Categories

Resources