Bluetooth LE sensors in Clojure

445 views
Skip to first unread message

Mikera

unread,
Oct 21, 2013, 4:07:28 AM10/21/13
to clo...@googlegroups.com
Hi all,

I'm doing some experiments with Bluetooth LE sensors using Clojure. Fun stuff.

The library ecosystem seems a bit sparse though (both in the Clojure and Java space). Currently I'm thinking of hacking together a quick communications stack using Sam Aarons nice little serial-port library (https://github.com/samaaron/serial-port) but I'm hoping there may be a better way. Seems like an interesting use case for core.async in particular.

Has anyone else tries this / discovered any good resources?

   Mike.

Karsten Schmidt

unread,
Oct 21, 2013, 5:42:50 PM10/21/13
to clo...@googlegroups.com
IIRC Sam's wrapper is using the original RXTX lib which hasn't been
updated since ~2006 and is only 32bits. There're a couple of more
recent forks (w/ x-platform 64bit binaries), most notable the one by
CMU's Robotics dept:

https://code.google.com/p/create-lab-commons/source/browse/#svn%2Ftrunk%2Fjava%2Flib%2Frxtx

It shouldn't be too hard to bundle these up and do a Clojure wrapper...

Alternatively, if you're targeting Android, you'll get API access from
4.3 onwards:
http://developer.android.com/guide/topics/connectivity/bluetooth-le.html

Hth! K.
> --
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@googlegroups.com
> Note that posts from new members are moderated - please be patient with your
> first post.
> To unsubscribe from this group, send email to
> clojure+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to clojure+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.



--
Karsten Schmidt
http://postspectacular.com | http://toxiclibs.org | http://toxi.co.uk

Mikera

unread,
Oct 21, 2013, 9:15:55 PM10/21/13
to clo...@googlegroups.com, in...@toxi.co.uk
Thanks, Karsten, useful links!

As it happens I managed to get it working on 64-bit Windows last night using Sam's regular serial-port. 

Code here for anyone interested:


Interestingly, I found a nice way to batch up a series of individual bytes arriving on the COM port into complete Bluetooth LE messages using core.async. I'd be interested in any comments from core.async experts on whether I'm doing this correctly but it seems to work:

(defn ble-messages 
  "Creates a Bluetooth LE listener channel that reads bytes from a channel c
   and returns a new value when the message is complete"
  ([c]
    (let [result-chan (chan)]
      (go (loop []
            (let [eb (<! c)]  ;; Type, expected to be 0x04 (Event)
              (when-not (== 0x04 eb) (println (str "Unknown byte: " (hex/hex-string eb))) (recur))
              (let [ec (<! c) ;; Event code, typically 0xFF (HCI_LE_ExtEvent)
                    elen (<! c)  ;; Event data length in bytes
                    ^bytes res (byte-array (+ 3 elen))]
                (aset res 0 (unchecked-byte eb))
                (aset res 1 (unchecked-byte ec))
                (aset res 2 (unchecked-byte elen))
                (dotimes [i elen]
                  (aset res (+ i 3) (unchecked-byte (<! c))))
                (>! result-chan res)
                (recur)))))
      result-chan)))
Reply all
Reply to author
Forward
0 new messages