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