Search code examples
androidpythonnetwork-programmingkivyosc

Kivy and OSC on Android


I made a simple OSC test on kivy, and it looks like I'm having problems with running them on my Android phone. To better understand how Kivy and OSC work, I made a little test, which should show communication between the app and another computer. Tipically a server

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.gridlayout import *
from kivy.uix.button import Button
from kivy.clock import Clock
from kivy.lib.osc import oscAPI

class CButton(Button):
    def __init__(self, **kwargs):
        kwargs['text'] = kwargs.get('text', 'empty')
        super(CButton, self).__init__(**kwargs)

class OscWidget(GridLayout):
    def __init__(self, **kwargs):
        super(OscWidget, self).__init__(**kwargs)

    def callback(self, instance, *args):
        self.oscbutton.text = instance[2]
        oscAPI.sendMsg('/current/reply', dataArray=['answer'], ipAddr='192.168.1.101', port=8889)

class OscButtonApp(App):
    def buttpress(self, *args):
        oscAPI.sendMsg('/current/reply', dataArray=['call'], ipAddr='192.168.1.101', port=8889)

    def build(self):
        btnapp = OscWidget()
        oscAPI.init()
        oscid = oscAPI.listen(port=8889)
        osc.bind(oscid, btnapp.callback, '/test/reply')
        Clock.schedule_interval(lambda *x: osc.readQueue(oscid), 0)
        return btnapp


if __name__ == '__main__':
    OscButtonApp().run()

When I test it on my computer it works in both ways, so from another terminal I can send a message and the Kivy app reacts:

oscAPI.sendMsg('/test/reply', dataArray=['change'], ipAddr='localhost', port=8889)

Sending this makes Kivy change the label on the button. Also, setting up a listen port on another terminal (and editing the output port on the script) shows me the message sent from Kivy. When I try this simple app on my phone, although, it look like it can only send messages, and not receive them. What am I doing wrong?

Thanks


Solution

  • By default, oscApi.listen listen only on 127.0.0.1. If you want to receive message from everyone, you need to listen to 0.0.0.0:

    oscid = oscAPI.listen(ipAddr='0.0.0.0', port=8889)