aboutsummaryrefslogtreecommitdiff
path: root/bluetooth_paired_devices.py
diff options
context:
space:
mode:
authorMarek Siarkowicz <mareksiarkowicz@gmail.com>2015-06-15 01:10:40 +0200
committerMarek Siarkowicz <mareksiarkowicz@gmail.com>2015-06-15 01:20:11 +0200
commitffa76dd958282735c1b39793351a075a3d1a64ac (patch)
tree99593c6c6e09a6228c4a7172f773779e77f7e6f3 /bluetooth_paired_devices.py
parent21cf625f6970c001c9f14bdbc603a104e544b9e1 (diff)
downloadpyParrotZikTCP-ffa76dd958282735c1b39793351a075a3d1a64ac.tar.xz
pyParrotZikTCP-ffa76dd958282735c1b39793351a075a3d1a64ac.zip
Refactor.
Diffstat (limited to '')
-rw-r--r--bluetooth_paired_devices.py (renamed from BluetoothPairedDevices.py)53
1 files changed, 50 insertions, 3 deletions
diff --git a/BluetoothPairedDevices.py b/bluetooth_paired_devices.py
index aa5b056..139ca3d 100644
--- a/BluetoothPairedDevices.py
+++ b/bluetooth_paired_devices.py
@@ -2,13 +2,18 @@ import sys
import re
import os
+from resource_manager import GenericResourceManager
+
if sys.platform == "darwin":
from binplist import binplist
-elif sys.platform == "win32":
- import _winreg
+ import lightblue
+else:
+ import bluetooth
+ if sys.platform == "win32":
+ import _winreg
-def ParrotZikMac():
+def get_parrot_zik_mac():
p = re.compile('90:03:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}|'
'A0:14:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}')
if sys.platform == "linux2":
@@ -43,3 +48,45 @@ def ParrotZikMac():
except EnvironmentError:
pass
+
+
+def connect(addr=None):
+ if sys.platform == "darwin":
+ service_matches = lightblue.findservices(
+ name="Parrot RFcomm service", addr=addr)
+ else:
+ uuids = ["0ef0f502-f0ee-46c9-986c-54ed027807fb",
+ "8B6814D3-6CE7-4498-9700-9312C1711F63"]
+ service_matches = []
+ for uuid in uuids:
+ service_matches = bluetooth.find_service(uuid=uuid, address=addr)
+ if service_matches:
+ break
+
+ if len(service_matches) == 0:
+ print "Failed to find Parrot Zik RFCOMM service"
+ return GenericResourceManager(None)
+
+ if sys.platform == "darwin":
+ first_match = service_matches[0]
+ port = first_match[1]
+ name = first_match[2]
+ host = first_match[0]
+ else:
+ first_match = service_matches[0]
+ port = first_match["port"]
+ name = first_match["name"]
+ host = first_match["host"]
+
+ print "Connecting to \"%s\" on %s" % (name, host)
+
+ if sys.platform == "darwin":
+ sock = lightblue.socket()
+ else:
+ sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
+
+ sock.connect((host, port))
+
+ sock.send('\x00\x03\x00')
+ sock.recv(1024)
+ return GenericResourceManager(sock)