kivy / oscpy

An efficient OSC implementation compatible with python2.7 and 3.5+
MIT License
109 stars 27 forks source link

Activity and Service don't respond to messages from one another on Android #58

Open olek5andr opened 3 years ago

olek5andr commented 3 years ago

The code works on mac, but on an Android device it seems that the service and main app don't exchange (respond to) messages from one another. Because if I don't send messages between them and just start service and push notification on a time interval it works. Below is my code.

main.py:

from oscpy.client import OSCClient
from oscpy.server import OSCThreadServer

SERVICE_NAME = '{packagename}.Service{servicename}'.format(
    packagename='clientsandorders.com.clientsandorders',
    servicename='Taskreminder'
)

class MainApp(MDApp):
    def build(self):
        kv = Builder.load_file("main.kv")

        self.start_service()

        self.server = server = OSCThreadServer()
        server.listen(
            address=b'localhost',
            port=3002,
            default=True,
        )

        server.bind(b'/ping_response', self.ping_response)
        self.client = OSCClient(address=b'localhost', port=3000)

        return kv

        def ping_response(self):
             self.client.send_message(b'/push_up', [])

And here's the service code:

from time import sleep
from oscpy.server import OSCThreadServer
from oscpy.client import OSCClient
from plyer import notification
from plyer.utils import platform
from jnius import autoclass

CLIENT = OSCClient(address='localhost', port=3002)

def push_up():
    if platform == 'android':
        notification.notify(title='Test', message='This is a test message')
    else:
        print('Service works')

def ping_activity():
    CLIENT.send_message(b'/ping_response', [])

if __name__ == '__main__':
    SERVER = OSCThreadServer()
    SERVER.listen(address='localhost', port=3000, default=True)
    SERVER.bind(b'/push_up', push_up)
    PythonService = autoclass('org.kivy.android.PythonService')
    PythonService.mService.setAutoRestartService(True)

    while True:
        sleep(10)
        ping_activity()

Expected behaviour is for service to ping the main app on a specific time interval, then get a response (with some data in the future) and to push a notification with plyer.

logcat shows no errors.

Many thanks in advance!

tshirtman commented 3 years ago

Sorry for not seeing this earlier, did you succeed? if not, did you try adding the INTERNET permission to your app in buildozer.spec or through p4a command line options?

olek5andr commented 3 years ago

Sorry for not seeing this earlier, did you succeed? if not, did you try adding the INTERNET permission to your app in buildozer.spec or through p4a command line options?

Yeah, I had the INTERNET permission in my buildozer.spec. I've kinda found a workaround for my project, I just launch the service from the main app, but don't exchange any data between them