简体   繁体   中英

onServicesDiscovered gives 'Method does not override method from its super class' Error

在此处输入图片说明 I am working on Bluetooth Low energy, i have connected my Bluetooth device to my android but when i try to discover its services and implement onServicesDiscovered by Overriding it. It gives me syntax error that Method does not override method from its super class. I searched it on google and Stack but could't find any appropriate Solution. Please help me in this. Thanking in anticipation. Here is my code and Screenshot of the Error.

@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {

    List<BluetoothGattService> services = gatt.getServices();
    Log.i("onServicesDiscovered", services.toString());
    gatt.readCharacteristic(services.get(1).getCharacteristics().get
            (0));
}

I have written this method in my class and here is the class header. public class A1 extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemClickListener

This is wrong. onServicesDiscovered() is method of abstract BluetoothGattCallback class. So your @Override tells compiler this method overrides parent's method, but you are not extending BluetoothGattCallback so this raises the error.

You are most victim of crappy tutorials where author didn't care isolating elements and he ie made Activity implementing all the interfaces and listeners, instead of having them separated. As solution you must either rework your code and extend BluetoothGattCallback or abandon bad practice and separate the callback ie by creating inner class:

class MyCallback extends BluetoothGattCallback {
   // your code here
}

and then using instance of MyCallback .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM