Advertisement
Guest User

bluetooth_gabriel_smr

a guest
May 25th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.59 KB | None | 0 0
  1. [IMPORTANTE]:
  2. EDITAR EL ARCHIVO: /etc/systemd/system/dbus-org.bluez.service
  3. En la linea del ExecStart=/usr/lib/bluetooth/bluetoothd
  4. Cambiarlo por -> ExecStart=/usr/lib/bluetooth/bluetoothd -C
  5. Y reiniciar el servicio:
  6.     sudo systemd-daemon
  7.     sudo service bluetooth restart 
  8.  
  9. Código servidor bluetooth:
  10.  
  11. #!/usr/bin/env python
  12.  
  13. import os
  14. import glob
  15. import time
  16. import random
  17.  
  18. from bluetooth import *
  19.  
  20. server_sock = BluetoothSocket( RFCOMM )
  21. server_sock.bind(("",PORT_ANY))
  22. server_sock.listen(1)
  23.  
  24. port = server_sock.getsockname()[1]
  25.  
  26. uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee"
  27.  
  28. advertise_service( server_sock, "TestServer",
  29.                    service_id = uuid,
  30.                    service_classes = [ uuid, SERIAL_PORT_CLASS ],
  31.                    profiles = [ SERIAL_PORT_PROFILE ],
  32. #                   protocols = [ OBEX_UUID ]
  33.                     )
  34.  
  35. print "Esperando conexiones en el puerto %d" % port
  36. client_sock, client_info = server_sock.accept()
  37. print "Conexión entrante: ", client_info
  38.  
  39. while True:
  40.  
  41.     try:
  42.         req = client_sock.recv(1024)
  43.         if len(req) == 0:
  44.             break
  45.         print "received [%s]" % req
  46.  
  47.         data = None
  48.         if req in ('temp', '*temp'):
  49.             data = str(random.random())+'!'
  50.         else:
  51.             pass
  52.  
  53.         if data:
  54.             print "sending [%s]" % data
  55.             client_sock.send(data)
  56.  
  57.     except IOError:
  58.         pass
  59.  
  60.     except KeyboardInterrupt:
  61.  
  62.         print "Desconectado"
  63.  
  64.         client_sock.close()
  65.         server_sock.close()
  66.         print "Sockets cerrados"
  67.  
  68.         break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement