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-3 of 3 total  RSS 

Using Bluetooth to control Asterisk calls.

Redirect an Asterisk call

   1  
   2      de61.old_status = get_status(de61.file) # found is either true or false
   3      de61.old_location = get_location(de61.file) # location is either 'at home', 'gone to bed' or 'asleep'
   4      
   5      while count < 1
   6  
   7        puts 'old status : ' + de61.old_status.to_s
   8        de61.new_status = check_device(de61.id, de61.name)
   9        puts 'new_status: ' + de61.new_status.to_s
  10  
  11  
  12        if de61.new_status and de61.old_status
  13          if de61.old_status == false
  14            puts 'connecting to the bluetooth device ...'
  15            Thread.new{`rfcomm connect 0 #{de61.id}`}
  16            sleep 12
  17          end
  18          de61.new_location = check_location(de61.id) 
  19        end
  20        
  21        puts 'old_location: ' + de61.old_location.to_s
  22        puts 'new_location: ' + de61.new_location.to_s
  23        update_status(de61.project, de61.new_status, de61.new_location) if ((de61.old_status != de61.new_status.to_s) or (de61.old_location != de61.new_location)) 
  24          
  25        mystatus = ''
  26        
  27        if  (not de61.old_status == de61.new_status) then
  28          if (de61.new_status.to_s == 'true') then
  29              mystatus = ' has entered the building'
  30              callto('SIP/line1')
  31          elsif  (de61.new_status == false) then
  32            mystatus = ' has left the building'
  33            callto('SIP/07736668666@sipgate')
  34          end
  35          puts 'updating in or out the building'
  36          ms = MyStatus.new(mystatus)
  37        elsif (de61.old_location.to_s != de61.new_location.to_s)
  38          if de61.old_location.match(/sleep/) and de61.new_location.match(/home|bed/) and not old_mystatus.match(/awoken/) then
  39            mystatus = ' has awoken'
  40          elsif (de61.old_location.match(/home|bed/)) and de61.new_location.match(/sleep/) 
  41            mystatus = ' has gone to sleep'           
  42          elsif (de61.old_location.match(/home/)) and de61.new_location.match(/bed/) 
  43            mystatus = ' has gone to bed'
  44          elsif de61.old_location.match(/bed/) and de61.new_location.match(/home/) 
  45            mystatus = ' has got out of bed'
  46          end
  47          puts 'updating home location'
  48          ms = MyStatus.new(mystatus) if mystatus != ''
  49        end
  50  
  51  
  52        de61.old_location = de61.new_location
  53        de61.new_location = 'at home'
  54        de61.old_status = de61.new_status
  55        old_mystatus = mystatus
  56        sleep 7
  57      end
  58    end
  59      
  60    def callto(dialstring)
  61      file = File.new('/etc/asterisk/ruby/call-to.txt','w')
  62      file.puts 'Dial(' + dialstring + ')'
  63      file.close
  64    end


file: extensions.conf
   1  
   2  exten => 5168666,n,ReadFile(dialplan1=/etc/asterisk/ruby/call-to.txt,130)
   3  exten => 5168666,n,Exec(${dialplan1})

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
« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS