summaryrefslogtreecommitdiffstats
path: root/bluetooth.py
blob: 13ab0c9e33aee49fac011d78ed326e3c51a7ec72 (plain)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import asyncio
from random import randint
from aglbaseservice import AGLBaseService

Verbs = ['subscribe', 'unsubscribe', 'managed_objects', 'adapter_state', 'default_adapter', 'avrcp_controls',
         'connect', 'disconnect', 'pair', 'cancel_pairing', 'confirm_pairing', 'remove_device']

BTEventType = ['adapter_changes', 'device_changes', 'media', 'agent']

class BluetoothService(AGLBaseService):

    def __init__(self, ip, port):
        super().__init__(api='Bluetooth-Manager', ip=ip, port=port)

    async def subscribe(self, event='location'):
        super().subscribe(event=event)

    async def unsubscribe(self, event='location'):
        super().unsubscribe(event=event)

    async def managed_objects(self):
        verb = 'managed_objects'
        msgid = randint(0, 999999)
        msg = f'[2,"{msgid}","{self.api}/{verb}",""]'
        print(msg)
        await self.send(msg)
        return await self.receive()

    async def adapter_state(self, param = None, value = None):
        verb = 'adapter_state'
        msgid = randint(0, 999999)
        if param:
            p = str({'adapter': param})
            # msg = f'[2,"{msgid}","{self.api}/{verb}","{param}": {value if value is not None else ""}]'
            msg = f'[2,"{msgid}","{self.api}/{verb}", {p}]'
        else:
            msg = f'[2,"{msgid}","{self.api}/{verb}", ""]'

        print(msg)
        await self.send(msg)
        return await self.receive()

    async def default_adapter(self):
        verb = 'default_adapter'
        msgid = randint(0, 999999)
        msg = f'[2,"{msgid}","{self.api}/{verb}",""]'
        print(msg)
        await self.send(msg)
        return await self.receive()

    async def avrcp_controls(self):
        pass


async def main(loop):
    BTS = await BluetoothService(ip='192.168.234.202', port='30005')
    #print(await BTS.managed_objects())
    print(await BTS.adapter_state('hci0', '{"discoverable": true}'))
    await asyncio.sleep(delay=1)
    print(await BTS.adapter_state('hci0'))
    print(await BTS.adapter_state('hci1', '""'))

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main(loop))