6

I'm pretty new to raspberry pis, I got my first one last week.

I am trying to setup up a "bluetooth server". It collects data from a serial connection and then sends it, via bluetooth, to an android app I have written. I have written the "bluetooth server" in Java and it works fine if I pair the tablet and pi first, however I would like to run the pi headlessly and be able to connect to it after it boots with new devices.

I have read quite a few example of how to do this but they all seem to use bluetooth-agent, which I have been unable to find and I think might now be deprecated in Jessie? I would not mind using Wheezy if I have to, however I'm using a pi3 and I believe Wheezy would not work with a pi3? I also wouldn't mind using bluetoothctl if someone would kindly explain how it could be automated

I would really like to do something like suggested in the first answer here:

Automatically accepting Bluetooth connections on a Pi 3

Thanks very much for any help you can give.

Gareth Edwards
  • 61
  • 1
  • 1
  • 2
  • I've been looking for the very same information these last days, thinking I was going mad. Everyone references bluetooth-agent and I can't seem to find any information regarding how to actually access this. – Kenji Kina Jan 01 '17 at 22:31

4 Answers4

1

Building off of @NULL's answer, this is what you have to do to get bluetooth discoverable mode turned on at boot.

$ sudo nano /etc/rc.local

Before the exit command at the end put this line in:

echo -e 'discoverable on \nquit' | bluetoothctl

Kyle Goss
  • 11
  • 1
1

Have a look here, this helped me get started...

https://core-electronics.com.au/tutorials/using-usb-and-bluetooth-controllers-with-python.html

Once you have it running, with pair xx.xx.xx.xx.xx.xx and know it works type:

trust xx.xx.xx.xx.xx.xx

And then it will automagically mount it when it is turned on and Bluetooth is active

tarnis
  • 46
  • 4
0

From what I have seen it looks like bluetooth-agent is in fact not available in Jessie. However, this might work to automate bluetoothctl:

#!/bin/bash

# enable bluetooth
sudo systemctl start bluetooth

sleep 1

# run the program bluez
echo -e 'power on\nconnect \t \nquit' | bluetoothctl

Quoting from the site linked above, "You have to pass with echo (with option -e) the commands you want to execute. Every command you have to terminate it with a new line (\n). When I do connect I use the tab (\t) so that I do not need to update the script with the MAC of my keyboard."

The options for bluetoothctl are:

Available commands:
  list                       List available controllers
  show [ctrl]                Controller information
  select <ctrl>              Select default controller
  devices                    List available devices
  paired-devices             List paired devices
  power <on/off>             Set controller power
  pairable <on/off>          Set controller pairable mode
  discoverable <on/off>      Set controller discoverable mode
  agent <on/off/capability>  Enable/disable agent with given capability
  default-agent              Set agent as the default one
  set-scan-filter-uuids [uuid1 uuid2 ...] Set scan filter uuids
  set-scan-filter-rssi [rssi] Set scan filter rssi, and clears pathloss
  set-scan-filter-pathloss [pathloss] Set scan filter pathloss, and clears rssi
  set-scan-filter-transport [transport] Set scan filter transport
  set-scan-filter-clear      Clears discovery filter.
  scan <on/off>              Scan for devices
  info [dev]                 Device information
  pair [dev]                 Pair with device
  trust [dev]                Trust device
  untrust [dev]              Untrust device
  block [dev]                Block device
  unblock [dev]              Unblock device
  remove <dev>               Remove device
  connect <dev>              Connect device
  disconnect [dev]           Disconnect device
  list-attributes [dev]      List attributes
  select-attribute <attribute> Select attribute
  attribute-info [attribute] Select attribute
  read                       Read attribute value
  write <data=[xx xx ...]>   Write attribute value
  notify <on/off>            Notify attribute value
  register-profile <UUID ...> Register profile to connect
  unregister-profile         Unregister profile
  version                    Display version
  quit                       Quit program

You could use discoverable on and pairable on. I have never done much with bluetooth but I don't believe you need much more to get a basic connection.

To get this running at boot, put the line with the path to the script in /etc/rc.local with a & to fork it to the background, e.g

sudo /home/pi/Desktop/StartServer.sh &

You might also be able to use the tool hciconfig instead of bluetoothctl. This might be a more streamlined approach.

NULL
  • 2,240
  • 8
  • 26
  • 49
0

I did a similar project in RaspberryPi3 running Jessie. But I made my own application using Qt 5 and made D-Bus calls to control the Bluetooth adapter. I had implemented it in a GUI but I guess it can be done for a headless mode of working also.

The coding is done using Qt5. I created my own BT agent which would then override the default agent (You have dbus APIs in Qt to do the registration and make the agent default one). You can make use of agent API to define the functions. It requires some coding effort. You can refer to the BlueZ simple agent test code to know what each function does.

If you are planning to use Qt, they have released a new API for BT connectivity called QtBluetooth. I have no experience in using that API, but if interested you can give it a try.