Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

James Robertson http://www.r0bertson.co.uk

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS 

Detecting bluetooth devices with hcitool scan

A trivial Ruby script to store the bluetooth devices found with the shell command 'hcitool scan'.

   1  
   2  #!/usr/bin/ruby
   3  
   4  #file: bt_scan.rb
   5  
   6  require 'rexml/document'
   7  include REXML
   8  
   9  while true
  10    filein = File.new('bt_found.xml','r')
  11    doc = Document.new(filein)
  12    filein.close
  13  
  14    fileout = File.new('bt_found.xml','w')
  15  
  16    result = `hcitool scan`
  17    found = result.split(/\n/) # retrieve each found device
  18    found.delete_at(0)         # remove the item containing 'scanning ...'
  19  
  20    found.each do |b|
  21      node_found = Element.new('found')
  22      p = b.split(/\t/) # retrieve device details
  23      id = p[1]
  24      name = p[2]
  25      puts "name #{name} --- id #{id}"
  26      node_id = Element.new('id')
  27      node_name = Element.new('name')
  28      node_date = Element.new('date')
  29  
  30      node_id.text = id
  31      node_name.text = name
  32      node_date.text = Time.now
  33  
  34      node_found << node_id
  35      node_found << node_name
  36      node_found << node_date
  37      doc.root << node_found
  38    end
  39  
  40  
  41    fileout.puts doc
  42    fileout.close
  43    puts 'sleeping'
  44    sleep 10 # during this time we can kill the script, 
  45             # otherwise we risk losing the contents of the xml file.
  46    puts 'awake'
  47  end


Note: Shell commands don't really have much to do with Ruby, however if there is no easy to use bluetooth libraries then I will of course use what's available.

Using Ruby to detect Bluetooth proximity

This Ruby script uses the hcitool and rfcomm to detect the proximity of a bluetooth enabled mobile phone. The signal strength in this script will return a value from 0 to 10, 0 is near and 10 is far, otherwise if there is no connection it will keep on trying.

   1  
   2  deviceid = '00:12:D1:E6:57:BE' 
   3  range = ''
   4  count = 0
   5  while count < 1
   6    # assuming the bt device is already connected get the signal strength value.
   7    range = `hcitool rssi #{deviceid}`.chomp
   8    if  range.length > 0
   9      puts range[/\w+$/]
  10    else
  11      puts 'connecting to the bluetooth device ...'
  12      Thread.new{`rfcomm connect 0 #{deviceid}`}
  13    end
  14  	
  15    sleep 7
  16  end
  17  


warning (11-Jul-2008): I've found Thread.new to be causing a problem, it appears not to remove itself from the process queue when it is no longer required.

Simple Bluetooth Proximity

Following on from my earlier investigation into bluetooth proximity, I wanted something which returned a value which could be used to determine distance. The output from hcitool rssi ranges from 0 for close object down to -10 for far away objects.

   1  
   2  rfcomm connect 0 00:12:D1:E6:57:BE

   1  
   2  hcitool rssi 00:12:D1:E6:57:BE


Proximity Results:
-4 .. 0 : Bedroom - nearest to my bed
-4 .. -6 : Hallway
-8 .. -9 : Livingroom - on the computer desk
-9 .. -10 : Livingroom - nearest to the window

Detect the presence of a Bluetooth device

This example shows how to check for the presence of a mobile phone. The code was based on the article 'Implementing Bluetooth Proximity Detection with Asterisk, Part II' http://snipr.com/1vvi5 [nerdvittles.com]

   1  
   2  #!/usr/bin/ruby
   3  #file: whereib.rb
   4  
   5  deviceid = '00:0E:6D:29:38:EB'
   6  devicename = 'Nokia 6600'
   7  
   8  count = 0
   9  while count < 1
  10    if `hcitool name #{deviceid}`.chomp == devicename 
  11      puts devicename + ' IN RANGE'
  12      puts Time.now
  13    else
  14      puts devicename + ' OUT OF RANGE'
  15      puts Time.now
  16    end
  17    sleep 7
  18  end  
« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS