<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: hcitool code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Tue, 07 Oct 2008 17:06:50 GMT</pubDate>
    <description>DZone Snippets: hcitool code</description>
    <item>
      <title>Detecting bluetooth devices with hcitool scan</title>
      <link>http://snippets.dzone.com/posts/show/5764</link>
      <description>A trivial Ruby script to store the bluetooth devices found with the shell command 'hcitool scan'.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/ruby&lt;br /&gt;&lt;br /&gt;#file: bt_scan.rb&lt;br /&gt;&lt;br /&gt;require 'rexml/document'&lt;br /&gt;include REXML&lt;br /&gt;&lt;br /&gt;while true&lt;br /&gt;  filein = File.new('bt_found.xml','r')&lt;br /&gt;  doc = Document.new(filein)&lt;br /&gt;  filein.close&lt;br /&gt;&lt;br /&gt;  fileout = File.new('bt_found.xml','w')&lt;br /&gt;&lt;br /&gt;  result = `hcitool scan`&lt;br /&gt;  found = result.split(/\n/) # retrieve each found device&lt;br /&gt;  found.delete_at(0)         # remove the item containing 'scanning ...'&lt;br /&gt;&lt;br /&gt;  found.each do |b|&lt;br /&gt;    node_found = Element.new('found')&lt;br /&gt;    p = b.split(/\t/) # retrieve device details&lt;br /&gt;    id = p[1]&lt;br /&gt;    name = p[2]&lt;br /&gt;    puts "name #{name} --- id #{id}"&lt;br /&gt;    node_id = Element.new('id')&lt;br /&gt;    node_name = Element.new('name')&lt;br /&gt;    node_date = Element.new('date')&lt;br /&gt;&lt;br /&gt;    node_id.text = id&lt;br /&gt;    node_name.text = name&lt;br /&gt;    node_date.text = Time.now&lt;br /&gt;&lt;br /&gt;    node_found &lt;&lt; node_id&lt;br /&gt;    node_found &lt;&lt; node_name&lt;br /&gt;    node_found &lt;&lt; node_date&lt;br /&gt;    doc.root &lt;&lt; node_found&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;  fileout.puts doc&lt;br /&gt;  fileout.close&lt;br /&gt;  puts 'sleeping'&lt;br /&gt;  sleep 10 # during this time we can kill the script, &lt;br /&gt;           # otherwise we risk losing the contents of the xml file.&lt;br /&gt;  puts 'awake'&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;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.</description>
      <pubDate>Sun, 13 Jul 2008 22:31:47 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5764</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Using Ruby to detect Bluetooth proximity</title>
      <link>http://snippets.dzone.com/posts/show/5536</link>
      <description>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.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;deviceid = '00:12:D1:E6:57:BE' &lt;br /&gt;range = ''&lt;br /&gt;count = 0&lt;br /&gt;while count &lt; 1&lt;br /&gt;  # assuming the bt device is already connected get the signal strength value.&lt;br /&gt;  range = `hcitool rssi #{deviceid}`.chomp&lt;br /&gt;  if  range.length &gt; 0&lt;br /&gt;    puts range[/\w+$/]&lt;br /&gt;  else&lt;br /&gt;    puts 'connecting to the bluetooth device ...'&lt;br /&gt;    Thread.new{`rfcomm connect 0 #{deviceid}`}&lt;br /&gt;  end&lt;br /&gt;	&lt;br /&gt;  sleep 7&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;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.</description>
      <pubDate>Sun, 25 May 2008 10:57:00 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5536</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Simple Bluetooth Proximity</title>
      <link>http://snippets.dzone.com/posts/show/5528</link>
      <description>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.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;rfcomm connect 0 00:12:D1:E6:57:BE&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;hcitool rssi 00:12:D1:E6:57:BE&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Proximity Results:&lt;br /&gt;-4 ..  0  : Bedroom - nearest to my bed&lt;br /&gt;-4 .. -6  : Hallway&lt;br /&gt;-8 .. -9  : Livingroom - on the computer desk&lt;br /&gt;-9 .. -10 : Livingroom - nearest to the window&lt;br /&gt;</description>
      <pubDate>Tue, 20 May 2008 22:39:59 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5528</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Detect the presence of a Bluetooth device</title>
      <link>http://snippets.dzone.com/posts/show/4928</link>
      <description>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]&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/ruby&lt;br /&gt;#file: whereib.rb&lt;br /&gt;&lt;br /&gt;deviceid = '00:0E:6D:29:38:EB'&lt;br /&gt;devicename = 'Nokia 6600'&lt;br /&gt;&lt;br /&gt;count = 0&lt;br /&gt;while count &lt; 1&lt;br /&gt;  if `hcitool name #{deviceid}`.chomp == devicename &lt;br /&gt;    puts devicename + ' IN RANGE'&lt;br /&gt;    puts Time.now&lt;br /&gt;  else&lt;br /&gt;    puts devicename + ' OUT OF RANGE'&lt;br /&gt;    puts Time.now&lt;br /&gt;  end&lt;br /&gt;  sleep 7&lt;br /&gt;end  &lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 27 Dec 2007 15:11:41 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4928</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
  </channel>
</rss>
