Search code examples
python-3.xsocketskivybuildozerkivymd

socket sendto not working on Android (kivy, buildozer)


I am writing a script in kivymd to toggle an LED which is connected to a microcontroller. The script is supposed to send a socket UDP msg to the MCU server, which includes a command to toggle the LED. The script works fine on windows, and the building with buildozer is successful (although not shown in this script but I tried another version to simply print hello world to an MDLabel and it worked) but whenever I press the btn_led on Android, the application shuts down. Any ideas why this is happening and how to fix it?

from kivymd.app import MDApp
from kivymd.uix.button import MDFlatButton
from kivy.uix.screenmanager import Screen
import socket


class TableApp(MDApp):

    def build(self):
     
        # Add Widgets
        screen = Screen()

        btn_led = MDFlatButton(text="Toggle LED", on_release=self.toggle_led,
                           pos_hint={"center_x": 0.5, "center_y": 0.5})

        screen.add_widget(btn_led)
        return screen

    def toggle_led(self, event):
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.sendto(b"LED", ("10.0.0.167", 1111))
        s.close()


TableApp().run()

Solution

  • I just learned that the buildozer.spec file contains permissions which can be configured to include INTERNET. I uncommented the permissions line which contains INTERNET by default and everything worked like a charm!