#!/usr/bin/python # Requirements: # sudo aptitude install python-bluetooth # Information Sources: # http://code.google.com/p/pybluez/source/browse/trunk/examples/simple/rfc... # http://people.csail.mit.edu/albert/bluez-intro/x290.html#py-rfcomm-serve... import bluetooth import threading name = "BluetoothChat" uuid = "fa87c0d0-afac-11de-8a39-0800200c9a66" server_sock = bluetooth.BluetoothSocket( bluetooth.RFCOMM ) server_sock.bind(("", bluetooth.PORT_ANY)) server_sock.listen(1) port = server_sock.getsockname()[1] bluetooth.advertise_service( server_sock, name, uuid ) print "Waiting for connection on RFCOMM channel %d" % port class echoThread(threading.Thread): def __init__ (self,sock,client_info): threading.Thread.__init__(self) self.sock = sock self.client_info = client_info def run(self): try: while True: data = self.sock.recv(1024) if len(data) == 0: break print self.client_info, ": received [%s]" % data self.sock.send(data) print self.client_info, ": sent [%s]" % data except IOError: pass self.sock.close() print self.client_info, ": disconnected" while True: client_sock, client_info = server_sock.accept() print client_info, ": connection accepted" echo = echoThread(client_sock, client_info) echo.setDaemon(True) echo.start() server_sock.close() print "all done"
rd@radekdostal.com
+43 681 815 945 10
skype: radekdostal
www.mimo-domov.cz - Czech and Slovak people abroad
Out of curiosity. What
Out of curiosity. What version of the Android OS are you using, build of linux, etc..? I have been trying to implement serial communication over bluetooth (android <-> computer) for sometime and I cannot seem to make it work. I tried your code, but it is failing as well. I sent you a skype request if you are willing to chat about this (and hopefully help me solve my problem).
Wrote up my problem here. http://stackoverflow.com/questions/4395386/android-bluetooth-bluetoothsocket-cannot-connect
Thanks,
Bryan
Re: Out of curiosity
Hi,
I have used Ubuntu 10.04 (32bit) and HTC desire as my testing system. There is a bug in HTC desire, which was fixed only in the recent firmware update. I have posted quite some hints about it to your StackOverFlow question. Please read through and let me know, if you need some more info.
Thanks,
Radek