The method and class in question is the setInstanceId method of the class BluetoothGattCharacteristic. The method is declared as:
public void setInstanceId(int instanceId) {
mInstance = instanceId;
}
For reasons that are beyond the scope of this question but which can be explained here, I am trying to make a copy of a BluetoothGattCharacteristic object. I am creating the new instance using this following code:
mTxCharacteristic2 = new BluetoothGattCharacteristic(mTxCharacteristic.getUuid(),mTxCharacteristic.getProperties(), mTxCharacteristic.getPermissions());
So basically I have an instance of the characteristic called mTxCharacteristic and I want to make a copy, mTxCharacteristic2. I then want to make the following call:
mTxCharacteristic2.setInstanceId(mTxCharacteristic.getInstanceId());
This results in a compilation error "error: cannot find symbol method setInstanceId(int)".
The part I do not understand is that setInstanceId is declared as public to the class. So why can't I call it?
I also tried to call it using reflection. However, when I call:
Method setInstanceIdMethod = BluetoothGattCharacteristic.class.getDeclaredMethod("setInstanceId", int.class);
Calling this results in a NoSuchMethodException exception being thrown. Even if the problem was due to an SDK 24 vs. SDK 28 issue, the phone I am getting the exception on at runtime is running Android 9.
A possible explanation that comes to mind is that maybe Android Studio is actually using some older version of the the BluetoothGattCharacteristic class than what it shows me in the BluetoothGattCharacteristic.java class that opens when I right click on the class name. I have the minSdkVersion set to 24 and the targetSdkVersion set to 28 in my build.gradle file. When I choose "Reveal in Finder" for the BluetoothGattCharacteristic.java file it shows it at a path of: ~/Library/Android/sdk/sources/android-28/android/bluetooth/BluetoothGattCharacteristic.java
Or perhaps there is some way in which this method is declared as not being available in SDK 24? Either way I clearly don't understand what it is doing. I am somewhat new to Java and have come from a C++ background.