connectGatt issues

108 views
Skip to first unread message

Micah Johnson

unread,
Jan 14, 2016, 12:29:35 AM1/14/16
to Kivy users support
I am having some trouble connecting to my BLE device in my android app. When I use device.connectGatt(...) I get the following error:

I/python  (12811):    File "jnius/jnius_export_class.pxi", line 894, in jnius.jnius.JavaMultipleMethod.__call__ (jnius/jnius.c:23650)
I/python  (12811):  jnius.jnius.JavaException: No methods matching your arguments

Part of the problem is that to use connectGatt you need an instance of BluetoothGattCallback which is an abstract class (which can't be done directly through PyJNIus). It is outlined here how to get around this problem. In brief, it consists of creating a custom java class that extends the BluetoothGattCallback and adds a public interface that we can use to create a our own python gattcallback.

I admit I am pretty new to java so if you see something glaring feel free to point me to a tutorial on the topic. At the moment I cannot figure out what I am doing wrong that would cause the error. Any help would be greatly appreciated. Thanks in advanced!

Here is my java class:

package org.radapp;
import android.bluetooth.*;

public class BluetoothGattImplem extends BluetoothGattCallback{
   
   public interface OnBluetoothGattCallback {
      
      // ... all the methods from BluetoothGattCallback here ... like: 
      void onCharacteristicChanged(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic);
    
    } 
    // private storage for the callback object 
    private OnBluetoothGattCallback callback = null;
    
    // method to set the callback object 
    
    public void setCallback(OnBluetoothGattCallback callback) {
        this.callback = callback;
    }
    
    // implement all the methods, and redirect to the callback object 
    public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
        if (this.callback!=null) 
            this.callback.onCharacteristicChanged(gatt, characteristic);
    }
}

Here is my python gattcallback:

    class PyBluetoothGattCallback(PythonJavaClass):
        __javacontext__ = "app"
        __javainterfaces__ = ["org/radapp/BluetoothGattImplem$OnBluetoothGattCallback"]

        def __init__(self):
            super(PyBluetoothGattCallback, self).__init__()
            print "callback init"

        @java_method('(Landroid/bluetooth/BluetoothGatt;Landroid/bluetooth/BluetoothGattCharacteristic;)V')
        def onCharacteristicChanged (self,gatt,characteristic):
            print "Characteristic Changed!"

And here is how I am using it:

        context = autoclass('android.content.Context')
        BluetoothGattImplem = autoclass('org/radapp/BluetoothGattImplem')
        pycallback = PyBluetoothGattCallback()
        gattcallback = BluetoothGattImplem()
        gattcallback.setCallback(pycallback)
        device.connectGatt(context, False, gattcallback)

Note: device is an object that is obtained from startLeScan and I have confirmed it is for sure a BluetoothDevice 



Pat L

unread,
Aug 23, 2016, 8:32:15 AM8/23/16
to Kivy users support
I'm having the same issue . Has anyone an idea?
Reply all
Reply to author
Forward
0 new messages