Skip to content

Commit 6573b91

Browse files
committed
added file for getting device list on android
1 parent 4ea7abb commit 6573b91

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# MIT License
2+
3+
# Copyright (c) 2023-2025 GvozdevLeonid
4+
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
12+
# The above copyright notice and this permission notice shall be included in all
13+
# copies or substantial portions of the Software.
14+
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.
22+
23+
from threading import Event
24+
25+
try:
26+
from jnius import (
27+
autoclass,
28+
cast,
29+
)
30+
except ImportError:
31+
def autoclass(item):
32+
raise RuntimeError('autoclass not available')
33+
34+
def cast(item):
35+
raise RuntimeError('cast not available')
36+
37+
try:
38+
from android.broadcast import BroadcastReceiver
39+
from android.runnable import run_on_ui_thread
40+
except ImportError:
41+
def BroadcastReceiver(item):
42+
raise RuntimeError('BroadcastReceiver not available')
43+
44+
def run_on_ui_thread(f):
45+
raise RuntimeError('run_on_ui_thread not available')
46+
47+
48+
class USBBroadcastReceiver:
49+
def __init__(self, events: dict) -> None:
50+
self.events = events
51+
52+
@run_on_ui_thread
53+
def start(self) -> None:
54+
self.br = BroadcastReceiver(self.on_broadcast, actions=['libusb.android.USB_PERMISSION'])
55+
self.br.start()
56+
57+
def stop(self) -> None:
58+
self.br.stop()
59+
60+
def on_broadcast(self, context, intent) -> None:
61+
action = intent.getAction()
62+
UsbManager = autoclass('android.hardware.usb.UsbManager')
63+
if action == 'libusb.android.USB_PERMISSION':
64+
usb_device = cast('android.hardware.usb.UsbDevice', intent.getParcelableExtra(UsbManager.EXTRA_DEVICE))
65+
66+
if usb_device is not None:
67+
granted = intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, False)
68+
device_name = usb_device.getDeviceName()
69+
70+
if device_name in self.events:
71+
self.events[device_name]['granted'] = granted
72+
self.events[device_name]['device'] = usb_device
73+
self.events[device_name]['event'].set()
74+
75+
76+
bladerf_usb_vids = (0x2cf0, 0x1d50)
77+
bladerf_usb_pids = (0x5246, 0x5250, 0x6066)
78+
79+
80+
def get_bladerf_device_list(num_devices: int | None = None) -> list:
81+
events = {}
82+
bladerf_device_list = []
83+
usb_broadcast_receiver = USBBroadcastReceiver(events)
84+
85+
this = autoclass('org.kivy.android.PythonActivity').mActivity
86+
87+
usb_manager = this.getSystemService(autoclass('android.content.Context').USB_SERVICE)
88+
device_list = usb_manager.getDeviceList()
89+
90+
permission_intent = autoclass('android.app.PendingIntent').getBroadcast(
91+
this.getApplicationContext(),
92+
0,
93+
autoclass('android.content.Intent')('libusb.android.USB_PERMISSION'),
94+
autoclass('android.app.PendingIntent').FLAG_MUTABLE,
95+
)
96+
97+
if device_list:
98+
for idx, usb_device in enumerate(device_list.values()):
99+
device_name = usb_device.getDeviceName()
100+
if (
101+
usb_device.getVendorId() in bladerf_usb_vids
102+
and usb_device.getProductId() in bladerf_usb_pids
103+
):
104+
105+
if usb_manager.hasPermission(usb_device):
106+
usb_device_connection = usb_manager.openDevice(usb_device)
107+
file_descriptor = usb_device_connection.getFileDescriptor()
108+
bladerf_device_list.append((file_descriptor, usb_device.getSerialNumber(), usb_device.getManufacturerName(), usb_device.getProductName()))
109+
else:
110+
events[device_name] = {'event': Event(), 'granted': False, 'device': None}
111+
usb_manager.requestPermission(usb_device, permission_intent)
112+
113+
if num_devices is not None and idx + 1 == num_devices:
114+
break
115+
116+
if len(events):
117+
usb_broadcast_receiver.start()
118+
for _, info in events.items():
119+
info['event'].wait()
120+
usb_broadcast_receiver.stop()
121+
122+
for _, info in events.items():
123+
if info['granted']:
124+
usb_device = info['device']
125+
usb_device_connection = usb_manager.openDevice(usb_device)
126+
file_descriptor = usb_device_connection.getFileDescriptor()
127+
bladerf_device_list.append((file_descriptor, usb_device.getSerialNumber(), usb_device.getManufacturerName(), usb_device.getProductName()))
128+
129+
return bladerf_device_list

0 commit comments

Comments
 (0)