|
| 1 | +--- |
| 2 | +slug: tap-into-native-android-and-ios-apis-with-Pyjnius-and-pyobjus |
| 3 | +title: Tap into native Android and iOS APIs with Pyjnius and Pyobjus |
| 4 | +author: Feodor Fitsner |
| 5 | +author_title: Flet founder and developer |
| 6 | +author_url: https://github.com/FeodorFitsner |
| 7 | +author_image_url: https://avatars0.githubusercontent.com/u/5041459?s=400&v=4 |
| 8 | +tags: [releases] |
| 9 | +--- |
| 10 | + |
| 11 | +When building mobile apps with Flet, you may need to interact directly with platform-specific APIs. Whether it’s accessing system information, managing Bluetooth devices, or working with user preferences, **Pyjnius** and **Pyobjus** by Kivy provide a seamless way to bridge Python with Java (for Android) and Objective-C (for iOS). |
| 12 | + |
| 13 | +You can now integrate both Pyjnius and Pyobjus into your Flet apps! 🚀 |
| 14 | + |
| 15 | +<!-- truncate --> |
| 16 | + |
| 17 | +## Pyjnius for Android |
| 18 | + |
| 19 | +Pyjnius is a Python library for accessing Java classes using the **Java Native Interface** (JNI). |
| 20 | + |
| 21 | +### Adding to a project |
| 22 | + |
| 23 | +Add `pyjnius` dependency for Android builds only (other settings in `pyproject.toml` were omitted for brevity): |
| 24 | + |
| 25 | +```toml |
| 26 | +[project] |
| 27 | +name = "pyjnius_demo" |
| 28 | +version = "0.1.0" |
| 29 | +dependencies = [ |
| 30 | + "flet==0.27.1" |
| 31 | +] |
| 32 | + |
| 33 | +[tool.flet.android] |
| 34 | +dependencies = [ |
| 35 | + "pyjnius" |
| 36 | +] |
| 37 | +``` |
| 38 | + |
| 39 | +### Usage examples |
| 40 | + |
| 41 | +Here are some example of how Pyjnius can be used in your Flet Android app. |
| 42 | + |
| 43 | +#### Getting Android OS details |
| 44 | + |
| 45 | +```python |
| 46 | +from jnius import autoclass |
| 47 | + |
| 48 | +# Get Build and Build.VERSION classes |
| 49 | +Build = autoclass('android.os.Build') |
| 50 | +Version = autoclass('android.os.Build$VERSION') |
| 51 | + |
| 52 | +# Get OS details |
| 53 | +device_model = Build.MODEL |
| 54 | +manufacturer = Build.MANUFACTURER |
| 55 | +brand = Build.BRAND |
| 56 | +hardware = Build.HARDWARE |
| 57 | +product = Build.PRODUCT |
| 58 | +device = Build.DEVICE |
| 59 | +os_version = Version.RELEASE |
| 60 | +sdk_version = Version.SDK_INT |
| 61 | +``` |
| 62 | + |
| 63 | +#### Listing Bluetooth devices |
| 64 | + |
| 65 | +```python |
| 66 | +from jnius import autoclass |
| 67 | + |
| 68 | +# Get BluetoothAdapter instance |
| 69 | +BluetoothAdapter = autoclass('android.bluetooth.BluetoothAdapter') |
| 70 | +bluetooth_adapter = BluetoothAdapter.getDefaultAdapter() |
| 71 | + |
| 72 | +if bluetooth_adapter is None: |
| 73 | + print("Bluetooth not supported on this device") |
| 74 | +else: |
| 75 | + if not bluetooth_adapter.isEnabled(): |
| 76 | + print("Bluetooth is disabled. Please enable it.") |
| 77 | + else: |
| 78 | + print("Bluetooth is enabled.") |
| 79 | + |
| 80 | + # Get paired devices |
| 81 | + paired_devices = bluetooth_adapter.getBondedDevices() |
| 82 | + for device in paired_devices.toArray(): |
| 83 | + print(f"Device Name: {device.getName()}, MAC Address: {device.getAddress()}") |
| 84 | +``` |
| 85 | + |
| 86 | +### Accessing the Activity |
| 87 | + |
| 88 | +App main activity instance can be retrieved with the following code: |
| 89 | + |
| 90 | +```python |
| 91 | +import os |
| 92 | +from jnius import autoclass |
| 93 | + |
| 94 | +activity_host_class = os.getenv("MAIN_ACTIVITY_HOST_CLASS_NAME") |
| 95 | +assert activity_host_class |
| 96 | +activity_host = autoclass(activity_host_class) |
| 97 | +activity = activity_host.mActivity |
| 98 | +``` |
| 99 | + |
| 100 | +Heck, you can basically call any Android API using Pyjnius - endless posibilities! |
| 101 | + |
| 102 | +Check complete [Flet Pyjnius example](https://github.com/flet-dev/python-package-tests/tree/main/Pyjnius). |
| 103 | + |
| 104 | +For more Pyjnius examples and API refer to the [Pyjnius Documentation](https://Pyjnius.readthedocs.io/en/latest/quickstart.html). |
| 105 | + |
| 106 | +...or just use ChatGPT (or your favorite LLM) to get more ideas and solutions! 😅 |
| 107 | + |
| 108 | +## Pyobjus for iOS |
| 109 | + |
| 110 | +Pyobjus is a library for accessing Objective-C classes as Python classes using Objective-C runtime reflection. |
| 111 | + |
| 112 | +### Adding to a project |
| 113 | + |
| 114 | +Add `pyobjus` dependency for iOS builds only (other settings in `pyproject.toml` were omitted for brevity): |
| 115 | + |
| 116 | +```toml |
| 117 | +[project] |
| 118 | +name = "Pyjnius" |
| 119 | +version = "0.1.0" |
| 120 | +dependencies = [ |
| 121 | + "flet==0.27.1" |
| 122 | +] |
| 123 | + |
| 124 | +[tool.flet.ios] |
| 125 | +dependencies = [ |
| 126 | + "pyobjus" |
| 127 | +] |
| 128 | +``` |
| 129 | + |
| 130 | +### Usage examples |
| 131 | + |
| 132 | +#### The simplest example |
| 133 | + |
| 134 | +```python |
| 135 | +from pyobjus import autoclass |
| 136 | + |
| 137 | +NSString = autoclass('NSString') |
| 138 | +text = NSString.alloc().initWithUTF8String_('Hello world') |
| 139 | +print(text.UTF8String()) |
| 140 | +``` |
| 141 | + |
| 142 | +#### Getting OS details |
| 143 | + |
| 144 | +```python |
| 145 | +from pyobjus import autoclass |
| 146 | +from pyobjus.dylib_manager import load_framework, INCLUDE |
| 147 | + |
| 148 | +# Load Foundation framework |
| 149 | +load_framework(INCLUDE.Foundation) |
| 150 | + |
| 151 | +# Get NSProcessInfo instance |
| 152 | +NSProcessInfo = autoclass('NSProcessInfo') |
| 153 | +process_info = NSProcessInfo.processInfo() |
| 154 | + |
| 155 | +# Retrieve OS version as a string |
| 156 | +os_version = process_info.operatingSystemVersionString.UTF8String() |
| 157 | + |
| 158 | +print(f"iOS Version: {os_version}") |
| 159 | +``` |
| 160 | + |
| 161 | +#### Working with app user settings |
| 162 | + |
| 163 | +```python |
| 164 | +from pyobjus import autoclass, objc_str |
| 165 | + |
| 166 | +NSUserDefaults = autoclass('NSUserDefaults') |
| 167 | + |
| 168 | +key = "pyobjus_hello_world_key" |
| 169 | +value = "Hello, world!" |
| 170 | + |
| 171 | +# set key |
| 172 | +NSUserDefaults.standardUserDefaults().setObject_forKey_(objc_str(value), objc_str(key)) |
| 173 | + |
| 174 | +# get key |
| 175 | +ret = NSUserDefaults.standardUserDefaults().stringForKey_(objc_str(key)) |
| 176 | + |
| 177 | +assert ret.UTF8String() == value |
| 178 | +``` |
| 179 | + |
| 180 | +Check complete [Flet Pyobjus example](https://github.com/flet-dev/python-package-tests/tree/main/pyobjus). |
| 181 | + |
| 182 | +For more Pyobjus examples and API refer to the [Pyobjus Documentation](https://pyobjus.readthedocs.io/en/latest/quickstart.html). |
| 183 | + |
| 184 | +## Plyer challenge |
| 185 | + |
| 186 | +There is a [Plyer](https://github.com/kivy/plyer) project by Kivy team which uses both Pyjnius and Pyobjus under the hood. |
| 187 | + |
| 188 | +It's not ported to Flet yet. You can either get more usage examples for [Android](https://github.com/kivy/plyer/tree/master/plyer/platforms/android) and [iOS](https://github.com/kivy/plyer/tree/master/plyer/platforms/ios) from there or help us with porting it to Flet. |
0 commit comments