Python bluetooth device scanning using pyjnius java wrapper

909 Views Asked by At

I'm trying to replicate the code below which is in java that's supposed to scan for bluetooth devices on an android device, but I'm trying to write it in python using a the pyjnius java to python wrapper.

There is very little documentation on how to do this so I cannot figure out how to get it working, looking at the java code it seems that the function onReceive is overridden inside BroadcastReceiver class but not sure how to do that in python. Currently the code just fails without any call traceback so I cannot even debug it.

Any help is appreciated

Java bluetooth code


 mBluetoothAdapter.startDiscovery();
 mReceiver = new BroadcastReceiver() {
 public void onReceive(Context context, Intent intent) {
     String action = intent.getAction();

     //Finding devices
     if (BluetoothDevice.ACTION_FOUND.equals(action))
     {
         // Get the BluetoothDevice object from the Intent
         BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
         // Add the name and address to an array adapter to show in a ListView
        mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
     }
   }
 };

 IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
 registerReceiver(mReceiver, filter);

python code


from android.broadcast import BroadcastReceiver
from jnius import autoclass
from android.permissions import request_permissions, Permission

BluetoothAdapter = autoclass('android.bluetooth.BluetoothAdapter')
BluetoothDevice = autoclass('android.bluetooth.BluetoothDevice')
BluetoothSocket = autoclass('android.bluetooth.BluetoothSocket')
BroadcastReceiver = autoclass('android.content.BroadcastReceiver')
IntentFilter = autoclass('android.content.IntentFilter')
Intent = autoclass('android.content.Intent')


request_android_permissions()
mBLuetoothAdapter = BluetoothAdapter.getDefaultDevice()
mBLuetoothAdapter.startDiscovery()

mReceiver = BroadcastReceiver(self.on_broadcast, actions = ['ACTION.FOUND'])
mReceiver.start()

def request_android_permissions():
    def callback(permissions, results):
        if all([res for res in results]):
            print("callback. All permissions granted")
        else:
            print("callback. Some permissions refused")

    request_permissions([Permission.ACCESS_COARSE_LOCATION, Permission.ACCESS_FINE_LOCATION])

def onReceive(context, intent):
    action = intent.getAction()
    
    if BluetoothDevice.ACTION_FOUND.equals(action):
        device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE)
        Devices.append(device.getName() + "  " + device.getAddress())

filter = IntentFilter(BluetoothDevice.ACTION_FOUND)
BroadcastReceiver.registerReceiver(mReceiver, filter)

1

There are 1 best solutions below

0
On

I used BroadcastReceiver for WiFi scanning, it works and you should be able to adapt this example for Bluetooth.

https://github.com/kivy/python-for-android/issues/2308

This is in 'issues' because there is a crash bug in BroadcastReceiver which occurs on a second start, for example from on_resume(). The fix is described in the second post, and requires cloning a local copy of p4a, editing as described, and pointing to it in the .spec with p4a.source_dir