File: passive_disconnect.py

package info (click to toggle)
pygattlib 0~20210616-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 468 kB
  • sloc: ansic: 5,031; cpp: 1,684; python: 386; makefile: 46; sh: 1
file content (46 lines) | stat: -rwxr-xr-x 1,276 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/python3
# -*- mode: python; coding: utf-8 -*-

# Copyright (C) 2014, Oscar Acena <oscaracena@gmail.com>
# This software is under the terms of Apache License v2 or later.

from __future__ import print_function

import sys
import time
from gattlib import GATTRequester


class PassiveDisconnect(object):
    def __init__(self, address):
        self.requester = GATTRequester(address, False)

        self.connect()
        self.wait_disconnection()

    def connect(self):
        print("Connecting...", end=' ')
        sys.stdout.flush()

        self.requester.connect(True)
        print("OK!")

    def wait_disconnection(self):
        status = "connected" if self.requester.is_connected() else "not connected"
        print("Checking current status: {}".format(status))
        print("\nNow, force a hardware disconnect. To do so, please switch off,\n"
              "reboot or move away your device. Don't worry, I'll wait...")

        while self.requester.is_connected():
            time.sleep(1)

        print("\nOK. Current state is disconnected. Congratulations ;)")


if __name__ == '__main__':
    if len(sys.argv) < 2:
        print("Usage: {} <addr>".format(sys.argv[0]))
        sys.exit(1)

    PassiveDisconnect(sys.argv[1])
    print("Done.")