I want to get the device name of the Bluetooth-connected serial port.

Asked 2 years ago, Updated 2 years ago, 82 views

When you ask users to select devices connected by COM port, it is not written as COM15 but
I want to see the device name. WMI can obtain the name of the device connected to the serial port using the code below. For Bluetooth-connected devices,
Standard serial (COMxx) via Bluetooth link and device name cannot be obtained.
Please let me know if there is any way to get the name of the Bluetooth-connected device.

//using System.Management;
using(var device=new ManagementClass("Win32_SerialPort")){
    foreach(ManagementObject port in device.GetInstances(){
        var name = port.GetPropertyValue("Name") as string;
        Console.WriteLine(name);
    }
}

Development Environment: Windows 7, C#6.0.NET 4.5.1

windows c# .net

2022-09-30 20:29

3 Answers

32feet.NET seems easy to retrieve.

Search nuget by 32feet.NET and install 32feet.NET Personal Area Networking for .NET.

The Windows Form application with only buttons and list boxes seems to be able to retrieve it with the following code... but now that I mention it, my machine was bluetooth off, so I couldn't get anything (no error).
(So I didn't know how to link it to the port.I think I can get DeviceAddress.)

private void button1_Click(object sender, EventArgse){

    BluetoothClient=new BluetoothClient();
    var devices=client.DiscoverDevicesInRange();
    foreach (BluetoothDeviceInford in devices) {
        listBox1.Items.Add(d.DeviceName);
    }
}

The license is written on GitHub, but it is MIT License.

Home This question (self-answer)was also introduced in as well as in Microsoft blog in Israel

.


2022-09-30 20:29

DiscoverDevicesInRange() is a discoverable device,
DiscoverDevices() seems to retrieve information about registered devices.

If only the name of the registered device is the same code as the previous answer, but
I was able to retrieve it below.

foreach (BluetoothDeviceInford in new BluetoothClient().DiscoverDevices())
        {
            listBox1.Items.Add(d.DeviceName);
        }


2022-09-30 20:29

You can link it to the COM port by doing the following:
The results are saved in the global variable ArrayList bluetoothPrinterList

However, there are precautions.
Star Precision Thermal Printer (mC-Print 3) with Bluetooth Connection Example

public void SearchPrinter(object theObj)
        {
            BluetoothDeviceInfo[] devices;
            using(BluetoothClient sdp=new BluetoothClient())
                devices=sdp.DiscoverDevices();

            bluetoothPrinterList.Clear();
            foreach (BluetoothDeviceInfo deviceInfo devices)
            {
//                Console.WriteLine(string.Format("{0}({1}), deviceInfo.DeviceName, deviceInfo.DeviceAddress));

                if(deviceInfo.DeviceName.Contains("mC-Print3"))
                {
                    bluetoothPrinterList.Add(deviceInfo.DeviceName+"|"+deviceInfo.DeviceAddress.ToString());
                }
            }

            ManagementObjectSearcher serialSearcher=
                new ManagementObjectSearcher("root\\CIMV2",
                "SELECT* FROM Win32_SerialPort";
            varquery=from ManagementObjects in serialSearcher.Get()
                        select new {Name=s["Name"], DeviceID=s["DeviceID", PNPDeviceID=s["PNPDeviceID"]}; // DeviceID --> PNPDeviceID

            foreach (var port in query)
            {
                Console.WriteLine("{0}-{1}", port.DeviceID, port.Name);
                varpnpDeviceId=port.PNPDeviceID.ToString();

                if(pnpDeviceId.Contains("BTHENUM"))
                {
                    var bluetoothDeviceAddress=pnpDeviceId.Split('&')[4].Split('_')[0];
                    if (bluetoothDeviceAddress.Length==12&bluetoothDeviceAddress!="000000000000")
                    {
                        Console.WriteLine("-Address: {0}", bluetoothDeviceAddress);
                    } else
                    {
// precautions
// The Star Precision DeviceID had a Bluetooth device address in the fifth &.
// The way DeviceIDs are created seems to vary from vendor to vendor.
                        try
                        {
                            bluetoothDeviceAddress=pnpDeviceId.Split('&')[5].Split('_')[0];
                            if (bluetoothDeviceAddress.Length==12&bluetoothDeviceAddress!="000000000000")
                            {
                                intcc=0;
                                foreach (string the Printer in bluetoothPrinterList)
                                {
                                    string myPrinterAddress=thePrinter.Split('|')[1];
                                    if(myPrinterAddress==bluetoothDeviceAddress)
                                        break;

                                    cc++;
                                }

                                if (cc<bluetoothPrinterList.Count)
                                {
                                    bluetoothPrinterList[cc]=bluetoothPrinterList[cc]+"|"+port.Name;
                                    Console.WriteLine("-myPrinter Info:{0}", bluetoothPrinterList[cc]);
                                }

                            }
                        }
                        catch
                        {
                            continue;
                        }
                    }
                }
            }
        }


2022-09-30 20:29

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.