All Projects → scivision → pybluez-examples

scivision / pybluez-examples

Licence: MIT License
Example Bluetooth tasks using the Python PyBluez module

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to pybluez-examples

py-bluetooth-utils
Python module containing bluetooth utility functions, in particular for easy BLE scanning and advertising
Stars: ✭ 60 (+1.69%)
Mutual labels:  bluetooth, bluetooth-low-energy, bluez
bluer
BlueR — Official BlueZ Bindings for Rust
Stars: ✭ 43 (-27.12%)
Mutual labels:  bluetooth, bluetooth-low-energy, bluez
mijia-homie
A Homie MQTT bridge for the Xiaomi Mijia 2 hygrometer-thermometer. This repo also serves as the monorepo for a handful of helper crates related to Homie and Bluetooth.
Stars: ✭ 40 (-32.2%)
Mutual labels:  bluetooth, bluetooth-low-energy
awesome-bluetooth-security
List of Bluetooth BR/EDR/LE security resources
Stars: ✭ 220 (+272.88%)
Mutual labels:  bluetooth, bluetooth-low-energy
android-ble-made-easy
An Android Library for handling Bluetooth Low Energy on Android Easy
Stars: ✭ 34 (-42.37%)
Mutual labels:  bluetooth, bluetooth-low-energy
hcitool
Bluetooth Host Controller Interface Command Line Tool for for sending HCI commands on macOS and Linux
Stars: ✭ 72 (+22.03%)
Mutual labels:  bluetooth, bluez
Bluetooth-ble-beamer-and-scanner-for-tracing-corona-virus-infected-individual
Bluetooth ble beacon beamer and scanner for tracing corona virus infected person similar to Trace Together app
Stars: ✭ 26 (-55.93%)
Mutual labels:  bluetooth, bluetooth-low-energy
cordova-posprinter-sample
Cordova/Inoic sample for Epson ESC/POS printer
Stars: ✭ 24 (-59.32%)
Mutual labels:  bluetooth, bluetooth-low-energy
BLELib
This library contains many of the features you need to interact with BLE peripherals
Stars: ✭ 21 (-64.41%)
Mutual labels:  bluetooth, bluetooth-low-energy
ble-heart-rate-demo
Web bluetooth heart rate monitor
Stars: ✭ 24 (-59.32%)
Mutual labels:  bluetooth, bluetooth-low-energy
bluez-rs
Control Bluetooth on Linux.
Stars: ✭ 36 (-38.98%)
Mutual labels:  bluetooth, bluez
bluetooth-manager
Java Bluetooth Manager. A library/framework for managing bluetooth adapters, bluetooth devices, GATT services and characteristics
Stars: ✭ 75 (+27.12%)
Mutual labels:  bluetooth, bluetooth-low-energy
pycalima
Python interface for Pax Calima Fan via Bluetooth LE
Stars: ✭ 34 (-42.37%)
Mutual labels:  bluetooth, bluetooth-low-energy
H.E.L.P.
Home Environment Locating People 🍍
Stars: ✭ 19 (-67.8%)
Mutual labels:  bluetooth, bluetooth-low-energy
pyrinas-ble-client
nRF52 SDK(v16) based OS for use with Particle Xenon and other nRF52840 boards.
Stars: ✭ 15 (-74.58%)
Mutual labels:  bluetooth, bluetooth-low-energy
ios
CoThings's iOS application. CoThings is a realtime counter for shared things.
Stars: ✭ 13 (-77.97%)
Mutual labels:  bluetooth, bluetooth-low-energy
GATT
Bluetooth Generic Attribute Profile (GATT) for Swift (Supports Linux)
Stars: ✭ 48 (-18.64%)
Mutual labels:  bluetooth, bluetooth-low-energy
ble-utilities-unreal
This is Unreal Engine plugin that allows to scan for BLE devices with Cycling Power service running, connect to one of them and subscribe for its notifications.
Stars: ✭ 48 (-18.64%)
Mutual labels:  bluetooth, bluetooth-low-energy
PiBeacon
Low-cost iBeacon using Raspberry Pi
Stars: ✭ 41 (-30.51%)
Mutual labels:  bluetooth, bluetooth-low-energy
bluetooth-iot-service-python
This application connects two devices over Bluetooth and allows one to send messages to the other using json. Raspberry Pi Bluetooth interfacing with Linux via RFCOMM BT network
Stars: ✭ 23 (-61.02%)
Mutual labels:  bluetooth, bluez

PyBlueZ Examples

Example Bluetooth tasks using the Python PyBluez module. Tested using BlueZ 5 on:

  • Raspberry Pi 2 with CSR bluetooth 4.0 USB adapter
  • Raspberry Pi 3 / 4 (on-board Bluetooth)
  • laptop with Ubuntu
  • Windows (Bluetooth classic, non-BLE only)

Linux

For Ubuntu <= 18.04 we use system Python 2.7 for ease of library install. If you have Anaconda/Miniconda, you can alternatively use conda-forge libraries.

  1. from Terminal:

    apt install python-pip python-bluez libbluetooth-dev libboost-python-dev libboost-thread-dev libglib2.0-dev bluez bluez-hcidump
    
    adduser lp $(whoami)
  2. setup Python code:

    python -m pip install -e .
  3. check that your Bluetooth devices are not blocked (should say "no"):

    rfkill list

Scan for bluetooth devices from Python

python bluetooth_scan.py

If no Bluetooth devices found in the PyBluez device scan, try each of the following:

hcitool scan

and:

bluetoothctl

scan on

If the second way finds devcies but not the first, you may have a chipset issue. I have noted this with Marvell hardware on Ubuntu 18.04. I did not look into a resolve for this, as I usually use other hardware.


If you get error

OSError: No such device

check that there is a Bluetooth adapter available:

hciconfig dev

The bluetooth adapter may need to be enabled:

hciconfig hci0 up

Non-Python Bluetooth examples

These example use Bluez directly from Terminal (without Python)

Bluetooth pairing

using Bluez5 bluetoothctl agent:

hciconfig hci0 up  # enables bt on computer
hcitool scan       # gets UUID of devices in pairing mode
hcitool dev        # get BT adapter uuid

bluetoothctl       # starts interactive prompt
scan on            # scans for UUID of device (BT and BLE) in pairing mode
pair uuid          # where "uuid" is what you found with scan
trust uuid
connect uuid       # after pairing, this is how you connect in the future

Notes

If you get the error

Creating device failed: org.bluez.Error.AuthenticationRejected: Authentication Rejected

then edit /etc/bin/bluez-simple-agent, changing "KeyboardDisplay" to "DisplayYesNo"

Also try:

bluez-test-device trusted <speaker uuid> yes

If connected but lacking sound, try editing ~/.asoundrc, pasting in:

pcm.btspkr {
   type plug
   slave {
       pcm {
           type bluetooth
           device "AA:BB:CC:DD:EE:FF"
           profile "auto"
       }
   }
   hint {
       show on
       description "BT Speaker"
   }
}
ctl.btspkr {
  type bluetooth
}

pcm.btspkr_softvol
{
   type softvol
   slave.pcm "btspkr"
   control.name "Bluetooth"
   control.card 0
}

# Using bluetooth as default :
pcm.!default {
    type plug
    slave.pcm "btspkr_softvol"
}

Bluetooth connect

hcitool cc <uuid>

I sometimes saw in Ubuntu that it disconnects after a second, maybe because system bluetooth menu is overriding with "off"?

Errors

Cannot open shared library /usr/lib/arm-linux-gnueabihf/alsa-lib/libasound_module_pcm_bluetooth.so

apt install bluez-alsa

bt_audio_service_open: connect() failed: Connection refused (111)

  1. edit /etc/bluetooth/audio.conf, pasting in:

    [general]
    Enable=Sink,Source,Socket
    Disable=Media
    
    AutoConnect=true
    SCORouting=PCM
  2. then:

    service bluetooth restart

Set Bluetooth speaker as default audio device

First test it works with:

mpg321 -a bluetooth  myfile.mp3

or:

mplayer -ao alsa:device=bluetooth myfile.mp3

Then, list your audio ALSA devices with:

aplay -L

and you can use:

alsamixer

References

https://bitbucket.org/OscarAcena/pygattlib

https://wiki.archlinux.org/index.php/bluetooth_keyboard

https://github.com/oz123/dude/blob/master/bin/speakersswitcher.sh

http://blog.scphillips.com/2013/01/sound-configuration-on-raspberry-pi-with-alsa/

https://wiki.debian.org/Bluetooth/Alsa

http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=92900

http://samtuke.com/2014/10/manually-pair-bluetooth-devices-on-linux-via-cli-nfc-workaround/

http://www.correderajorge.es/bluetooth-on-raspberry-audio-streaming/

http://www.ioncannon.net/linux/1570/bluetooth-4-0-le-on-raspberry-pi-with-bluez-5-x/

https://gist.github.com/dustywilson/8267078

http://www.correlatedcontent.com/blog/bluetooth-keyboard-on-the-raspberry-pi/

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].