3
votes

I have a Bluetooth enabled device with user-friendly name "Sensor1". This device uses the SPP profile. In order to ask the device to start data streaming via Bluetooth, I have to write '10111011' on the COM port corresponding to this device as follows:

ser = serial.Serial('COM5') 
ser.write('10111011')     

Problem is that I do not know which one of the COM ports corresponds to "Sensor1". So, I read windows registry to get the device name:

import _winreg as reg
from itertools import count

key = reg.OpenKey(reg.HKEY_LOCAL_MACHINE, 'HARDWARE\\DEVICEMAP\\SERIALCOMM')
for i in count():
    device, port = reg.EnumValue(key, i)[:2]
    print "Device name \"%s\" found at %s" % (device, port)

All I get is:

Device name \Device\Serial0 found at COM3
Device name \Device\BthModem16 found at COM4
Device name \Device\BthModem17 found at COM5

How can I get the device name as in:

service = bluetooth.find_service()
print service["name"]
3

3 Answers

1
votes

I recommend you to find MAC address first, then find COM port by MAC address. But I am not sure if this is the best way. I tested this code in Windows 10 and Python 3.5.

To find MAC address from friendly name, use this function:

import bluetooth
def find_bt_address_by_target_name(name):
    # sometimes bluetooth.discover_devices() failed to find all the devices
    MAX_COUNT = 3
    count = 0
    while True:
        nearby_devices = bluetooth.discover_devices()

        for btaddr in nearby_devices:
            if name == bluetooth.lookup_name( btaddr ):
                return btaddr

        count += 1
        if count > MAX_COUNT:
            return None
        print("Try one more time to find target device..")            

Then find COM port by MAC address. This asssume you already paired with the target device and enabled SPP ports:

import winreg
import serial
import time

class BluetoothSpp:
    key_bthenum = r"SYSTEM\CurrentControlSet\Enum\BTHENUM"
    # IMPORTANT!! 
    # you need to change this by searching the registry
    DEBUG_PORT = 'C00000001'

    def get_spp_com_port(self, bt_mac_addr):
        print(bt_mac_addr)
        bt_mac_addr = bt_mac_addr.replace(':', '').upper()
        for i in self.gen_enum_key('', 'LOCALMFG'):
            print(i)
            for j in self.gen_enum_key(i, bt_mac_addr):
                print(j)
                if self.DEBUG_PORT in j:
                    subkey = self.key_bthenum+'\\'+ i+'\\'+j
                    port = self.get_reg_data(subkey, 'FriendlyName')
                    assert('Standard Serial over Bluetooth link' in port[0])
                    items = port[0].split()
                    port = items[5][1:-1]
                    print(port)
                    return port

    def gen_enum_key(self, subkey, search_str):
        hKey = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, self.key_bthenum + '\\' + subkey)

        try:
            i = 0
            while True:
                output = winreg.EnumKey(hKey, i)
                if search_str in output:
                    yield output
                i += 1

        except:
            pass

        winreg.CloseKey(hKey)

    def get_reg_data(self, subkey, name):
        hKey = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, 
                            subkey)
        output = winreg.QueryValueEx(hKey, name) 
        winreg.CloseKey(hKey)
        return output

if __name__ == '__main__':
    mac_addr = '11:22:33:44:55:66'
    bt_spp = BluetoothSpp()
    com_port = bt_spp.get_spp_com_port(mac_addr)
0
votes
import bluetooth
decices = bluetooth.discover_devices()

Using the following library: https://pypi.python.org/pypi/PyBluez/
Here's some good usage examples: https://people.csail.mit.edu/albert/bluez-intro/c212.html

If you're not interested in using an additional library, you can always try to extract the discover function related to Windows, found here: https://github.com/karulis/pybluez/blob/2a22e61fb21c27b47898c2674662de65162b485f/bluetooth/widcomm.py#L109

0
votes

Another solution that worked for me.

import serial.tools.list_ports

#Find COM port with LookFor name
lookFor = "DG-1"
nb=discover_devices(lookup_names=True)
for addr,name in list(nb):
    if lookFor == name:
        break
    else:
        name = None
        addr = None
if name == lookFor:
    comPorts=list(serial.tools.list_ports.comports())
    addr=addr.translate(None,":")
    for COM,des,hwenu in comPorts :
        if addr in hwenu:
            break
if name!=None:
    print "COM=",COM,"   BTid=",name
else:
    print LookFor," not found."