0
votes

I am writing a android ble module for appcelerator-titanium and I have in a method the BluetoothGatt object after a succesfull connection with a bluetooth device. However I want to check what is the current status of the connection.

I tried to use:

public BluetoothGatt gatt;
private BluetoothDevice device;

@Kroll.method
public void discoverServices(){
    Log.i("PeripheralProxy", "discoverServices");
    if(gatt!=null){
        Log.i("PeripheralProxy", "Gatt is true discoverServices");
        try {
            if(device!=null){
                int connectionState= gatt.getConnectionState(device);

                Log.i("PeripheralProxy", "Device is not null");
            } else {
                Log.e("PeripheralProxy", "Device is null");
            }
        } catch (Exception e){
            Log.e("PeripheralProxy",e.getMessage());
        }
        //DISCOVER SERVICES
        boolean discoverResult = gatt.discoverServices();
        Log.i("PeripheralProxy", Boolean.toString(discoverResult));

    } else {
        Log.e("PeripheralProxy", "Gatt is null on discoverServices");
    }
}

However the console return the following exception: [ERROR] PeripheralProxy: (KrollRuntimeThread) [1,282875] Use BluetoothManager#getConnectionState instead.

But in this context I don't have access to the BluetoothManager. How can I get the connection state from the BluetoothGatt object?

On Android documentation they have this method BluetoothGatt.getConnectionState(BluetoothDevice device) Visit: https://developer.android.com/reference/android/bluetooth/BluetoothGatt.html#getConnectionState(android.bluetooth.BluetoothDevice)

But they also says: Not supported - please use getConnectedDevices(int) with GATT as argument. However this function requieres again the BluetoothManager which I don't have access in this context. Visit: https://developer.android.com/reference/android/bluetooth/BluetoothManager.html#getConnectedDevices(int)

Thanks for your help on this.

1

1 Answers

0
votes

Why can't you use

ble_manager=(BluetoothManager)getSystemService(BLUETOOTH_SERVICE);

Then you can use the getConnectionState() Method. This Object should always be available because the BLUETTOTH_SERVICE is part of the Context.