1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
# -*- coding: utf-8 -*-
# Copyright (C) 2008-2009 Team XBMC http://www.xbmc.org
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
BLUEZ=0
try:
import bluetooth
BLUEZ=1
except:
try:
import lightblue
except:
print "ERROR: You need to have either LightBlue or PyBluez installed\n"\
" in order to use this program."
print "- PyBluez (Linux / Windows XP) http://org.csail.mit.edu/pybluez/"
print "- LightBlue (Mac OS X / Linux) http://lightblue.sourceforge.net/"
exit()
def bt_create_socket():
if BLUEZ:
sock = bluetooth.BluetoothSocket(bluetooth.L2CAP)
else:
sock = lightblue.socket(lightblue.L2CAP)
return sock
def bt_create_rfcomm_socket():
if BLUEZ:
sock = bluetooth.BluetoothSocket( bluetooth.RFCOMM )
sock.bind(("",bluetooth.PORT_ANY))
else:
sock = lightblue.socket(lightblue.RFCOMM)
sock.bind(("",0))
return sock
def bt_discover_devices():
if BLUEZ:
nearby = bluetooth.discover_devices()
else:
nearby = lightblue.finddevices()
return nearby
def bt_lookup_name(bdaddr):
if BLUEZ:
bname = bluetooth.lookup_name( bdaddr )
else:
bname = bdaddr[1]
return bname
def bt_lookup_addr(bdaddr):
if BLUEZ:
return bdaddr
else:
return bdaddr[0]
def bt_advertise(name, uuid, socket):
if BLUEZ:
bluetooth.advertise_service( socket, name,
service_id = uuid,
service_classes = [ uuid, bluetooth.SERIAL_PORT_CLASS ],
profiles = [ bluetooth.SERIAL_PORT_PROFILE ] )
else:
lightblue.advertise(name, socket, lightblue.RFCOMM)
def bt_stop_advertising(socket):
if BLUEZ:
stop_advertising(socket)
else:
lightblue.stopadvertise(socket)
|