Thread: Python: Serial
View Single Post
Posts: 134 | Thanked: 57 times | Joined on Mar 2008 @ South Bend IN
#5
Below is the code I used. You have to know the address of the device you want to connect to, or offer an option to select it. You can have bluez do a search, and/or list the saved paired devices I think. I didn't do it when I wrote this section cause I was just messing around with the concept. I think that if you want to use a pass code you have to have the n8x0 paired with your receiver.

Once you connect, "self.port" acts just like a regular serial port. Reading stream data doesn't work so well (automated every 15ms is a bunch of data and at least in python things seemed to get bogged down some where). But I could send commands and read responses fine. I've used this same trick to talk to my arduino board, my laptop, and a gps receiver, all without any problems (other than remembering to change the bluesmirf's data rate). You might notice that I check to see if the bluetooth adapter is on (trying to use it when it's off throws an exception), and if not I set a bool to true when I turn on the adapter so that I can turn it off again at application close.

Good luck with your program, I'd be interested to see anything you come up with.

-Brad

Code:
self.bus = dbus.SystemBus();
self.bmgr = dbus.Interface(self.bus.get_object('org.bluez', '/org/bluez'), 'org.bluez.Manager')
self.bus_id = self.bmgr.ActivateService('serial')
self.adapter_add = self.bmgr.DefaultAdapter()
print self.adapter_add
self.adapter = dbus.Interface(self.bus.get_object('org.bluez', self.adapter_add), 'org.bluez.Adapter')
mode = self.adapter.GetMode()
print mode
self.disable_bt = False
if mode == "off":
    print "setting adapter on"
    self.adapter.SetMode("connectable")
    self.disable_bt = True
self.ser_int = dbus.Interface(self.bus.get_object(self.bus_id, '/org/bluez/serial'), 'org.bluez.serial.Manager')
self.device = self.ser_int.ConnectService("00:06:66:00:D4:0C", "spp")
print self.device;
self.port = serial.Serial(str(self.device), 115200, timeout=1)
 

The Following User Says Thank You to hvacengi For This Useful Post: