Changing postdelayed method every entrance - android

I have to change ttyS1 port's baudrate every second. So i need to wake-up remote machine on 9600 Bauds and after communicate with it on 19200 Bauds. But there is a time limit between wake-up signal and real data communication. I use Handler&Thread for this trick.
I done it and seems okay with Handler&Thread. I toggled postdelayed every entrance for 1 milliseconds and 500 milliseconds. But it works bad. Sometimes 1 milliseconds task takes almost 10-15 milliseconds.
Also i noticed that when i add "runOnUiThread" with some UI update, result goes worst like 30milliseconds.
Note: I need to send Wake-up signal everytime not just one time.
Any idea?
public void serial_query(){
try {
Runnable cookimageupdate = new Runnable(){
public void run(){
try {
if (mOutputStream != null) {
mSerialPort.close();
if (mLAP==0) //First LAP is used to HOLTEK Wake-Up. Second one is real data.
{mLAP=1; mSerialPort=new SerialPort(new File("/dev/ttyS1"), 9600, 0); mBufferbuf = (byte)0x00; mOutputStream.write(mBufferbuf);}
else {mLAP=0; mSerialPort=new SerialPort(new File("/dev/ttyS1"), 19200, 0); mOutputStream.write(mBuffer);}
} else {
return;
}
} catch (IOException e) {
e.printStackTrace();
return;
}
try{
runOnUiThread(new Runnable() {
public void run() {
//meat_temp.setText(_meatprobe_temp.toString());
if (_pt1000_status==1) {pt_status.setText(" PT1000 open-circuit");}
else if (_pt1000_status==2){pt_status.setText(" PT1000 short-circuit");}
else if (_pt1000_status==0){pt_status.setText(" -");}
}
});
}
catch (Exception e)
{
e.getLocalizedMessage();
}
if (mLAP==1)
{handler_serial.postDelayed(this, 1);}
else {handler_serial.postDelayed(this, 500);}
}
};
handler_serial.postDelayed(cookimageupdate, 50); //start with 50mSec. delay
}
catch(Exception e){
e.printStackTrace();
return;
}
};

1 millisecond is too short a time delay to post a delayed runnable. Most handlers take more time than that to process your message.
For such low delays you would be better off using
Thread.sleep().

Related

Which is best method to post delay when send message to Bluetooth?

I want to send five data to Bluetooth. First one is turn on and second one is turn off, third one is turn on, and so on... I want post delay between turn on and turn off (about 1 second). Which is best one to do it. Currently, I am using sleep in Thread class
for (int i=1;i<5;i++) {
try {
if(i%2==0){
send(1);
Thread.sleep(500);
}
else{
send(0);
Thread.sleep(500);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}

Timers accuracy

I am using this library to establish a connection and data transfer between two android devices. what i want to do is: one phone send the same message every 2 seconds, everytime the receiver reads the message, it starts some work..
I tried doing that with Timers and TimerTasks, but it is not synchronized at all. i get weird different delays.
public void onClick(View v){
new Timer().schedule(new TimerTask()
{
public void run()
{
bt.send("Start");//////////////////////////////////////////////////////
Log.i("time", "data sent "+System.nanoTime());
}
try {
Thread.currentThread();
Thread.sleep(50, 0);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
s.playSound();
//
}
}
, 0, 3000);
and also sleep() is not a good way to produce delays, because sometimes its interrupted.
Is there any other way to do that?

How to make arduino pin high and low for specific time from android

I am making android application which sends signal from android app to arduino.
But i want to make arduino pin high and low for specific time period say one hour.
i.e user will specify the time period and length of time period..
How i can approach in android for this..
For Android you need to schedule your background task at regular interval as a separate process. You can use Syncadapter for your android application.
or Try this
final Handler handler = new Handler();
Runnable runable = new Runnable() {
#Override
public void run() {
try{
//do your Arduino code here
//also call the same runnable
handler.postDelayed(this, 1000);
}
catch (Exception e) {
// TODO: handle exception
}
finally{
//also call the same Arduino code here
handler.postDelayed(this, 1000);
}
}
};
handler.postDelayed(runable, 1000);
Hope it helps !

Puzzle: Bluetooth data send interval shortened by half after each reconnect

I modified the standard Bluetoothchat example to send 4 bytes of data at a time to a bluetooth device every half a second. It works fine if I start the App fresh. However, there is a problem if I reconnect as follows:
While Bluetooth is connected, I click the connect button again on the menu and select the same device. This disconnects the bluetooth (not sure whether this is the right procedure to disconnect). Then, I connect again by selecting the device, and it will be reconnected. After reconnection, a very strange problem appears: instead of sending the data every half a second, it will send the data every quarter a second. If I go through the process again and reconnect, the time interval will become even shorter. It gets to a point that the bluetooth device on the receiving end can't keep up with the data. At this point, the only way out is to kill the app and restart again. Then everything becomes normal, till next time I try to reconnect again.
I have tried different things but nothing appear to fix this. For example, I made sure the thread sending the data is killed when disconnected so no multiple threads are sending the data. I was wondering whether the baud rate changed when reconnected, but then why would the baud rate affect the Thread.sleep(500); statement (which is responsible for controlling the half a second data send). Any help would be greatly appreciated.
Here is the code, the SendClass is created under the MainActivity:
class SendClass implements Runnable {
public void run() {
bytearr[0]=0;bytearr[1]=0;bytearr[2]=0;bytearr[3]=0;
while (!Thread.currentThread().isInterrupted()) {
if (mChatService==null || mChatService.getState()
!=BluetoothChatService.STATE_CONNECTED) {
continue;
} else {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mChatService.write(bytearr);
}
}//end of run
}//end of runnable
Then under STATE_CONNECTED:
case BluetoothChatService.STATE_CONNECTED:
setStatus(getString(R.string.title_connected_to,mConnectedDeviceName));
/*
if(sendingThread!=null){
//sendingThread.stop();
sendingThread.interrupt();
if(D) Log.i(TAG, "after sendingThread");
sendingThread = null;
}*/
sendingThread = new Thread(new SendClass());
sendingThread.start();
break;
As you can see, I tried to kill the thread before creating a new one but that didn't make any difference. Any suggestions?
You are creating a thread that never actually stops, even after you create a new thread and assign to the same variable that particular thread wont stop running.
You need to make sure that the thread will stop after it disconnects.
Here is my suggestion
Change your SendClass to:
class SendClass implements Runnable {
private boolean stopped = false;
public void setStopped(boolean s){
this.stopped = s;
}
public void run() {
bytearr[0]=0;bytearr[1]=0;bytearr[2]=0;bytearr[3]=0;
while (!Thread.currentThread().isInterrupted() && !stopped) {
if (mChatService==null || mChatService.getState() !=BluetoothChatService.STATE_CONNECTED) {
continue;
} else {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mChatService.write(bytearr);
}
}//end of run
}//end of runnable
Then when you start your thread keep the reference to the Runnable so you can call the setStopped(true); like this
SendClass sc = new SendClass();
sendingThread = new Thread(sc);
sendingThread.start();
When you disconnect the bluetooth dont forget to call sc.setStopped(true); so your thread will finish by not going into the while.

How to use scheduleAtFixedRate for executing in each second

in the below code send() function is executing many times in a second,i want to execute send() once in a second,how i change the code
timer.scheduleAtFixedRate(
new TimerTask() {
public void run() {
try {
send();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
},
1000,
1000);
send function is given below
void send() throws Exception, IOException
{
s=new Socket("10.0.2.2",4200);
r=new PrintWriter(s.getOutputStream());
while(true)
{
Log.e("msg","hi send\n");
r.print("hai");
}
}
Logcat output is given below
I replaced timers with Runnables/Handlers recently, it's much easier
//declare at top of your activity
private Handler h = new Handler();
private Runnable myRunnable = new Runnable() {
public void run() {
//do stuff
//run again in one second
h.postDelayed(myRunnable, 1000);
}
};
//trigger the runnable somewhere in your code e.g. onClickHander or onCreate etc
h.postDelayed(myRunnable, 1000);
It happened for me when I used a TaskTimer and the phone got into sleep mode. I think it is related to TimerTask using Thread.sleep() to provide the timing. This relies on uptimeMillis() which according to documentation - 'is counted in milliseconds since the system was booted. This clock stops when the system enters deep sleep (CPU off, display dark, device waiting for external input), but is not affected by clock scaling, idle, or other power saving mechanisms. This is the basis for most interval timing such as Thread.sleep(millls)'
Solution would be either to use AlarmManager or WakeLocks.
an easier approach would look like this:
new Thread() {
public void run() {
while(true) {
send();
try{
Thread.sleep(1000); // pauses for 1 second
catch(Exception e) {}
}
}
}.start();

Categories

Resources