Using Bluetooth serial devices on NetBSD

From Wikistix
Jump to: navigation, search

In this example, I'm talking to a Bluetooth serial device connected to an Arduino ATmega2560, which is running a simple thermostat program.


Find your local Bluetooth controller:

ksh$ btconfig -l
ubt0

Configure your Bluetooth controller, in this case, making it permanent, configured at startup:

ksh$ egrep '(blue|btcon)' /etc/rc.conf
bluetooth=YES
btconfig_ubt0='auth encrypt iscan name fubar'
ksh$ sudo /etc/rc.d/bluetooth restart
disabling Bluetooth controllers: ubt0.
Waiting for PIDS: 364 291.
configuring Bluetooth controllers: ubt0.

Dump out the current configuration to check:

ksh$ btconfig -v ubt0
ubt0: bdaddr 99:88:66:00:22:e8 flags 0x403<UP,RUNNING,MASTER>
        num_cmd = 1
        num_acl = 10 (max 10), acl_mtu = 310
        num_sco = 8 (max 8), sco_mtu = 64
        HCI version: unknown
        class: [0x000100] Computer
        name: "fubar"
        voice: [0x0060]
        pin: variable
        inquiry mode: std
        options: iscan pscan auth encrypt switch hold sniff park

Run a Bluetooth scan to find your target device:

ksh$ sudo btconfig ubt0 inquiry
Device Discovery from device: ubt0 ... 1 response
  1: bdaddr 00:02:0a:01:ee:38
   : name "U630AC"
   : class [0x001f00]
   : page scan rep mode 0x01
   : clock offset 18218
   : rssi 0

Set up pairing using the device address as reported by the scan:

ksh$ btpin -a 00:02:0a:01:ee:38 -P -p 1234 
Pairing.. done

You can then query the device using the Service Discovery Protocol. Not all methods are supported by all devices, hence no output from the Browse command here, so we explicitly check for the "Serial Port" service:

ksh$ sdpquery -d ubt0 -a 00:02:0a:01:ee:38 browse
ksh$ sdpquery -d ubt0 -a 00:02:0a:01:ee:38 search sp
ServiceRecordHandle: 0x00010000
ServiceClassIDList: 
    Serial Port
ProtocolDescriptorList: 
    L2CAP
    RFCOMM (channel 1)
LanguageBaseAttributeIDList: 
    en.UTF-8 base 0x0100
ServiceName: "Dev B"

Now you can connect to the serial device, ^C to exit:

ksh$ rfcomm_sppd -d ubt0 -a 00:02:0a:01:ee:38 
rfcomm_sppd[8274]: Starting on stdio...
t:temp,d:date,s:status
Set: D:date
2014-06-12 13:28:08
22.5929
rfcomm_sppd[8274]: Completed on stdio

To make it easier, you can save the Bluetooth address in your hosts file so you can refer to it by name:

ksh$ cat /etc/bluetooth/hosts 
# $NetBSD: hosts,v 1.1 2006/06/19 15:44:35 gdamore Exp $
# $Id: hosts,v 1.1 2006/06/19 15:44:35 gdamore Exp $
# $FreeBSD: /repoman/r/ncvs/src/etc/bluetooth/hosts,v 1.2 2005/11/10 19:09:22 emax Exp $
#
# Bluetooth Host Database
#
# This file should contain the Bluetooth addresses and aliases for hosts.
#
# BD_ADDR               Name [ alias0 alias1 ... ]

# 00:11:22:33:44:55     phone
00:02:0a:01:ee:38       U630AC

... which is somewhat easier to remember:

ksh$ rfcomm_sppd -d ubt0 -a U630AC           
rfcomm_sppd[18113]: Starting on stdio...
t:temp,d:date,s:status
Set: D:date
2014-06-12 13:30:34
22.5878
rfcomm_sppd[18113]: Completed on stdio

Misinformation found herein copyright Paul Ripke (aka “stix”) stixpjr@gmail.com.