Translate

Saturday, June 16, 2018

Set Raspberry Pi as Bluetooth slave

Some notes about how to connect a Raspberry Pi 3 via Bluetooth to an Android phone.
These notes shows some suggestions about how to connect an Android phone to the Raspberry set as Bluetooth slave.
Android side I'm using an off the shelf app.


Shopping list

  • Raspberry Pi 3 B+
  • Raspbian lite
  • BlueTerm on Android Phone

Raspberry Pi setting

  • install the Raspbian lite on a micro SD card
    (since using a Raspberry Pi 3 I installed the latest, Raspbian Stretch - for older version of Raspberry I had better luck with Jessie)
  • connect a USB keyboard, a HDMI monitor and a wired LAN or use PiBackery (see this article for more details)
  • log in
  • update
    • sudo apt-get update
    • sudo apt-get upgrade
  • run raspi-config
    • sudo raspi-config
      • set hostname
      • set locale
      • set timezone
      • enable WiFi
      • enable I2C
      • enable SPI
      • enable SSH
  • reboot - now is possible to disconnect LAN cable, HDMI monitor and keyboard

Bluetooth activation as slave

  • open a terminal and login via SSH
  • activate the ISCAN service
    • sudo hciconfig hci0 piscan
  • verify if service is running
    • sudo hciconfig -a
      Should show some lines and among them :  UP RUNNING PSCAN ISCAN

      For example :
sudo hciconfig -a
        hci0: Type: Primary  Bus: UART
BD Address: B8:27:EB:F1:EB:2D  ACL MTU: 1021:8  SCO MTU: 64:1
UP RUNNING PSCAN ISCAN
RX bytes:780 acl:0 sco:0 events:50 errors:0
TX bytes:2778 acl:0 sco:0 commands:50 errors:0
Features: 0xbf 0xfe 0xcf 0xfe 0xdb 0xff 0x7b 0x87
Packet type: DM1 DM3 DM5 DH1 DH3 DH5 HV1 HV2 HV3
Link policy: RSWITCH SNIFF
Link mode: SLAVE ACCEPT
Name: 'NiRis'
Class: 0x000000
Service Classes: Unspecified
Device Class: Miscellaneous,
HCI Version: 4.1 (0x7)  Revision: 0x8b
LMP Version: 4.1 (0x7)  Subversion: 0x6119
Manufacturer: Broadcom Corporation (15)

The Raspberry Pi is configured in slave mode for Bluetooth.

The phone scanning should show the hostname set when scanning for Bluetooth.
The setting by default is NOT retained after a reboot, i.e. is necessary to re-issue the sudo hciconfig hci0 piscan command just before to start the scan on the phone.
Clicking Pair on the phone should work.

At this point is possible to install other things to handle Bluetooth via Python :
  • sudo apt-get install bluetooth blueman bluez
  • sudo apt-get install python-bluetooth
With the python bluetooth library in place will be possible to write some code to handle the bluetooth.
For now just use Blueterm to connect to the Raspberry.


No comments:

Post a Comment