Discover Bluetooth devices on Android

144 views
Skip to first unread message

Frédéric

unread,
Oct 6, 2022, 5:58:21 PM10/6/22
to Kivy users support
Hello,

I want to discover unpaired bluetooth devices on Android with Kivy.
If I understand correctly,  for Android I must receive intents in a BroadcastReceiver, after launching startDiscovery on a Bluetooth adapter

I succeeded to create a buildable kivy program, but it does nothing (I think that the onReceive method is never called).
Does anybody see what's missing here?

Here is the full code of main.py (in Buildozer I added the android.permissions  BLUETOOTH and BLUETOOTH_ADMIN

Frederic

----
from kivy.lang import Builder
from kivy.app import App

from jnius import autoclass
import kivy
from android.broadcast import BroadcastReceiver
import sys

BluetoothAdapter = autoclass('android.bluetooth.BluetoothAdapter')
BluetoothDevice  = autoclass('android.bluetooth.BluetoothDevice')

class BluetoothApp(App):
    def build(self):
        self.layout = Builder.load_string(kv)
        self.myData = []
        self.unpairedBT()
        self.layout.data = [item for item in self.myData]
        return self.layout
       
    def unpairedBT(self):
        BTAdapter = BluetoothAdapter.getDefaultAdapter()
        # Search foir unpaired devices
        print("Unpaired devices")
        self.myData.append({"text": "Unpaired Devices"})
        myReceiver = BroadcastReceiver(self.onReceive, actions = [BluetoothDevice.ACTION_FOUND, BluetoothAdapter.ACTION_DISCOVERY_STARTED, BluetoothAdapter.ACTION_DISCOVERY_FINISHED])
        myReceiver.start()
        BTAdapter.startDiscovery()

    # Called by Broadcastreceiver
    def onReceive(self, context, intent):
        print(f"*BT* On receive context{context}", flush = True)
        print(f"*BT* On receive intent {intent}", flush = True)
        sys.stdout.flush()
       
        self.myData.append({"text": f"Context {context}, intent {intent}"})
        self.layout.data = [item for item in self.myData]


if __name__ == '__main__':
    kv = '''
RecycleView:
    data: []
    viewclass: 'Label'
    RecycleBoxLayout:
        default_size_hint: 1, 1
        orientation: 'vertical'
'''
           
    BluetoothApp().run()
   



Frédéric

unread,
Oct 11, 2022, 7:44:38 AM10/11/22
to Kivy users support
Well,

no answer here, nor for the (almost) same question on Stack Overflow...
I didn't have any success on my trials to do it, and I didn't found a working example on internet...
It suppose that discovering Bluetooth devices isn't  something that jnius can do  in Android?


Frederic

Robert

unread,
Oct 11, 2022, 12:43:00 PM10/11/22
to Kivy users support
I think it is possible, I have played with that in the past.
If I had an example to share I would, but I don't have one.

Jnius is just an interface, the hard part is is to understand the Android api on the other side.
Reply all
Reply to author
Forward
0 new messages