Ejemplo n.º 1
0
 def __init__(self):
     self._level = 0
     super().__init__(text="Waiting for advertising to start...")
     self.advertiser = BatteryAdvertiser(
         ble=BluetoothDispatcher(),
         data=self.construct_data(level=100),
         interval=advertising.Interval.MIN
     )
     self.advertiser.bind(on_advertising_started=self.on_started)  # bind to start of advertising
     self.advertiser.start()
Ejemplo n.º 2
0
 def __init__(self):
     super(BLELink, self).__init__()
     BluetoothDispatcher.__init__(self)
     self.rx_fifo = Fifo()
     self.addr = ''
     self.device = None
     self.device_list = []
     self.scoot_found = False
     self.state = StringProperty()
     self.tx_characteristic = None
     self.rx_characteristic = None
     self.keys_characteristic = None
     self.iotimeout = 2
     self.timeout = SCAN_TIMEOUT
     self.dump = True
     self.keys = None
     self.scanned = Event()
     self.keys_recovered = Event()
     self.connected = Event()
Ejemplo n.º 3
0
def main():
    uuid = environ.get(
        "PYTHON_SERVICE_ARGUMENT",
        "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
    )
    advertiser = Advertiser(
        ble=BluetoothDispatcher(),
        data=AdvertiseData(ServiceUUID(uuid)),
    )
    advertiser.start()
    while True:
        time.sleep(0xDEAD)
Ejemplo n.º 4
0
class MainLayout(NavigationDrawer):
    ble = BluetoothDispatcher()
    app_name=StringProperty("")
    data_adv=StringProperty("")
    data_pressure=StringProperty("")
    data_batt=StringProperty("")
    dev=StringProperty("pressure")
    pressure_id=StringProperty("")
    throttle_id=StringProperty("")
    def __init__(self,*args,**kwargs):
        super(MainLayout,self).__init__(*args,**kwargs)
        self.ids["root_page"].add_widget(PressurePage())
        self.ble.bind(on_device=self.on_device)
        try:
            self.ble.start_scan()
        except:
            pass
    def on_device(self, ble, device, rssi, advertisement):
        for ad in advertisement:
            if ad.ad_type == Advertisement.ad_types.manufacturer_specific_data:
                self.data_adv=str(binascii.hexlify(ad.data).decode())
                self.data_pressure = str(int(self.data_adv[12:16], 16) / 1.2593856/2)
                if self.dev=="pressure":
                    try:
                        self.give_pressure_data(int(self.data_adv[12:16], 16) / 1.2593856/2-1.588,int(self.data_adv[8:12], 16)/51.33)
                    except:
                        pass
                if self.dev=="throttle":
                    try:
                        self.give_throttle_data(int(self.data_adv[8:12], 16),
                                                int(self.data_adv[12:16], 16),
                                                int(self.data_adv[16:20], 16),
                                                int(self.data_adv[20:24], 16),
                                                int(self.data_adv[24:28], 16))
                    except:
                        pass
    def open_qrscan(self):
        print("yaaaa")
    def give_pressure_data(self,data,data_volt):
        self.ids["root_page"].children[0].show_value(data,data_volt)
    def give_throttle_data(self,batt,cyl1,cyl2,cyl3,cyl4):
        self.ids["root_page"].children[0].show_value(batt,cyl1,cyl2,cyl3,cyl4)
Ejemplo n.º 5
0
class BLETestApp(App):
    ble = BluetoothDispatcher()
    state = StringProperty('')
    test_string = StringProperty('')
    notification_value = StringProperty('')
    counter_value = StringProperty('')
    increment_count_value = StringProperty('')
    incremental_interval = StringProperty('100')
    counter_max = StringProperty('128')
    counter_value = StringProperty('')
    counter_state = StringProperty('')
    counter_total_time = StringProperty('')
    queue_timeout_enabled = BooleanProperty(True)
    queue_timeout = StringProperty('1000')

    uids = {
        'string': '0d01',
        'counter_reset': '0d02',
        'counter_increment': '0d03',
        'counter_read': '0d04',
        'notifications': '0d05'
    }

    def build(self):
        return MainLayout()

    def on_pause(self):
        return True

    def on_resume(self):
        pass

    def init(self, *args, **kwargs):
        self.set_queue_settings()
        self.ble.bind(on_device=self.on_device)
        self.ble.bind(on_scan_started=self.on_scan_started)
        self.ble.bind(on_scan_completed=self.on_scan_completed)
        self.ble.bind(
            on_connection_state_change=self.on_connection_state_change)
        self.ble.bind(on_services=self.on_services)
        self.ble.bind(on_characteristic_read=self.on_characteristic_read)
        self.ble.bind(on_characteristic_changed=self.on_characteristic_changed)

    def start_scan(self, *args, **kwargs):
        if not self.state:
            self.init()
        self.state = 'scan_start'
        self.ble.start_scan()

    def on_scan_started(self, ble, success):
        self.state = 'scan' if success else 'scan_error'

    def on_device(self, ble, device, rssi, advertisement):
        if self.state != 'scan':
            return
        for ad in advertisement:
            if ad.ad_type == Advertisement.ad_types.complete_local_name:
                if str(ad.data) == 'KivyBLETest':
                    self.device = device
                    self.state = 'found'
                    self.ble.stop_scan()

    def on_scan_completed(self, ble):
        if self.device:
            self.ble.connect_gatt(self.device)

    def on_connection_state_change(self, ble, status, state):
        if status == GATT_SUCCESS:
            if state:
                self.ble.discover_services()
            else:
                self.state = 'disconnected'
        else:
            self.state = 'connection_error'

    def on_services(self, ble, status, services):
        if status != GATT_SUCCESS:
            self.state = 'services_error'
            return
        self.state = 'connected'
        self.services = services
        self.read_test_string(ble)
        self.characteristics = {
            'counter_increment':
            self.services.search(self.uids['counter_increment']),
            'counter_reset':
            self.services.search(self.uids['counter_reset']),
        }

    def read_test_string(self, ble):
        characteristic = self.services.search(self.uids['string'])
        if characteristic:
            ble.read_characteristic(characteristic)
        else:
            self.test_string = 'not found'

    def read_remote_counter(self, *args, **kwargs):
        characteristic = self.services.search(self.uids['counter_read'])
        if characteristic:
            self.ble.read_characteristic(characteristic)
        else:
            self.counter_value = 'error'

    def enable_notifications(self, enable):
        if enable:
            self.notification_value = '0'
        characteristic = self.services.search(self.uids['notifications'])
        if characteristic:
            self.ble.enable_notifications(characteristic, enable)
        else:
            self.notification_value = 'error'

    def enable_counter(self, enable):
        if enable:
            self.counter_state = 'init'
            interval = int(self.incremental_interval) * .001
            Clock.schedule_interval(self.counter_next, interval)
        else:
            Clock.unschedule(self.counter_next)
            if self.counter_state != 'stop':
                self.counter_state = 'stop'
                self.read_remote_counter()

    def counter_next(self, *args, **kwargs):
        if self.counter_state == 'init':
            self.counter_started_time = time.time()
            self.counter_total_time = ''
            self.reset_remote_counter()
            self.increment_remote_counter()
        elif self.counter_state == 'enabled':
            if int(self.increment_count_value) < int(self.counter_max):
                self.increment_remote_counter()
            else:
                self.enable_counter(False)

    def reset_remote_counter(self):
        self.increment_count_value = '0'
        self.counter_value = ''
        self.ble.write_characteristic(self.characteristics['counter_reset'],
                                      '1')
        self.counter_state = 'enabled'

    def on_characteristic_read(self, ble, characteristic, status):
        uuid = characteristic.getUuid().toString()
        if self.uids['string'] in uuid:
            self.update_string_value(characteristic, status)
        elif self.uids['counter_read'] in uuid:
            self.counter_total_time = str(time.time() -
                                          self.counter_started_time)
            self.update_counter_value(characteristic, status)

    def update_string_value(self, characteristic, status):
        result = 'ERROR'
        if status == GATT_SUCCESS:
            value = characteristic.getStringValue(0)
            if value == 'test':
                result = 'OK'
        self.test_string = result

    def increment_remote_counter(self):
        characteristic = self.characteristics['counter_increment']
        self.ble.write_characteristic(characteristic, '1')
        prev_value = int(self.increment_count_value)
        self.increment_count_value = str(prev_value + 1)

    def update_counter_value(self, characteristic, status):
        if status == GATT_SUCCESS:
            self.counter_value = characteristic.getStringValue(0)
        else:
            self.counter_value = 'ERROR'

    def set_queue_settings(self):
        self.ble.set_queue_timeout(None if not self.queue_timeout_enabled else
                                   int(self.queue_timeout) * .001)

    def on_characteristic_changed(self, ble, characteristic):
        uuid = characteristic.getUuid().toString()
        if self.uids['notifications'] in uuid:
            prev_value = self.notification_value
            value = int(characteristic.getStringValue(0))
            if (prev_value == 'error') or (value != int(prev_value) + 1):
                value = 'error'
            self.notification_value = str(value)
Ejemplo n.º 6
0
class BLETestApp(App):
    ble = BluetoothDispatcher()
    state = StringProperty('')
    test_string = StringProperty('')
    rssi = StringProperty('')
    notification_value = StringProperty('')
    counter_value = StringProperty('')
    increment_count_value = StringProperty('')
    incremental_interval = StringProperty('100')
    counter_max = StringProperty('128')
    counter_value = StringProperty('')
    counter_state = StringProperty('')
    counter_total_time = StringProperty('')
    queue_timeout_enabled = BooleanProperty(True)
    queue_timeout = StringProperty('1000')
    device_name = StringProperty('KivyBLETest')
    device_address = StringProperty('')

    store = JsonStore('bletestapp.json')

    uids = {
        'string': '0d01',
        'counter_reset': '0d02',
        'counter_increment': '0d03',
        'counter_read': '0d04',
        'notifications': '0d05'
    }

    def build(self):
        if self.store.exists('device'):
            self.device_address = self.store.get('device')['address']
        else:
            self.device_address = ''
        return MainLayout()

    def on_pause(self):
        return True

    def on_resume(self):
        pass

    def init(self):
        self.set_queue_settings()
        self.ble.bind(on_device=self.on_device)
        self.ble.bind(on_scan_started=self.on_scan_started)
        self.ble.bind(on_scan_completed=self.on_scan_completed)
        self.ble.bind(
            on_connection_state_change=self.on_connection_state_change)
        self.ble.bind(on_services=self.on_services)
        self.ble.bind(on_characteristic_read=self.on_characteristic_read)
        self.ble.bind(on_characteristic_changed=self.on_characteristic_changed)
        self.ble.bind(on_rssi_updated=self.on_rssi_updated)

    def start_scan(self):
        if not self.state:
            self.init()
        self.state = 'scan_start'
        self.ble.close_gatt()
        self.ble.start_scan()

    def connect_by_mac_address(self):
        self.store.put('device', address=self.device_address)
        if not self.state:
            self.init()
        self.state = 'try_connect'
        self.ble.close_gatt()
        try:
            self.ble.connect_by_device_address(self.device_address)
        except ValueError as exc:
            self.state = str(exc)

    def on_scan_started(self, ble, success):
        self.state = 'scan' if success else 'scan_error'

    def on_device(self, ble, device, rssi, advertisement):
        if self.state != 'scan':
            return
        if device.getName() == self.device_name:
            self.device = device
            self.state = 'found'
            self.ble.stop_scan()

    def on_scan_completed(self, ble):
        if self.device:
            self.ble.connect_gatt(self.device)

    def on_connection_state_change(self, ble, status, state):
        if status == GATT_SUCCESS:
            if state:
                self.ble.discover_services()
            else:
                self.state = 'disconnected'
        else:
            self.state = 'connection_error'

    def on_services(self, ble, status, services):
        if status != GATT_SUCCESS:
            self.state = 'services_error'
            return
        self.state = 'connected'
        self.services = services
        self.read_test_string(ble)
        self.characteristics = {
            'counter_increment':
            self.services.search(self.uids['counter_increment']),
            'counter_reset':
            self.services.search(self.uids['counter_reset']),
        }

    def read_rssi(self):
        self.rssi = '...'
        result = self.ble.update_rssi()

    def on_rssi_updated(self, ble, rssi, status):
        self.rssi = str(
            rssi) if status == GATT_SUCCESS else f"Bad status: {status}"

    def read_test_string(self, ble):
        characteristic = self.services.search(self.uids['string'])
        if characteristic:
            ble.read_characteristic(characteristic)
        else:
            self.test_string = 'not found'

    def read_remote_counter(self):
        characteristic = self.services.search(self.uids['counter_read'])
        if characteristic:
            self.ble.read_characteristic(characteristic)
        else:
            self.counter_value = 'error'

    def enable_notifications(self, enable):
        if enable:
            self.notification_value = '0'
        characteristic = self.services.search(self.uids['notifications'])
        if characteristic:
            self.ble.enable_notifications(characteristic, enable)
        else:
            self.notification_value = 'error'

    def enable_counter(self, enable):
        if enable:
            self.counter_state = 'init'
            interval = int(self.incremental_interval) * .001
            Clock.schedule_interval(self.counter_next, interval)
        else:
            Clock.unschedule(self.counter_next)
            if self.counter_state != 'stop':
                self.counter_state = 'stop'
                self.read_remote_counter()

    def counter_next(self, dt):
        if self.counter_state == 'init':
            self.counter_started_time = time.time()
            self.counter_total_time = ''
            self.reset_remote_counter()
            self.increment_remote_counter()
        elif self.counter_state == 'enabled':
            if int(self.increment_count_value) < int(self.counter_max):
                self.increment_remote_counter()
            else:
                self.enable_counter(False)

    def reset_remote_counter(self):
        self.increment_count_value = '0'
        self.counter_value = ''
        self.ble.write_characteristic(self.characteristics['counter_reset'],
                                      [])
        self.counter_state = 'enabled'

    def on_characteristic_read(self, ble, characteristic, status):
        uuid = characteristic.getUuid().toString()
        if self.uids['string'] in uuid:
            self.update_string_value(characteristic, status)
        elif self.uids['counter_read'] in uuid:
            self.counter_total_time = str(time.time() -
                                          self.counter_started_time)
            self.update_counter_value(characteristic, status)

    def update_string_value(self, characteristic, status):
        result = 'ERROR'
        if status == GATT_SUCCESS:
            value = characteristic.getStringValue(0)
            if value == 'test':
                result = 'OK'
        self.test_string = result

    def increment_remote_counter(self):
        characteristic = self.characteristics['counter_increment']
        self.ble.write_characteristic(characteristic, [])
        prev_value = int(self.increment_count_value)
        self.increment_count_value = str(prev_value + 1)

    def update_counter_value(self, characteristic, status):
        if status == GATT_SUCCESS:
            self.counter_value = characteristic.getStringValue(0)
        else:
            self.counter_value = 'ERROR'

    def set_queue_settings(self):
        self.ble.set_queue_timeout(None if not self.queue_timeout_enabled else
                                   int(self.queue_timeout) * .001)

    def on_characteristic_changed(self, ble, characteristic):
        uuid = characteristic.getUuid().toString()
        if self.uids['notifications'] in uuid:
            prev_value = self.notification_value
            value = int(characteristic.getStringValue(0))
            if (prev_value == 'error') or (value != int(prev_value) + 1):
                value = 'error'
            self.notification_value = str(value)