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

« 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'.

#!/usr/bin/ruby

#file: bt_scan.rb

require 'rexml/document'
include REXML

while true
  filein = File.new('bt_found.xml','r')
  doc = Document.new(filein)
  filein.close

  fileout = File.new('bt_found.xml','w')

  result = `hcitool scan`
  found = result.split(/\n/) # retrieve each found device
  found.delete_at(0)         # remove the item containing 'scanning ...'

  found.each do |b|
    node_found = Element.new('found')
    p = b.split(/\t/) # retrieve device details
    id = p[1]
    name = p[2]
    puts "name #{name} --- id #{id}"
    node_id = Element.new('id')
    node_name = Element.new('name')
    node_date = Element.new('date')

    node_id.text = id
    node_name.text = name
    node_date.text = Time.now

    node_found << node_id
    node_found << node_name
    node_found << node_date
    doc.root << node_found
  end


  fileout.puts doc
  fileout.close
  puts 'sleeping'
  sleep 10 # during this time we can kill the script, 
           # otherwise we risk losing the contents of the xml file.
  puts 'awake'
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.

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



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.

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

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]

#!/usr/bin/ruby
#file: whereib.rb

deviceid = '00:0E:6D:29:38:EB'
devicename = 'Nokia 6600'

count = 0
while count < 1
  if `hcitool name #{deviceid}`.chomp == devicename 
    puts devicename + ' IN RANGE'
    puts Time.now
  else
    puts devicename + ' OUT OF RANGE'
    puts Time.now
  end
  sleep 7
end  
« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS