Question

I am working with sending some signals from an App via Bluetooth.

The way my application works is that it starts by entering an activity which handles all the Bluetooth related things. Here it shows a layout with all the found devices.

When a device is pressed it connects to it, as in:

public BTCommunicator myBTCommunicator = null;

gets the MAC-address into it.

After it is successfully connected, I go to another activity with a bunch of button listeners.

What happens is that when you press a button it calls a function from the bluetooth activity, which should send a signal to the external device.

public void updateMotorControl(int left, int right) {

    if (myBTCommunicator != null) {                                           
        // send messages via the handler
        sendBTCmessage(BTCommunicator.NO_DELAY, motorLeft, left * directionLeft, 0);
        sendBTCmessage(BTCommunicator.NO_DELAY, motorRight, right * directionRight, 0);
    }

The problem is that when we return, myBTCommunicator == null again. When I check my external device, it is still connected, but apparently myBTCommunicator isn't saved when you leave and return. Is there a way to solve this?

Was it helpful?

Solution

Make your Connection Globally Static

public BTCommunicator myBTCommunicator = null;

To

SomeGlobal.class
public static BTCommunicator myBTCommunicator;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top