I have a BluetoothSocket mmSocket;
declared in public cass of my main activity.
In a function, i asign value a variable and if connected i start a Thread.
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
mmSocket.connect();
mConnectedThread = new ConnectedThread(mmSocket);
mConnectedThread.start();
My code runing ok.
But, when i start another activity in menu, if i return in less of 5 secs to main activity the thread is ok and mi app read from BT ok. But if i stay in activity B more of 10 secs when i return to the main activity the variable mmSocket is null and my thread is kill.
Intent i = new Intent(getApplicationContext(), AjustesActivity.class);
i.putExtra("distancia", Math.floor(MetrosRecorridos / 10));
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivityForResult(i, 3);
Any solution?
EDIT (14/07/2015)
In my service class i have:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//In this part i connected whith Bluetooth.
new Handler().post(new Runnable() {
@Override
public void run() {
byte[] buffer = new byte[2500];
int bytes;
while (true) {
try {
bytes = mmInStream.read(buffer);
String readMessage = new String(buffer, 0, bytes);
sendMessage(readMessage);
} catch (IOException e) { }
}
});
}
private void sendMessage(String Msg) {
Intent intent = new Intent("custom-event-name");
intent.putExtra("message", Msg);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
and in my mainactivity:
@Override
public void onCreate(Bundle savedInstanceState) {
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
new IntentFilter("custom-event-name"));
}
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String message = intent.getStringExtra("message");
}
};
When i debug the main acitivity no recived the message. any idea?
Thanks