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

Download:

Hardware:

  • Bluetooth radio (USB dongle)

  • SMA Female Jack

  • SMA male to N-male pigtail

  • 2.4 GHz antenna (with N-female connector)

Software:

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 IMG_2071

 

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.

   1: private bool DevicePresent()
   2:         {
   3:             BluetoothDeviceServicesManager workerBTMgr = new BluetoothDeviceServicesManager();
   4:             Device workerDevice = workerBTMgr.DiscoverDeviceByName(_watchItem.DeviceName);
   5:  
   6:             if (workerDevice != null)
   7:             {
   8:                 return true;
   9:             }
  10:             else
  11:             {
  12:                 return false;
  13:             }
  14:  
  15:         }
  16:  
  17:         public void Run(String Operation)
  18:         {
  19:             switch (Operation)
  20:             {
  21:                 case "SingleDevice":
  22:                     if (DevicePresent())
  23:                     {
  24:                         _parentForm.Invoke(_parentForm.AddToDeviceSeenList, new object[] {_watchItem});
  25:                     }
  26:                     else
  27:                     {
  28:                         _parentForm.Invoke(_parentForm.RemoveFromDeviceSeenList, new object[] { _watchItem });
  29:                     }
  30:                     break;
  31:                 case "AllDevices":
  32:                     BluetoothDeviceServicesManager workerBTMgr = new BluetoothDeviceServicesManager();
  33:                     List<Device> Devices = workerBTMgr.DiscoverAllDevices();
  34:                     _parentForm.Invoke(_parentForm.ThreadUpdateDiscoverBox, new object[] { Devices });
  35:                     break;
  36:             }
  37:  
  38:         }

 

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). 

   1: public WatchItemForm(Form1 f, String DeviceName)
   2:         {
   3:             InitializeComponent();
   4:             this._parentForm = f;
   5:             lblDeviceName.Text = DeviceName;
   6:  
   7:         }
   8:  
   9:         public WatchItemForm(Form1 f, WatchItem item)
  10:         {
  11:             InitializeComponent();
  12:             this._parentForm = f;
  13:  
  14:             lblDeviceName.Text = item.DeviceName;
  15:             tbxAlertMessage.Text = item.AlertMessage;
  16:             tbxPicturePath.Text = item.ImagePath;
  17:             tbxProgramPath.Text = item.ProgramPath;
  18:             this._parentForm.Enabled = true;
  19:             
  20:         }

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:

   1: public void NotifyDeviceWindow(WatchItem x)
   2: {
   3:     NotifyWindow nw;
   4:     nw = new NotifyWindow();
   5:     //validate Alert Message
   6:     if (x.AlertMessage == null)
   7:     {
   8:         nw.Text = x.DeviceName;
   9:     }
  10:     else
  11:     {
  12:         nw.Text = x.AlertMessage;
  13:     }
  14:     //validate picture
  15:     if (x.ImagePath != null)
  16:     {
  17:         FileInfo imgfile = new FileInfo(x.ImagePath);
  18:         if (imgfile.Exists)
  19:         {
  20:             nw.PicturePath = x.ImagePath;
  21:         }
  22:         else
  23:         {
  24:             MessageBox.Show("Image does not exist");
  25:         }
  26:     }
  27:     nw.Notify();
  28:  
  29:     if (x.ProgramPath != null)
  30:     {
  31:         RunProcess(x.ProgramPath);
  32:     }
  33: }

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 bb-watchitem bb-notify