The Notebook Review forums were hosted by TechTarget, who shut down them down on January 31, 2022. This static read-only archive was pulled by NBR forum users between January 20 and January 31, 2022, in an effort to make sure that the valuable technical information that had been posted on the forums is preserved. For current discussions, many NBR forum users moved over to NotebookTalk.net after the shutdown.
Problems? See this thread at archive.org.

    Batch file for bluetooth

    Discussion in 'Windows OS and Software' started by Pbottie, Aug 15, 2008.

  1. Pbottie

    Pbottie Notebook Enthusiast

    Reputations:
    1
    Messages:
    30
    Likes Received:
    0
    Trophy Points:
    15
    Hi!
    I've been lurking NBR forums for some time and you guys have "silently" helped me with choosing laptop and laptop bag!
    Now I've run into a problem, or a snag really.
    I have a HP dv2700 with Vista Ultimate x64.
    It comes with Wireless and Bluetooth built in.
    It has a switch where I can turn off both WiFi and BT.
    If the switch is on I can use "HP wireless asistant" to turn on/off either WiFi, BT or both.
    Either by using the graphical interface it has OR it's command line:
    Wireless <Device> <on/off>.

    Here comes my snag. I have managed to make TWO separate batch files that turns Bluetooth ON and the other turns it OFF, but what I'd like is ONE batch file that turns it ON if it's OFF and OFF if it's ON.
    I found something called SET and SETX. Which creates variables in windows. But none help me as SET requires the command line to be running or it will delete the variable and SETX requires the command line to be running or it will REVERT the variable to it's "original" value until reboot.

    I've googled and searched countless places on how to perhaps "refresh" the system variables so that setx wouldn't require reboot. I've also looked for some way to keep a command window in the background where I can send setx commands to but to no avail.

    Sorry for the wall of text, but in summation:
    I need one batchfile to turn bluetooth on or off depending on if it's on or off.

    This is what I have tried which kinda worked at some point:
    Code:
    @ECHO OFF
    cd "C:\Program Files (x86)\Hewlett-Packard\HP Wireless Assistant\"
    goto %BT%
    :av
    Wireless Bluetooth on
    setx BT pa
    goto end
    :pa
    Wireless Bluetooth off
    setx BT av
    goto end
    :end
    
    Thanks in advance!
     
  2. flyt

    flyt Notebook Enthusiast

    Reputations:
    4
    Messages:
    30
    Likes Received:
    0
    Trophy Points:
    15
    Tar väl detta på engelska :p

    While i can't really help you with SET and SETX (i have far too little experience with Windows command line), however the simplest way to just save a setting would probably to use a standard text file.

    However, the best way to do what you are trying to do is probably to check for a running process. When Bluetooth is turned on there is probably a background process launched; if you can find the name of this process, you can determine if Bluetooth is on by checking if the process is running.

    Now i have Windows XP, so this might be different on Vista, however, both of these commands are included by default on Windows XP Pro.

    Code:
    tasklist.exe | find "*name-of-bt-process*"
    tasklist.exe prints all running processes, the Pipe ("|") takes the output (multiple lines) from tasklist.exe and sends it as input to find.exe, find.exe will then print any lines matching *name-of-bt-process* to the console.

    So if the code above returns anything like "bluetoothProcess.exe 2660 Console 0 130*060 kB",
    then your Bluetooth is ON, if Bluetooth is OFF you will just get an empty string.

    If you still want to create a file you can do
    Code:
    echo "Hello" > file.txt
    This will create a file named "file.txt" with the text "Hello" inside.

    Code:
    for /f %a in (file.txt) do (echo %a)
    This code will loop through "file.txt" and print every single line.

    (just realized an even simpler way would be to see if a certain file exists, eg. when you turn Bluetooth on, you create a file named "Bt.on", then in the script you check to see if that file exists, if it doesnt, bluetooth is obviously off)
     
  3. swarmer

    swarmer beep beep

    Reputations:
    2,071
    Messages:
    5,234
    Likes Received:
    0
    Trophy Points:
    205
    Here's a simple script that increases a counter, remembering its value between executions:

    Code:
    @echo off
    set /a i = %count% + 1
    setx count %i%
    set count="%i%"
    echo count = %count%
    pause
    
    This works for me whether I'm in a command prompt window, or I just double-click the batch file from Explorer.

    "set" sets it for the current command shell, while "setx" sets it for future shells. (And "set /a" lets you do math.) Notice the different syntax used for set vs. setx.

    Now, if that works for you... I think you can probably adapt this to do what you need. However, I haven't tested whether it'll persist across a reboot... I think it probably will, so you may need another script to reset the variable to the right value at startup.
     
  4. Pbottie

    Pbottie Notebook Enthusiast

    Reputations:
    1
    Messages:
    30
    Likes Received:
    0
    Trophy Points:
    15
    Allright! Thank you for your advice, I'll fiddle with it a bit and get back with my results.
     
  5. Pbottie

    Pbottie Notebook Enthusiast

    Reputations:
    1
    Messages:
    30
    Likes Received:
    0
    Trophy Points:
    15
    Last thing worked like a charm!
    Code here:
    Code:
    @ECHO OFF
    cd "C:\Program Files (x86)\Hewlett-Packard\HP Wireless Assistant"
    IF EXIST bt.txt (
    del bt.txt
    Wireless Bluetooth off
    ) ELSE (
    echo ON > bt.txt
    Wireless Bluetooth on
    )