Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I tell if a computer is Bluetooth-enabled?

-- RR

SpacerHey, Scripting Guy! AnswerScript Center

Hey, RR. You know, any time the Scripting Guys get asked something like this, one question immediately pops into our minds: “So where did the name Bluetooth come from anyway?”

As it turns out, the Bluetooth “personal area networking” protocol was named in honor of Harald Bluetooth Gormson, son of King Gorm the Old, the one time King of Jutland. (We’re not sure if Gorm the Old is the name that the King was given at birth; that would be pretty cool, though, wouldn’t it?) We found it interesting that Harald’s middle name was Bluetooth. After all, Scripting Guy Jean Bluetooth Ross has spent her entire life (79 years and counting) thinking she was the only person to have that middle name. Good news, Jean: you’re not the only Bluetooth after all.

At any rate, Harald became King sometime around 958, upon the death of his father. Over time, Harald conquered all of modern-day Denmark and Norway, and, along the way, kind of, sort of converted to Christianity. He had a son named Sweyn Forkbeard who, no doubt in retaliation for being given the name Sweyn Forkbeard, eventually led a revolt that cost his father both his throne and his life.

But Harald got the last laugh; after all, how many Forkbeard-enabled devices do you see these days?

Note. There seems to be some disagreement as to where the name Bluetooth came from. Some scholars insist it comes from the old Norse words for “dark-skinned” and “great man,” meaning that Harald had dark skin and, well, was a great man. Others say it’s because Harald actually had a blue tooth. Fortunately, Peter Costantini, the oldest living Scripting Guy, went to junior high school with Harald, so we’ll ask Peter about that.

Just as soon as he wakes up from his nap.

After we disposed of the question “Where did the name Bluetooth come from?” a second question popped into our heads: how can you tell if a computer is Bluetooth-enabled? That turned out to be a much tougher question to answer; in fact, we never did find anything that could tell us definitively whether or not a computer was Bluetooth-enabled. (Or at least not a definitive way to do this programmatically; it’s easy enough to verify that a computer is Bluetooth-enabled by using the Windows Control Panel.) However, based on the fact that Bluetooth is a network protocol, we managed to come up with the following a script, a script that correctly determined whether a handful of test computers were Bluetooth-enabled:

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery _
    ("Select * From Win32_NetworkProtocol")

For Each objItem in colItems
    If InStr(objItem.Name, "Bluetooth") Then
        Wscript.Echo "Description: " & objItem.Description
        Wscript.Echo "Name: " & objItem.Name
    End If
Next

So what are we doing here? Well, to begin with, we’re connecting to the WMI service on the local computer. And yes, you can just as easily run this script against a remote computer; all you need to do is assign the name of that remote machine to the variable strComputer:

strComputer = "atl-ws-01"

After making the connection to the WMI service, we then use the following query to return a collection of all the network protocols installed on the computer:

Set colItems = objWMIService.ExecQuery _
    ("Select * From Win32_NetworkProtocol")

Is that the answer to our question? No, but we are getting closer.

To actually find the answer, we next set up a For Each loop to loop through the collection of network adapters. What do we do inside that loop? Well, for starters, we execute this line of code:

If InStr(objItem.Name, "Bluetooth") Then

Here we’re using VBScript’s InStr function to search the value of the protocol Name property. And what are we searching for? Why, the string value Bluetooth, of course. If InStr returns a 0, that means it failed to find the word Bluetooth anywhere in the protocol Name; in that case, we zip back to the top of the loop and repeat the process with the next protocol in the collection. If InStr returns anything other than 0, that means that the target word was found in the value of the Name property. In that case, we then echo back the values of the protocol Description and Name, like so:

Description: Bluetooth Device (RFCOMM Protocol TDI)
Name: MSAFD RfComm [Bluetooth]

As best we can tell, the presence of this protocol means that the computer is Bluetooth-enabled; this protocol appears to be installed only on computers that are capable of running Bluetooth. We can’t guarantee that this is the case, but, like we said, it worked on our admittedly-limited number of test machines.

OK. Next question: is there any way to tell if a particular Bluetooth device is installed on a computer? That’s another tough one; unfortunately there’s no such thing as a Win32_Bluetooth class that collects information about Bluetooth devices. That’s definitely a shame; that would be a useful class. However, one thing you can do is use WMI classes that do exist to look for the presence of a Bluetooth device. For example, this script uses the Win32_NetworkAdapter class to see if the computer has any Bluetooth adapters installed:

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkAdapter")

For Each objItem in colItems
    If InStr(objItem.Caption, "Bluetooth") Then
        Wscript.Echo "Caption: " & objItem.Caption
        Wscript.Echo "Device ID: " & objItem.DeviceID
        Wscript.Echo "Name: " & objItem.Name
    End If
Next

How does this script work? Pretty much the same way our first script did; the only difference is that this one uses InStr to see if the word Bluetooth appears anywhere in the Caption property for each network adapter installed on the computer. On our test machine, this script returns the following:

Caption: [00000016] Bluetooth Device (Personal Area Network)
Device ID: 16
Name: Bluetooth Device (Personal Area Network)

You could do something similar for the Win32_POTSModem class to see if the computer has a Bluetooth-enabled modem. And so on.

Note. Although these products are not available yet, at last not in the US, Toshiba apparently makes Bluetooth-enabled microwaves, refrigerators, and washers and dryers. That’s the good news. The bad news? WMI doesn’t have a Win32_WasherAndDryer class, either.

At least not yet. Maybe in Windows Server 2008.

Editor’s Note (for the humor-challenged at Microsoft): No, we’re not really telling anybody there will be a Win32_WasherAndDryer class in Windows Server 2008. We said “maybe.”

We hope that helps, RR. For those of you more interested in Harlald Bluetooth than Bluetooth devices, we should point out that, following his death, Harald was buried in the cathedral at Roskilde. Interestingly enough, Roskilde is also be the home of the Roskilde Festival, one of the two biggest rock music festivals in all of Europe. (Which, now that we think about it, is probably why they named it the Roskilde Festival.) In the year 2007 the Roskilde Festival was marked by the appearance of big-name groups such as the Who, the Beastie Boys, and the Red Hot Chili Peppers, as well as the nearly 4 inches of rain that fell during the festival’s 4 days.

And yes, that would be just another Saturday afternoon for those of us in the Seattle area. Or at least lately it would be.