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");
}
}
}
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.