Page 1 of 2 12 LastLast
Results 1 to 15 of 20

Thread: Bluetooth proximity monitor

  1. #1

    Bluetooth proximity monitor

    Hello,
    I haven't found anything similar on this site, so I hope it might be useful.

    I wrote a few scripts to automatically monitor bluetooth devices in range of BT USB stick attached to Asus router (for example mobile phones of people coming and leaving home or office). Devices can be in hidden mode.

    !!! Some modifications according your own environment will be needed, mainly paths and usernames !!!

    Requirements (see other related topics in this forum how to get it working):
    - kernel modules for USB bluetooth stick loaded
    - bluez-libs, bluez-utils (or bluez2-libs, bluez2-utils) packages installed
    - sqlite2 (or sqlite) package
    - procps (for pgrep used in startup script)


    Main script blueproximity.sh can be run directly from command line or using simple script S99local:
    Code:
    #!/bin/sh
    #
    # Startup script for local custom services
    case $1 in
        start)
            echo "Starting blueproximity.sh"
            su pavel -c "exec nice sh ~pavel/btproximity/blueproximity.sh" >/dev/null 2>&1 &
        ;;
        stop)
            pid=`pgrep -f blueproximity.sh`
            if [ -n "$pid" ] ; then
              echo "Stopping blueproximity.sh"
              kill $pid
            fi
        ;;
        restart)
            $0 stop
            sleep 1
            $0 start
        ;;
        *)
            echo "usage: $0 (start|stop|restart)"
            exit 1
        ;;
    esac
    Script doesn't require to be run under root privileges - I used my own account 'pavel'. It uses sqlite (version 2) for data storage - database has to be created in advance:
    Code:
    sqlite btlog.db < db.sql
    Then it is neccessary to add one or more MAC addresses and names into 'devices' table. Value of 'enabled' allows to selectively disable or enable scanning of devices.

    There are some variables that can be changed in the beginning of the script (blueproximity.sh):
    Code:
    treshold=2      # no of successive unsuccessful tries to treat device as out of range
    cycletime=120   # duration of one cycle
    treshold means that device will be logged as 'away' after two succesive unsuccessful attempts to find it.
    cycletime=120 means that every enabled device will be scanned every 2 minutes.

    Every device also has it's own directory (automatically created) which contain temporary state file. If you create a script run-in and/or run-out inside, it will be run on corresponding event with device name as argument. Additionally, 'run-in' gets second argument with minutes since last 'OUT' event.

    Finally, script btlog.php is a script to display records from the database (tables status and log). It is very simple - if you develop more sophisticated version, possibly including management of devices in database, you're welcome to post it here ;-).
    Script was tested under lighttpd and php-fcgi with extensions pdo.so and sqlite.so enabled in php.ini.

    I hope it will be useful for somebody.

    Paul
    Attached Files Attached Files
    Last edited by taylor729; 12-07-2009 at 20:20. Reason: Extending description

  2. #2
    Join Date
    Aug 2008
    Location
    Poland, Biaіa Podlaska
    Posts
    276
    Great! Thank you.
    Last edited by Lesiuk; 16-07-2009 at 15:36.

  3. #3
    I have made some changes and improvements - and also I organized directory structure and added some comments.
    • upgraded DB support to sqlite3
    • rate of looking for a device decreases after specified time the device hasn't been seen
    • optimized for less number of disk writes
    • addes some comments ;-)
    • more minor improvements in all parts


    Starting from this version, temporary files are created in /tmp/btproximity, but option for your own handler scripts remains. You only need to manually create directory with the same name as BT device located in the directory which contains blueproximity.sh script and put script named run-in and/or run-out into.

    Script gets following arguments when it is run:
    device_name mac_address minutes_away

    minutes_away has meaning only for IN event.

    I've learned how to handle sqlite3 via DSO class in PHP, so there is no need to use old sqlite2. Lines
    Code:
    extension=pdo.so
    extension=pdo_sqlite.so
    must be present in your /opt/etc/php.ini file

    initializing database:
    Code:
    sqlite3 btlog.db < db.sql
    or migrating database from sqlite v2 to sqlite v3:
    Code:
    sqlite btlog.db ".dump" > btlog_dump
    ipkg remove sqlite2 ; ipkg install sqlite
    rm btlog.db
    sqlite3 btlog.db < btlog_dump
    I've added optional decreasing of search rate because it takes about 20 second to look for device that's not present (at least in my case) and time to look for all devices is limited by length of cycle. So it saves some time on devicess that haven't been present for longer time (default setting is 24 hours).

    There is still no web management of devices, maybe in next version or up to someone else ;-)

    PS etc/init.d/S99local contains a small mistake - a quote char at line 9 is needed to remove. Newest version below is OK.
    Attached Files Attached Files
    Last edited by taylor729; 12-11-2009 at 13:20.

  4. #4
    Quote Originally Posted by taylor729 View Post
    Then it is neccessary to add one or more MAC addresses and names into 'devices' table. Value of 'enabled' allows to selectively disable or enable scanning of devices.
    how to add MAC addresses and names into 'devices' table?
    WL500gP v2 + USB WD 160Gb + HTC Shift USB Extension kit + QuickCam Pro 4000 + LOGITECH S-150 DIGITAL USB SPEAKERS + Mini Bluetooth Dongle
    installed: rtorrent, ados, samba3, lighttpd, palantir ,cron, rrd, mpd, mcabber

  5. #5
    For example:
    Code:
    sqlite3 btlog.db 'insert into devices (mac, name, enabled) values ("11:22:33:44:55:66", "MyDeviceName", 1)'
    Of course, database file btlog.db must already exist (the command for DB initialisation is in my first post).

  6. #6
    New version:
    • additionally to multiple probing ('treshold' times) of device before considering it as away, there is another (i think better) function - OUT events are written after 'writedelay' successive unsuccessful probes, but with time of first unsuccessful probe. Status is updated immediately
    • Reduced disk writes by storing absolute time when a device became unavailable instead of counting cycles of being away.


    I have also added a simple script for sending OBEX notes to mobile phones (tested on several Nokias). It is used in example of script run on IN event to send welcome message to the incoming phone.
    Attached Files Attached Files
    Last edited by taylor729; 10-11-2009 at 19:05.

  7. #7
    ./S99local: line 30: syntax error: "(" unexpected (expecting "fi")

    in btproximity_0.3

    nice sh /opt/btproximity/blueproximity.sh" >/dev/null 2>&1 &
    I think must be
    nice sh /opt/btproximity/blueproximity.sh >/dev/null 2>&1 &
    Last edited by derrij; 10-11-2009 at 16:14.
    WL500gP v2 + USB WD 160Gb + HTC Shift USB Extension kit + QuickCam Pro 4000 + LOGITECH S-150 DIGITAL USB SPEAKERS + Mini Bluetooth Dongle
    installed: rtorrent, ados, samba3, lighttpd, palantir ,cron, rrd, mpd, mcabber

  8. #8
    Sorry, I'm using slightly different version of S99local init script and I forgot to remove one quote, when creating package for this forum. File in attachment in the post above is now correct.

  9. #9
    Quote Originally Posted by derrij View Post
    nice sh /opt/btproximity/blueproximity.sh" >/dev/null 2>&1 &
    I think must be
    nice sh /opt/btproximity/blueproximity.sh >/dev/null 2>&1 &
    Yes, you're right.
    Sorry

  10. #10
    Thanks for the script.Works fine.

    P.S. In btproximity_0.3.tgz attachment btlog.php incorrect path in $dbname.
    Last edited by derrij; 10-11-2009 at 18:56.
    WL500gP v2 + USB WD 160Gb + HTC Shift USB Extension kit + QuickCam Pro 4000 + LOGITECH S-150 DIGITAL USB SPEAKERS + Mini Bluetooth Dongle
    installed: rtorrent, ados, samba3, lighttpd, palantir ,cron, rrd, mpd, mcabber

  11. #11
    Quote Originally Posted by derrij View Post
    Thanks for the script.Works fine.

    P.S. In btproximity_0.3.tgz attachment btlog.php incorrect path in $dbname.
    Thanks for notice - also forgotten to change this from environment that I'm running the script in. Corrected in the attachement above.

    I hope you find my work useful.

    Pavel

  12. #12
    Quote Originally Posted by taylor729 View Post
    Every device also has it's own directory (automatically created) which contain temporary state file. If you create a script run-in and/or run-out inside, it will be run on corresponding event with device name as argument. Additionally, 'run-in' gets second argument with minutes since last 'OUT' event.
    is this working in btproximity_0.3? I can't get script to run on event?
    WL500gP v2 + USB WD 160Gb + HTC Shift USB Extension kit + QuickCam Pro 4000 + LOGITECH S-150 DIGITAL USB SPEAKERS + Mini Bluetooth Dongle
    installed: rtorrent, ados, samba3, lighttpd, palantir ,cron, rrd, mpd, mcabber

  13. #13
    Yes, this has been working in all versions.
    The only difference is that first version used this directory also for temporary file and created it itself. Since version 0.2, temporary files go to /tmp/btproximity and directory for scripts run-in and run-out has to be created manually. It is located at the same level as btproximity.sh script (try to rename dev-name-example directory to real name of one of your bluetooth device).

    I have added this information to the post related to version 0.2 (post #3).
    Last edited by taylor729; 12-11-2009 at 13:29.

  14. #14
    Code:
    # call external script (if exists); $1 = device name, $2 = BT addr, $3 = minutes away
      timeaway=${5:-0}
      if [ -x "${mydir}/$1/run-$3" ] ; then
        "${mydir}/$1/run-$3" $1 $2 $(($timeaway / 60))
      fi
    }
    I was trying to rename dev-name-example, but nothing happens.
    And I think btproximity.sh trying call run-1... , but not run-in.
    (run-1, I was trying to)
    WL500gP v2 + USB WD 160Gb + HTC Shift USB Extension kit + QuickCam Pro 4000 + LOGITECH S-150 DIGITAL USB SPEAKERS + Mini Bluetooth Dongle
    installed: rtorrent, ados, samba3, lighttpd, palantir ,cron, rrd, mpd, mcabber

  15. #15
    Quote Originally Posted by derrij View Post
    Code:
    # call external script (if exists); $1 = device name, $2 = BT addr, $3 = minutes away
      timeaway=${5:-0}
      if [ -x "${mydir}/$1/run-$3" ] ; then
        "${mydir}/$1/run-$3" $1 $2 $(($timeaway / 60))
      fi
    }
    I was trying to rename dev-name-example, but nothing happens.
    And I think btproximity.sh trying call run-1... , but not run-in.
    (run-1, I was trying to)
    Don't let to confuse yourself, $3 in the comment doesn't mean $3 in the code, but third argument passed to run-{in,out} script.
    $3 that is passed to the function log () is name of the event (in, out). See lines 139 and 162. So, script names should really be run-in and run-out.
    They have to have eXecute right set.
    Last edited by taylor729; 13-11-2009 at 04:00.

Page 1 of 2 12 LastLast

Similar Threads

  1. Bluetooth в wl500gP
    By TIk in forum Russian Discussion - РУССКИЙ (RU)
    Replies: 410
    Last Post: 23-02-2011, 00:14
  2. vnstat - Network traffic monitor with log
    By WLAN-Fan in forum German Discussion - Deutsch (DE)
    Replies: 4
    Last Post: 22-09-2008, 13:53
  3. [HOW TO] Bluetooth in wl500g
    By TIk in forum WL-500g/WL-500gx Tutorials
    Replies: 1
    Last Post: 17-09-2008, 22:38

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •