Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linux Bluetooth Support #7

Open
gunaNeelamegam opened this issue May 5, 2024 · 1 comment
Open

Linux Bluetooth Support #7

gunaNeelamegam opened this issue May 5, 2024 · 1 comment

Comments

@gunaNeelamegam
Copy link
Owner

gunaNeelamegam commented May 5, 2024

Bluetooth BLE Support

  • Need's to Advertisment Custom Service's.
  • Connect and Disconnect.
  • Bluetooth characteristic properties.
    • Read
    • Write
    • Notification
    • Indication
@gunaNeelamegam
Copy link
Owner Author

import asyncio
from bluez_peripheral.util import *
from bluez_peripheral.advert import Advertisement
from bluez_peripheral.agent import NoIoAgent
from bluez_peripheral.gatt.service import Service
from bluez_peripheral.gatt.characteristic import characteristic, CharacteristicFlags as CharFlags
from bluez_peripheral.advert import PacketType
from bluez_peripheral.gatt.descriptor import descriptor,DescriptorFlags
from random import randbytes,randint

NAVIGATION_LOCATION_SERVICE = "1234"
CURRENT_COORINATE_CHAR = "0001"
TEXT_LOCATION_CHAR = "0002"
CURRENT_DESC = "0003"
DOWNLOAD_ROUTE = "0004"

class LocationService(Service):

    def __init__(self):
        super().__init__(NAVIGATION_LOCATION_SERVICE, True)


    @characteristic(CURRENT_COORINATE_CHAR, CharFlags.WRITE)
    def text_navigation_characteristic(self, options):
        pass

    @text_navigation_characteristic.setter
    def text_navigation_read(self, data, options):
        print("inside read function")
        print("Data :", data)

    @characteristic(TEXT_LOCATION_CHAR, CharFlags.NOTIFY)
    def current_coordinate(self, options):
        print("triggered notify")

    @characteristic(DOWNLOAD_ROUTE, CharFlags.READ | CharFlags.WRITE)
    def send_coordinate(self, options):
        print("reading characteristics")
        return b"hello from charactertics"

    @send_coordinate.setter
    def download_map_on_message(self, data, options):
        print("inside on_message map download response")
        print(data)

    def update_location_coordinate(self, count):
        self.current_coordinate.changed(randbytes(count))

    @descriptor(CURRENT_DESC, text_navigation_characteristic, DescriptorFlags.READ | DescriptorFlags.WRITE)
    def get_refresh_btn(*args,**kwargs) :
        return b"current_cooridnate"

    @get_refresh_btn.setter
    def refresh_button_pressed(self, data , descriptor_options):
        if data := data.decode():
            print(data)



"""
when creating
"""
async def main():
    bus = await get_message_bus()
    service = LocationService()

    await service.register(bus)
    agent = NoIoAgent()
    await agent.register(bus)
    adapter = await Adapter.get_first(bus)
    advert = Advertisement("Android Prober",[NAVIGATION_LOCATION_SERVICE],0,0, packet_type= PacketType.PERIPHERAL)
    await advert.register(bus, adapter)

    try:
        while True:
            value = randint(0, 1000)
            service.update_location_coordinate(value % 514)
            await asyncio.sleep(5)

    except KeyboardInterrupt as e:
        await bus.wait_for_disconnect()


if __name__ == "__main__":
    asyncio.run(main())
    
    ```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant