0

I know that questions are supposed to involve code but coming from someone who has never used a pi, the linux environment, or python before, is there a way to write a python script using pybluez to make the raspberry pi zero w's Bluetooth visible on other devices like a phone, answer pair requests, store the devices paired and connect the to the devices paired? Thanks. A written tutorial would help out a lot. I updated with code to allow the pi to be discover-able, but I am unsure on how to use the command "sudo bluetooth-agent 1234" so that it listens for a pair request.

import subprocess

from bluetooth import *

print "performing inquiry..."

cmd = 'sudo hciconfig hci0 piscan'

subprocess.check_output(cmd, shell = True)

cmd = 'sudo bluetooth-agent 1234'

nearby_devices = discover_devices(lookup_names = True)

print "found %d devices" % len(nearby_devices)

for name, addr in nearby_devices:
     print " %s - %s" % (addr, name)
  • I updated answer for you. Your code works for me. – Dr.Rabbit Oct 6 '17 at 0:02
1

You can make it discoverable using subprocess in Python without using pybluez.

A bluetooth command list is found here

import subprocess

cmd = 'sudo hciconfig hci0 piscan'
subprocess.check_output(cmd, shell = True )

Or check out the PyBluez repository, they have a bunch of examples included.

sudo git clone https://github.com/karulis/pybluez.git

update

Make The Raspberry Pi Discoverable

sudo hciconfig hci0 piscan

To allow the connection to complete you need to also do the following step to allow a remote device to actually connect with the pairing key 1234

sudo bluetooth-agent 1234

https://github.com/karulis/pybluez#pybluez

Update 2

Check you have the correct sources

sudo apt-get update
sudo apt-get install bluetooth
sudo apt-get install bluez
sudo apt-get install python-bluez
  • I have looked over the github before, I don't think that there is really anything on making the bluetooth discover-able for other devices to discover the pi instead of having the pi discover the other devices. Basically the pi needs to be the slave while other devices are the master, sending sound to it for speakers and other stuff. – Noah Tanenholtz Oct 1 '17 at 3:16
  • updated info for you, let me know how you go. – Dr.Rabbit Oct 1 '17 at 3:54
  • I'll test it out thanks, but is there a way to do without the shell? – Noah Tanenholtz Oct 5 '17 at 19:45
  • Also another question, how can I make the pi listen for pair requests, and then pair/connect with the device? – Noah Tanenholtz Oct 5 '17 at 19:48
  • sudo hciconfig hci0 piscan this will make it discoverable and sudo bluetooth-agent 1234 this will allow the connection to complete with the paircode 1234 I updated the answer for you. – Dr.Rabbit Oct 5 '17 at 21:14

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.