Issue
I'm developing an Android Bluetooth App and I use aSyncTask when I want to scan available device around mine. This aSyncTask
is simple, in "onPreExecute()"
, I use "myBluteoothAdaptaer.startDiscovery()"
to discover devices and "myBluteoothAdaptaer.cancelDiscovery()"
in "onPostExecute()"
to stop it.
But when I run my app, the doInBackground()
function is executed only one time on the 1st click. If I click again on the button, Button's code work but aSyncTask isn't executed properly (when it work, a progressBar need 2s to be complete. When it doesn't work, it only need around 0.1 ms
to complete it).
So maybe this error is coming from doInBackground()
, stop at onPreExecute()
or something like this ?
Thanks in advance for your help.
Here is the code :
Button click code : (just set a )
/** Scan on click **/
Scan_btn.setOnClickListener(new View.OnClickListener() { // click on SCAN button
@Override
public void onClick(View view) {
if (!blueAdapter.isDiscovering()) {
//blueAdapter.startDiscovery();
Scan_btn_clic = true;
} else {
//blueAdapter.cancelDiscovery();
Scan_btn_clic = false;
}
Maj scan = new Maj();
scan.execute();
}
});
/***********/
doInBackground() function :
@Override
protected Void doInBackground(Void... arg0) {
int progress;
for (progress=0;progress<=100;progress++)
{
if (blueAdapter.isEnabled() && Scan_btn_clic == false || !blueAdapter.isEnabled())
{
for (int i=0; i<5000000; i++){}
//la méthode publishProgress met à jour l'interface en invoquant la méthode onProgressUpdate
publishProgress(progress);
progress++;
}
else if (blueAdapter.isEnabled() && Scan_btn_clic == true)
{
for (int i = 0; i < 100000000; i++) {}
//la méthode publishProgress met à jour l'interface en invoquant la méthode onProgressUpdate
publishProgress(progress);
progress++;
}
}
return null;
}
Solution
The solution was very simple, thanks to Jyoti JK and 0xDEADC0DE for help.
Only need to use Thread.sleep(milli)
instead of for()
loop.
Now my aSyncTask is working well
Answered By - HQ_BT
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.