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).
Here is the full code of main.py (in Buildozer I added the android.permissions BLUETOOTH and BLUETOOTH_ADMIN
----
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()