From db29fcb24521c4ab1c2bab3e7ffb512afdb74978 Mon Sep 17 00:00:00 2001 From: Dmitry Moiseev Date: Fri, 13 Dec 2013 15:38:02 +0600 Subject: Added lightblue support in mac os x --- ParrotZik.py | 22 ++++++++++--- ParrotZikMac.py | 98 --------------------------------------------------------- 2 files changed, 17 insertions(+), 103 deletions(-) delete mode 100644 ParrotZikMac.py diff --git a/ParrotZik.py b/ParrotZik.py index 73df83b..4510e27 100644 --- a/ParrotZik.py +++ b/ParrotZik.py @@ -1,6 +1,11 @@ #!/usr/bin/env python -import bluetooth +import sys +if sys.platform == "darwin": + import lightblue +else: + import bluetooth + import ParrotProtocol import struct from BeautifulSoup import BeautifulSoup @@ -9,7 +14,11 @@ class ParrotZik(object): def __init__(self,addr=None): uuid = "0ef0f502-f0ee-46c9-986c-54ed027807fb" - service_matches = bluetooth.find_service( uuid = uuid, address = addr ) + + if sys.platform == "darwin": + service_matches = lightblue.findservices( name = "RFCOMM", addr = addr ) + else: + service_matches = bluetooth.find_service( uuid = uuid, address = addr ) if len(service_matches) == 0: print "Failed to find Parrot Zik RFCOMM service" @@ -23,7 +32,11 @@ class ParrotZik(object): print "Connecting to \"%s\" on %s" % (name, host) - self.sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM ) + if sys.platform == "darwin": + self.sock=lightblue.lightblueSocket( lightblue.RFCOMM ) + else: + self.sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM ) + self.sock.connect((host, port)) self.sock.send('\x00\x03\x00') @@ -115,10 +128,9 @@ class ParrotZik(object): self.sock ="" return data = self.sock.recv(7) - len = struct.unpack('B', data[1])[0] data = self.sock.recv(1024) data=BeautifulSoup(data) return data def Close(self): - self.sock.close() + self.sock.close() \ No newline at end of file diff --git a/ParrotZikMac.py b/ParrotZikMac.py deleted file mode 100644 index a4b536c..0000000 --- a/ParrotZikMac.py +++ /dev/null @@ -1,98 +0,0 @@ -#!/usr/bin/env python - -import lightblue -import ParrotProtocol -import struct -from BeautifulSoup import BeautifulSoup - -class ParrotZik(object): - def __init__(self,addr=None): - uuid = "0ef0f502-f0ee-46c9-986c-54ed027807fb" - - service_matches = lightblue.findservices( name = "RFCOMM", addr = addr ) - - if len(service_matches) == 0: - print "Couldn't find Parrot Zik" - return - - 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) - - self.sock=lightblue.lightblueSocket( lightblue.RFCOMM ) - self.sock.connect((host, port)) - - self.sock.send('\x00\x03\x00') - data = self.sock.recv(3) - - self.BatteryLevel = 100 - print "Connected" - - def getBatteryState(self): - data = self.sendGetMessage("/api/system/battery/get") - return data.answer.system.battery["state"] - - def getBatteryLevel(self): - data = self.sendGetMessage("/api/system/battery/get") - try: - if data.answer.system.battery["level"] <> '': - self.BatteryLevel = data.answer.system.battery["level"] - except: - pass - - try: - print "notification received" + data.notify["path"] - except: - pass - - return self.BatteryLevel - - def getVersion(self): - data = self.sendGetMessage("/api/software/version/get") - return data.answer.software["version"] - - def getFriendlyName(self): - data = self.sendGetMessage("/api/lightblue/friendlyname/get") - return data.answer.lightblue["friendlyname"] - - def getAutoConnection(self): - data = self.sendGetMessage("/api/system/auto_connection/enabled/get") - return data.answer.system.auto_connection["enabled"] - - def setAutoConnection(self,arg): - data = self.sendSetMessage("/api/system/auto_connection/enabled/set",arg) - return data - - def getAncPhoneMode(self): - data = self.sendGetMessage("/api/system/anc_phone_mode/enabled/get") - return data.answer.system.anc_phone_mode["enabled"] - - def getNoiseCancel(self): - data = self.sendGetMessage("/api/audio/noise_cancellation/enabled/get") - return data.answer.audio.noise_cancellation["enabled"] - - def setNoiseCancel(self,arg): - data = self.sendSetMessage("/api/audio/noise_cancellation/enabled/set",arg) - return data - - def sendGetMessage(self,message): - message = ParrotProtocol.getRequest(message) - return self.sendMessage(message) - - def sendSetMessage(self,message,arg): - message = ParrotProtocol.setRequest(message,arg) - return self.sendMessage(message) - - def sendMessage(self,message): - self.sock.send(str(message)) - data = self.sock.recv(7) - len = struct.unpack('B', data[1])[0] - data = self.sock.recv(1024) - data=BeautifulSoup(data) - return data - - def Close(self): - self.sock.close() -- cgit v1.2.1