Skip to content

Commit

Permalink
discover primary services
Browse files Browse the repository at this point in the history
  • Loading branch information
andete committed Feb 25, 2014
1 parent 27add30 commit c20a1c0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 27 deletions.
30 changes: 25 additions & 5 deletions ble.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
import serial
import serial, time

import bglib
from PySide import QtCore

class ActivityThread(QtCore.QThread):

def __init__(self, ble, parent=None):
super(ActivityThread, self).__init__(parent)
self._stop = False
self.ble = ble

def run(self):
while not self._stop:
self.ble.check_activity()
time.sleep(0.01)

def stop(self):
self._stop = True


class BLE(QtCore.QObject):
scan_response = QtCore.Signal(dict)
connection_status = QtCore.Signal(int, str, int)
timeout = QtCore.Signal()
service_result = QtCore.Signal(int, list, int, int)

CONNECTED = 0

Expand Down Expand Up @@ -84,8 +101,10 @@ def handle_connection_status(self, sender, args):
self.connection_status.emit(h, f, self.CONNECTED)

def handle_attclient_group_found(self, sender, args):
uuid = reversed(args['uuid'])
print uuid
uuid = ''.join(["%02X" % c for c in reversed(args['uuid'])])
print "Found attribute group for service: %s start=%d, end=%d" % (uuid, args['start'], args['end'])
handle = args['connection']
self.service_result.emit(handle, uuid, args['start'], args['end'])

def check_activity(self):
return self.ble.check_activity(self.ser)
Expand All @@ -94,7 +113,7 @@ def connect_direct(self, target):
print "connecting to", target
address = target
addr_type = 0 # public
timeout = 10 # 1 sec
timeout = 30 # 3 sec
slave_latency = 0 # disabled
conn_interval_min = 100/1.25 # in ms
conn_interval_max = 1000/1.25 # in ms
Expand All @@ -103,5 +122,6 @@ def connect_direct(self, target):
conn_interval_max, timeout, slave_latency))

def primary_service_discovery(self, handle):
self.send_command(self.ble.ble_cmd_attclient_read_by_group_type(handle, 0x0001, 0xFFFF, list(reversed(BLE.uuid_service))))
print "service discovery for %d ..." % handle
self.send_command(self.ble.ble_cmd_attclient_read_by_group_type(handle, 0x0001, 0xFFFF, list(reversed(BLE.uuid_primary))))

19 changes: 2 additions & 17 deletions collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,7 @@
from PySide.QtCore import Qt

from productize import parse_data
from ble import BLE

class CollectThread(QtCore.QThread):

def __init__(self, ble, parent=None):
super(CollectThread, self).__init__(parent)
self._stop = False
self.ble = ble

def run(self):
while not self._stop:
self.ble.check_activity()
time.sleep(0.01)

def stop(self):
self._stop = True
from ble import BLE, ActivityThread

def print_scan_response(args):
print "gap_scan_response",
Expand Down Expand Up @@ -54,7 +39,7 @@ def run():
baud_rate = 115200

ble = BLE(port_name, baud_rate)
ct = CollectThread(ble)
ct = ActivityThread(ble)
ble.scan_response.connect(print_scan_response)
ct.start()
return app.exec_()
Expand Down
9 changes: 4 additions & 5 deletions gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
from PySide import QtGui, QtCore
from PySide.QtCore import Qt

from collect import CollectThread
from productize import parse_data
from ble import BLE
from ble import BLE, ActivityThread

class MainWin(QtGui.QMainWindow):

Expand Down Expand Up @@ -52,7 +51,7 @@ def about(self):
QtGui.QMessageBox.about(self, "about BTLE tool", a)

def close(self):
self.collect_thread.stop()
self.activity_thread.stop()
time.sleep(0.1)
QtGui.qApp.quit()

Expand Down Expand Up @@ -88,10 +87,10 @@ def row_changed(self, current, previous):
self.selected_device_raw = self.collect_model.item(current.row(), 1).data()

def run_collection(self):
self.collect_thread = CollectThread(self.ble)
self.activity_thread = ActivityThread(self.ble)
self.ble.scan_response.connect(self.scan_response)
self.ble.connection_status.connect(self.connection_status)
self.collect_thread.start()
self.activity_thread.start()

def scan_response(self, args):
s = QtGui.QStandardItem
Expand Down

0 comments on commit c20a1c0

Please sign in to comment.