0

Is it possible to call method outside class, using an existing instance? I want to read data from sensor and try to send it through BT connection (already connected and sending other informations) but I cannot call write() method from "onSensorChange".

I am only capable to call write() and send message in "private class ConnectThread" (using connectedThread.write();).

Whole code works like a charm, I'm sending only most important parts of it to acquaint you with my problem.

Thanks in advance!

public class MainActivity extends WearableActivity implements SensorEventListener{

BluetoothAdapter bluetoothAdapter;
String TEXT = "textToSend"; 
private SensorManager sensorManager;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);  
}

@Override
public void onSensorChanged(SensorEvent event) {     
   TEXT = "..something that I want to send..";

   ConnectedThread.currentThread.write(); //TOTAL nonsense, tried several things but 
//cannot call write(), how should I call this from existing thread?  
}


/* ___ Finding paired devices ___ */
private void pairedDevices() {       
    final ConnectThread connectThread = new ConnectThread(device);              
}


/* ___ Connecting to a Server. ___*/
private class ConnectThread extends Thread {

    private final BluetoothSocket mmSocket;
    private final BluetoothDevice mmDevice;
    ConnectedThread connectedThread;

    public void run() {
            mmSocket.connect();
            Log.d("Connected", "To Server");

            connectedThread = new ConnectedThread(mmSocket);
            connectedThread.start();
            connectedThread.write();
    }                 
}


/* Managing Connection */
private class ConnectedThread extends Thread {
    private final BluetoothSocket mmSocket;

    public ConnectedThread(BluetoothSocket socket) {

    private void write() { //How to acces this method from "public void onSensorChanged"?

        if (TEXT != null) {
            outputStream.println(TEXT);
            Log.d(TEXT, "Written to Server");
        }
    }
}
4
  • You cannot call private methods outside of the class that they're enclosed in... private means that it can only be accessed by the class itself, protected would allow you to access in the same package which is something you can have a look at. You might want to read more about how OOP scope works in Java.
    – px06
    Sep 10, 2016 at 22:04
  • Thanks for quick reply. Even if I changed all private's to public, there was an error that disallowed me to use this method. Maybe I've overlooked something, I would try again tommorow and follow your guides. Thanks!
    – JoeDoe
    Sep 10, 2016 at 22:08
  • Try posting a stacktrace after changing the scope, so we can see what the problem is.
    – px06
    Sep 10, 2016 at 22:09
  • i think you should start connectThread first. Sep 10, 2016 at 22:29

1 Answer 1

0

Ok so a few problems. Actually, more than a few, and so this answer is kind of going to be all over the place (TL;DR: Go read the tutorials and try again):

First of all write() is private. Rather than explaining what that means, since this is already explained well in many places, I suggest you just take a look at the official tutorial on access specifiers.

Second, even if you made it not private, you have:

ConnectedThread.currentThread.write(); 

But you can't do that for a few reasons. First, there is no ConnectedThread.currentThread so I'll assume you meant ConnectThread.connectedThread, and secondly connectedThread is an instance member not a class member (i.e. static), so you need an actual ConnectThread instance:

private class ConnectThread extends Thread {
    ...
    ConnectedThread connectedThread;
    ...
}

So you may also want to read the official tutorial on class members and the official tutorial on instance members. I also strongly suggest you take a quick look at the general write-up about variables for an overview.

You need an actual, specific instance of a ConnectThread to work with here (simply making that static is not the correct choice here even though it would compile). So you'd need to make that available to your onSensorChanged() method. Unfortunately advice is now becoming too broad, there's a lot of ways to skin this cat, so you really ought to read through all the relevant official tutorials then give it another shot from scratch.

I will say this: You have...

private void pairedDevices() {       
    final ConnectThread connectThread = new ConnectThread(device);              
}

Here connectThread goes out of scope as soon as that function returns. That is, you create a ConnectThread, then immediately throw away any references you had to it, thus dooming yourself to never be able to use that instance you just created. You'll need to not do that. You could store it in a member field of MainActivity, for example, and access it from there.

Btw you might want to see if you can come up with two class names that aren't as similar to each other as ConnectThread and ConnectedThread, this is only adding to your confusion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.