Entries:
Comments:
Posts:

Loading User Information from Channel 9

Something went wrong getting user information from Channel 9

Latest Achievement:

Loading User Information from MSDN

Something went wrong getting user information from MSDN

Visual Studio Achievements

Latest Achievement:

Loading Visual Studio Achievements

Something went wrong getting the Visual Studio Achievements

BlueBoss - Bluetooth Proximity Detection

Summary:

Bluetooth is now found in a variety of devices and enable the user to use wireless accessories.  The Bluetooth protocol allows a user to “discover” any device that is in proximity to your Bluetooth radio.  Why not see who is in proximity to you?  Why not have the presence of a device execute programs or alert you?

Andy Konkol – http://copyandwaste.com

 

Hardware:

  • Bluetooth radio (USB dongle)

  • SMA Female Jack

  • SMA male to N-male pigtail

  • 2.4 GHz antenna (with N-female connector)

Software:

Download:

Bluetooth and Hardware:


Bluetooth was designed for devices to communicate wirelessly over short distances.  However, with a very simple hardware modification you can extend the range of your Bluetooth radio with standard 2.4ghz antennas used in wireless networking (802.11 a/b/g).

Modifying Bluetooth dongles to accept external antennas is documented all over the Internet.  In principle it is very easy: find the antenna lead and solder on a connector/antenna.  I purchased a very cheap Bluetooth USB dongle on eBay and opened the casing.  After finding the antenna trace on the circuit board I soldered on a SMA Female connector to it.  After soldering the antenna jack in place I slipped a 3 inch chunk of heatshrink and heated it to cover the exposed circuit board.  Now I had a Bluetooth radio that accepts external antennas. Adding an antenna simply increases the range of your radio, allowing you to “see” devices from a farther distance.

To connect an external antenna to the dongle I needed to use a connector converter called a pigtail.  I used a SMA male to N-male pigtail.  I connected one end of the pigtail to my dongle and the other to an omni-directional 9dbi panel antenna that had an N-female connector.

IMG_2070[1] IMG_2071[1]

Software:


To take advantage of my newly modified hardware I needed to download the Coding4Fun Toolkit.  Included in the toolkit is an API for Bluetooth devices.  This API allows you to do a wide variety of things with your Bluetooth radio but I focused on two methods from the ServiceAndDeviceDiscovery library: DiscoverAllDevices and DiscoverDeviceByName.

DiscoverAllDevices allows you to “scan” the airwaves on the 2.4 GHz band and report back what devices your radio sees.

DiscoverDeviceByName allows you to scan for a particular device with a specified name and report back if it is present or not.

private bool DevicePresent()
{
    BluetoothDeviceServicesManager workerBTMgr = new BluetoothDeviceServicesManager();
    Device workerDevice = workerBTMgr.DiscoverDeviceByName(_watchItem.DeviceName);

    return (workerDevice != null);
}

public void Run(String Operation)
{
    switch (Operation)
    {
        case "SingleDevice":
            if (DevicePresent())
            {
                _parentForm.Invoke(_parentForm.AddToDeviceSeenList, new object[] { _watchItem });
            }
            else
            {
                _parentForm.Invoke(_parentForm.RemoveFromDeviceSeenList, new object[] { _watchItem });
            }
            break;
        case "AllDevices":
            BluetoothDeviceServicesManager workerBTMgr = new BluetoothDeviceServicesManager();
            List<Device> Devices = workerBTMgr.DiscoverAllDevices();
            _parentForm.Invoke(_parentForm.ThreadUpdateDiscoverBox, new object[] { Devices });
            break;
    }
}

Both of these methods require that the device you are scanning for is in discoverable mode (which most manufacturers enable by default).  Using the two methods described above I was able to tell if a device is in proximity. And ultimately enabling me create alerts and execute programs based on what device is present.

To perform device discovery and not have my UI lag I had to create two worker threads.  One worker thread to discover all devices and display it under my devices listbox, and another to discover devices by name specified in the “watchlist.”  I am not an expert at multi-threaded programs but I managed to implement them without any major headaches.

User Interface:

Since the “Add to watchlist” and “Edit” buttons essentially do the same thing, I decided to overload a windows form.  I also wanted to keep track of the parent form and disable it when the WatchItemForm was shown.

The second overload allows me to fill in the form control's text based on the data that is already set for the WatchItem that has been selected (the Edit button). 

public WatchItemForm(Form1 f, String DeviceName)
{
    InitializeComponent();
    this._parentForm = f;
    lblDeviceName.Text = DeviceName;
}

public WatchItemForm(Form1 f, WatchItem item)
{
    InitializeComponent();
    this._parentForm = f;

    lblDeviceName.Text = item.DeviceName;
    tbxPicturePath.Text = item.ImagePath;
    tbxProgramPath.Text = item.ProgramPath;
    this._parentForm.Enabled = true;
}

I wanted to have a pop up notify window similar to outlook and was able to find Robert Misiak's NotifyWindow. This is a very simple library which allows you to create pop up notify windows very easily. I edited NotifyWindow to include a “picturepath” variable as well as picturebox on the form. As you can see creating a NotifyWindow is quite easy:

public void NotifyDeviceWindow(WatchItem x)
{
    NotifyWindow nw;
    nw = new NotifyWindow();
    //validate Alert Message
    if (x.AlertMessage == null)
    {
        nw.Text = x.DeviceName;
    }
    else
    {
        nw.Text = x.AlertMessage;
    }
    //validate picture
    if (x.ImagePath != null)
    {
        FileInfo imgfile = new FileInfo(x.ImagePath);
        if (imgfile.Exists)
        {
            nw.PicturePath = x.ImagePath;
        }
        else
        {
            MessageBox.Show("Image does not exist");
        }
    }
    nw.Notify();

    if (x.ProgramPath != null)
    {
        RunProcess(x.ProgramPath);
    }
}

Process Flow/Software Operation:

  1. User clicks the “Discover” button, a thread is spawned which enumerates all Bluetooth devices in proximity
  2. Thread finishes and updates the Devices listbox
  3. User selects a discovered device and clicks “Add to watchlist” A watch item form is spawned and asks you for an alert message, a picture path, and an executable path
  4. User clicks save , a WatchItem object is created and added to the watchList object
  5. A timer starts and every 10 seconds a thread is spawned to discover that device by name
  6. If the device is discovered, it is added to the “deviceSeenList” and a notify alert is sent and the executable is executed.
  7. If that device is still present after the next timer click, no notification is sent, If that device is not present it is removed from the “deviceSeenList”

Screenshots:

bb-main[1]  
 bb-watchitem[1] bb-notify_1[1]

Tags:

Follow the Discussion

  • BenBen

    Hi Andy, great article. A couple questions:

    - Can it handle detection of multiple blue-tooth devices at a time?

    - Does the 2.4GHz antenna screw with your existing 802.11 a/b/g WiFi network?

  • Shan CanShan Can

    Hi Andy,

    Great work!

    In Chicago Marathon, last Sunday, contestants wore a chip on the shoe lace for race information transmittal. Were they using Bluetooth.

    Thanks for info.

    Regards.

  • abcdabcd

    can anyone send me code for bluetooth data acquisition btw two pcs

  • JohnJohn

    The device discovery of C4F devkit bluetooth  has a virtual memory (VM) leak. Just watch what happens to the VM each time you search for devices.

  • RobRob

    Nice Work.

    If there's one thing I'd suggest it would be to add a way to execute commands when the watched device disappears

  • Clint RutkasClint I'm a "developer"

    @John, did you do a small scale test of this, is it just BlueBoss or the actual kit?

  • Clint RutkasClint I'm a "developer"

    @abcd source code is at the top, http://www.codeplex.com/blueboss/Release/ProjectReleases.aspx?ReleaseId=14662

  • JeremyJeremy

    Is there a method to DiscoverDevice(), not Devices() at a very quick rate.  For instance, I would like to return simply the address as soon as it appears then call the method again.  I would like to scan the cars as they drive by my house.  

  • Clint RutkasClint I'm a "developer"

    check out the http://www.codeplex.com/C4FDevKit which Andy used to do this.  An API in there may do what you want.  I don't know if bluetooth itself will do what you're asking however.

  • SebastianSebastian

    Hello, I'm working on a project that requires me to extend the range of an USB dongle as much as possible. Have you any data on how much does a panel antenna like yours extend the signal?

    Thanks!

    Seb

  • Clint RutkasClint I'm a "developer"

    @Jirshiri, the code is in c#.  We don't have a VB.net port but what you can do is use Telerik code converter to switch the code over.  http://converter.telerik.com/

  • JishiriJishiri

    nice... but do you have the source code in VB ?

    because i would like to do it also someting similar...

    and no i dont know nothing about c++ Big Smile

Remove this comment

Remove this thread

close

Comments Closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.