{"id":18076239,"url":"https://github.com/scivision/pybluez-examples","last_synced_at":"2025-06-28T13:32:14.143Z","repository":{"id":28648747,"uuid":"32168022","full_name":"scivision/pybluez-examples","owner":"scivision","description":"Example Bluetooth tasks using the Python PyBluez module","archived":false,"fork":false,"pushed_at":"2023-01-10T06:39:47.000Z","size":32,"stargazers_count":74,"open_issues_count":0,"forks_count":15,"subscribers_count":7,"default_branch":"main","last_synced_at":"2025-04-12T08:15:06.821Z","etag":null,"topics":["bluetooth","bluetooth-low-energy","bluez"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/scivision.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-03-13T16:45:18.000Z","updated_at":"2025-03-19T19:56:20.000Z","dependencies_parsed_at":"2023-01-14T09:15:31.175Z","dependency_job_id":null,"html_url":"https://github.com/scivision/pybluez-examples","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/scivision/pybluez-examples","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scivision%2Fpybluez-examples","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scivision%2Fpybluez-examples/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scivision%2Fpybluez-examples/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scivision%2Fpybluez-examples/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/scivision","download_url":"https://codeload.github.com/scivision/pybluez-examples/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scivision%2Fpybluez-examples/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260629499,"owners_count":23038940,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bluetooth","bluetooth-low-energy","bluez"],"created_at":"2024-10-31T11:09:21.339Z","updated_at":"2025-06-18T20:33:46.646Z","avatar_url":"https://github.com/scivision.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PyBlueZ Examples\n\nExample Bluetooth tasks using the Python\n[PyBluez](https://pybluez.github.io/)\nmodule. Tested using BlueZ 5 on:\n\n* Raspberry Pi 2 with CSR bluetooth 4.0 USB adapter\n* Raspberry Pi 3 / 4 (on-board Bluetooth)\n* laptop with Ubuntu\n* Windows (Bluetooth classic, non-BLE only)\n\n\n## Linux\n\nFor Ubuntu \u003c= 18.04 we use system Python 2.7 for ease of library install.\nIf you have Anaconda/Miniconda, you can alternatively use conda-forge libraries.\n\n1. from Terminal:\n\n   ```sh\n   apt install python-pip python-bluez libbluetooth-dev libboost-python-dev libboost-thread-dev libglib2.0-dev bluez bluez-hcidump\n\n   adduser lp $(whoami)\n   ```\n2. setup Python code:\n\n   ```sh\n   python -m pip install -e .\n   ```\n3. check that your Bluetooth devices are not blocked (should say \"no\"):\n\n   ```sh\n   rfkill list\n   ```\n\n\n## Scan for bluetooth devices from Python\n\n```sh\npython bluetooth_scan.py\n```\n\nIf no Bluetooth devices found in the PyBluez device scan, try each of the following:\n\n```sh\nhcitool scan\n```\n\nand:\n\n```sh\nbluetoothctl\n\nscan on\n```\n\nIf the second way finds devcies but not the first, you may have a chipset issue.\nI have noted this with Marvell hardware on Ubuntu 18.04.\nI did not look into a resolve for this, as I usually use other hardware.\n\n---\n\nIf you get error\n\n\u003e OSError: No such device\n\ncheck that there is a Bluetooth adapter available:\n\n```sh\nhciconfig dev\n```\n\nThe bluetooth adapter may need to be enabled:\n\n```sh\nhciconfig hci0 up\n```\n\n## Non-Python Bluetooth examples\n\nThese example use Bluez directly from Terminal (without Python)\n\n### Bluetooth pairing\n\nusing Bluez5 bluetoothctl agent:\n\n```sh\nhciconfig hci0 up  # enables bt on computer\nhcitool scan       # gets UUID of devices in pairing mode\nhcitool dev        # get BT adapter uuid\n\nbluetoothctl       # starts interactive prompt\nscan on            # scans for UUID of device (BT and BLE) in pairing mode\npair uuid          # where \"uuid\" is what you found with scan\ntrust uuid\nconnect uuid       # after pairing, this is how you connect in the future\n```\n\n## Notes\n\nIf you get the error\n\n\u003e Creating device failed: org.bluez.Error.AuthenticationRejected:\n\u003e Authentication Rejected\n\nthen edit `/etc/bin/bluez-simple-agent`,\n[changing](http://www.wolfteck.com/projects/raspi/iphone/)\n\"KeyboardDisplay\" to \"DisplayYesNo\"\n\nAlso try:\n\n```sh\nbluez-test-device trusted \u003cspeaker uuid\u003e yes\n```\n\nIf connected but lacking sound, try editing `~/.asoundrc`,\n[pasting in](https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=570468):\n\n```\npcm.btspkr {\n   type plug\n   slave {\n       pcm {\n           type bluetooth\n           device \"AA:BB:CC:DD:EE:FF\"\n           profile \"auto\"\n       }\n   }\n   hint {\n       show on\n       description \"BT Speaker\"\n   }\n}\nctl.btspkr {\n  type bluetooth\n}\n\npcm.btspkr_softvol\n{\n   type softvol\n   slave.pcm \"btspkr\"\n   control.name \"Bluetooth\"\n   control.card 0\n}\n\n# Using bluetooth as default :\npcm.!default {\n    type plug\n    slave.pcm \"btspkr_softvol\"\n}\n```\n\n## Bluetooth connect\n\n```sh\nhcitool cc \u003cuuid\u003e\n```\n\nI sometimes saw in Ubuntu that it disconnects after a second, maybe\nbecause system bluetooth menu is overriding with \"off\"?\n\n## Errors\n\n\u003e Cannot open shared library\n\u003e /usr/lib/arm-linux-gnueabihf/alsa-lib/libasound_module_pcm_bluetooth.so\n\n```sh\napt install bluez-alsa\n```\n\n---\n\n\u003e bt_audio_service_open: connect() failed: Connection refused (111)\n\n1. edit `/etc/bluetooth/audio.conf`, pasting in:\n\n   ```ini\n   [general]\n   Enable=Sink,Source,Socket\n   Disable=Media\n\n   AutoConnect=true\n   SCORouting=PCM\n   ```\n2. then:\n\n   ```sh\n   service bluetooth restart\n   ```\n\n## Set Bluetooth speaker as default audio device\n\nFirst test it works with:\n\n```sh\nmpg321 -a bluetooth  myfile.mp3\n```\n\nor:\n\n```sh\nmplayer -ao alsa:device=bluetooth myfile.mp3\n```\n\nThen, list your audio ALSA devices with:\n\n```sh\naplay -L\n```\n\nand you can use:\n\n```sh\nalsamixer\n```\n\n## References\n\n\u003chttps://bitbucket.org/OscarAcena/pygattlib\u003e\n\n\u003chttps://wiki.archlinux.org/index.php/bluetooth_keyboard\u003e\n\n\u003chttps://github.com/oz123/dude/blob/master/bin/speakersswitcher.sh\u003e\n\n\u003chttp://blog.scphillips.com/2013/01/sound-configuration-on-raspberry-pi-with-alsa/\u003e\n\n\u003chttps://wiki.debian.org/Bluetooth/Alsa\u003e\n\n\u003chttp://www.raspberrypi.org/forums/viewtopic.php?f=63\u0026t=92900\u003e\n\n\u003chttp://samtuke.com/2014/10/manually-pair-bluetooth-devices-on-linux-via-cli-nfc-workaround/\u003e\n\n\u003chttp://www.correderajorge.es/bluetooth-on-raspberry-audio-streaming/\u003e\n\n\u003chttp://www.ioncannon.net/linux/1570/bluetooth-4-0-le-on-raspberry-pi-with-bluez-5-x/\u003e\n\n\u003chttps://gist.github.com/dustywilson/8267078\u003e\n\n\u003chttp://www.correlatedcontent.com/blog/bluetooth-keyboard-on-the-raspberry-pi/\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fscivision%2Fpybluez-examples","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fscivision%2Fpybluez-examples","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fscivision%2Fpybluez-examples/lists"}