Skip to content

Commit dea3107

Browse files
author
Uros Petrevski
committed
Added support for bluetooth devices
1 parent 2cbb45d commit dea3107

File tree

7 files changed

+130
-0
lines changed

7 files changed

+130
-0
lines changed

examples/bluetooth/__init__.py

Whitespace-only changes.

examples/bluetooth/btDiscovery/__init__.py

Whitespace-only changes.
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
##########################################
2+
# #
3+
# Discovering devices with Bluetooth #
4+
# #
5+
##########################################
6+
# to run this example you need Bluetooth usb dongle
7+
8+
from weioLib.weio import *
9+
import bluetooth
10+
from subprocess import Popen, PIPE
11+
12+
def setup():
13+
14+
# Reset BT and make sure that BT is UP
15+
execute("hciconfig hci0 down")
16+
execute("hciconfig hci0 up")
17+
18+
result = execute("hciconfig")
19+
print result
20+
if ("UP RUNNING" in result):
21+
attach.process(myProcess)
22+
23+
24+
def myProcess():
25+
print("performing inquiry...")
26+
nearby_devices = bluetooth.discover_devices(duration=2, lookup_names=True)
27+
print("found %d devices" % len(nearby_devices))
28+
for addr, name in nearby_devices:
29+
print(" %s - %s" % (addr, name))
30+
31+
32+
def execute(cmd): # cmd is string like in command line
33+
proc = Popen(cmd.split(" "), stdout=PIPE, stderr=PIPE)
34+
s = proc.communicate()
35+
# return console output
36+
return s[0]
37+
38+
39+
# Will be called on stop button
40+
def stop():
41+
# Stopping BT
42+
execute("hciconfig hci0 down")

examples/bluetooth/btServiceDiscovery/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from weioLib.weio import *
2+
import bluetooth
3+
from subprocess import Popen, PIPE
4+
5+
def setup():
6+
7+
# Reset BT and make sure that BT is UP
8+
execute("hciconfig hci0 down")
9+
execute("hciconfig hci0 up")
10+
result = execute("hciconfig")
11+
print result
12+
if ("UP RUNNING" in result):
13+
attach.process(myProcess)
14+
15+
def myProcess():
16+
print("Please wait while searching for BT services")
17+
18+
# address None means to search all
19+
services = bluetooth.find_service(address=None)
20+
21+
if len(services) > 0:
22+
print("Found " + str(len(services)) + " services")
23+
else:
24+
print("no services found")
25+
26+
for svc in services:
27+
print("Service Name: %s" % svc["name"])
28+
print(" Host: %s" % svc["host"])
29+
print(" Description: %s" % svc["description"])
30+
print(" Provided By: %s" % svc["provider"])
31+
print(" Protocol: %s" % svc["protocol"])
32+
print(" channel/PSM: %s" % svc["port"])
33+
print(" svc classes: %s "% svc["service-classes"])
34+
print(" profiles: %s "% svc["profiles"])
35+
print(" service id: %s "% svc["service-id"])
36+
print()
37+
38+
# Will be called on stop button
39+
def stop():
40+
# Stopping BT
41+
execute("hciconfig hci0 down")
42+
43+
def execute(cmd): # cmd is string like in command line
44+
proc = Popen(cmd.split(" "), stdout=PIPE, stderr=PIPE)
45+
s = proc.communicate()
46+
# return console output
47+
return s[0]
48+

examples/bluetooth/iBeacon/__init__.py

Whitespace-only changes.

examples/bluetooth/iBeacon/main.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
##########################################
2+
# #
3+
# Turning your WeIO into iBeacon #
4+
# #
5+
##########################################
6+
# to run this example you need Bluetooth usb dongle
7+
# this one is proven to work :
8+
# http://www.amazon.com/IOGEAR-Bluetooth-Micro-Adapter-GBU521/dp/B007GFX0PY
9+
10+
from weioLib.weio import *
11+
import bluetooth
12+
from subprocess import Popen, PIPE
13+
14+
# This is your unique ID address
15+
ID = "E2 0A 39 F4 73 F5 4B C4 A1 2F 17 D1 AD 07 A9 61"
16+
17+
def setup():
18+
19+
# Reset BT and make sure that BT is UP
20+
execute("hciconfig hci0 down")
21+
execute("hciconfig hci0 up")
22+
23+
print execute("hciconfig hci0 leadv 3")
24+
print execute("hciconfig hci0 noscan")
25+
26+
result = execute("hciconfig")
27+
print result
28+
if ("UP RUNNING" in result):
29+
print execute("hcitool -i hci0 cmd 0x08 0x0008 1E 02 01 1A 1A FF 4C 00 02 15 "+ID+" 00 00 00 00 C8 00")
30+
31+
32+
def execute(cmd): # cmd is string like in command line
33+
proc = Popen(cmd.split(" "), stdout=PIPE, stderr=PIPE)
34+
s = proc.communicate()
35+
# return console output
36+
return s[0]
37+
38+
def stop():
39+
# stop bluetooth
40+
execute("hciconfig hci0 down")

0 commit comments

Comments
 (0)