aboutsummaryrefslogtreecommitdiffstats
path: root/bluetooth.py
blob: caef235267fa43b97b998579ca3fa57bd18bfaa4 (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
66
67
from aglbaseservice import AGLBaseService
import asyncio
import os

Verbs = ['subscribe', 'unsubscribe', 'managed_objects', 'adapter_state', 'default_adapter', 'avrcp_controls',
         'connect', 'disconnect', 'pair', 'cancel_pairing', 'confirm_pairing', 'remove_device']
AdapterStateParams = ['discovery', 'discoverable', 'powered', ]
BTEventType = ['adapter_changes', 'device_changes', 'media', 'agent']


class BluetoothService(AGLBaseService):

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

    async def subscribe(self, event='device_changes', waitresponse=False):
        await super().subscribe(event=event)

    async def unsubscribe(self, event='device_changes', waitresponse=False):
        await super().unsubscribe(event=event)

    async def managed_objects(self, waitresponse=False):
        return await self.request('managed_objects', waitresponse=waitresponse)

    async def adapter_state(self, adapter=None, value=None, waitresponse=False):
        p = {}
        if adapter:
            p = {'adapter': adapter}
            if isinstance(value, dict):
                p = {**p, **value}

        return await self.request('adapter_state', p, waitresponse=waitresponse)

    async def default_adapter(self, waitresponse=False):
        return await self.request('default_adapter', "", waitresponse=waitresponse)

    async def connect(self, device: str = 'hci0', waitresponse=False):
        return await self.request('connect', {'device': device}, waitresponse=waitresponse)

    async def disconnect(self, device: str = 'hci0', waitresponse=False):
        return await self.request('disconnect', {'device': device}, waitresponse=waitresponse)

    async def pair(self, device, waitresponse=False):
        return await self.request('pair', {'device': device}, waitresponse=waitresponse)

    async def cancel_pairing(self, waitresponse=False):
        return await self.request('cancel_pairing', waitresponse=waitresponse)

    async def confirm_pairing(self, pincode, waitresponse=False):
        return await self.request('confirm_pairing', {'pincode': pincode}, waitresponse=waitresponse)

    async def avrcp_controls(self):
        pass

async def main(loop):
    addr = os.environ.get('AGL_TGT_IP', 'localhost')
    port = os.environ.get('AGL_TGT_PORT', None)
    BTS = await BluetoothService(ip=addr, service='agl-service-bluetooth', port=port)
    listener = loop.create_task(BTS.listener())
    await BTS.subscribe()
    BTS.logger.debug(await BTS.adapter_state('hci0', {'uuids': ['0000110e-0000-1000-8000-00805f9b34fb']}))
    await listener

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