Actually I don't know how it calls it, from P2, I just run it without any warnings and even if I off or on my GUI, it can reconnect over and over again for it always closes and in-progress connection and close the socket. Unlike on the revised code for P3, it does not, that is the reason why you won't be able to reconnect on the second time. I am not very good with the bluetooth programming, I just got it from multiple references, unfortunately I could not find any good reference that works for P3.
I tried testing the old code with P2, and it still works. I don't know how you call it, but I will try comparing the old codes and the new codes for P3, maybe I will get how the code is called.
was removed from the old P2 code, in the revised P3 code. But when I added in the P3 code, it says that the function does not exist.
when I tried using "act.unregisterReceiver(myDiscoverer);" it executes program but does not run and stops the application.
Here is the P2 Code:
if(discoveredDeviceName.equals("HMSoft")) {//name of device
ToastMaster("Connecting you Now !!!");
unregisterReceiver(myDiscoverer);
connectBT = new ConnectToBluetooth(discoveredDevice);
new Thread(connectBT).start();
}
Here is the P3 Code:
if(discoveredDeviceName.equals("HMSoft")) {//name of device
ToastMaster("Connecting you Now !!!");
connectBT = new ConnectToBluetooth(discoveredDevice);
new Thread(connectBT).start();
}
Here is the P3 Code that does not execute the GUI:
if(discoveredDeviceName.equals("HMSoft")) {//name of device
ToastMaster("Connecting you Now !!!");
act.unregisterReceiver(myDiscoverer); // evaluating //changed
connectBT = new ConnectToBluetooth(discoveredDevice);
new Thread(connectBT).start();
I think this is the part that affects the execution, the running, closing and reconnection of the GUI.
This is currently the part where I am working on, it only stops the GUI or it does does not reconnect. The target is it should connect onto the device with a new thread after unregistering the Receiver (myDiscoverer).
if(discoveredDeviceName.equals("HMSoft")) {//name of device
ToastMaster("Connecting you Now !!!");
if(foundDevice == false) {
act.unregisterReceiver(myDiscoverer);
} else {// evaluating //changed
connectBT = new ConnectToBluetooth(discoveredDevice);
new Thread(connectBT).start();
}
it s really difficult to help you without knowing EXACTLY which code you are NOW using. Please put it completely: from some of your posts i can guess that you have made changes, sometimes i know what they were, sometimes i dont know if you have only tried something then left for another change and so on.
As for the last post eg, i dont understand why you are unregistering when the device is not found...
I did not change anything, just made some tests, What I am trying to do now is on the last post, I was just doing some tests that is why it is false, but either it its true all false, it does not work properly. Need to create the whole code for P3 to work like the old one in P2. I already made tests and comparison and it all directs me to the last posted code.
if(discoveredDeviceName.equals("HMSoft")) {//name of device
ToastMaster("Connecting you Now !!!");
unregisterReceiver(myDiscoverer);
connectBT = new ConnectToBluetooth(discoveredDevice);
new Thread(connectBT).start();
}
If we could only find an equivalent code for P3, I think the code will already be finished (especially the " unregisterReceiver(myDiscoverer); " part)
After a lot of experimental coding. I think there are no problems regarding the codes, but only different responses of the GUI when the android device back button and home button is pressed. I will be doing a couple of hardware testing and will be posting the results.
Tests already finished, indicated are the observations.
P2 codes when executed with or without the unregisterReceiver(myDiscoverer); have the same behavior. So since problems are encountered when this line of code is added, I decided to remove it.
Response of the P2 and P3 codes when the back button and home button is pressed.
P2 hides on the background and disconnects connection with the bluetooth. While P3 hides on the background while still playing the application.
This is the reason why when P2 is placed on background, it re-executes / reconnects to the target device. While P3, while placed on the background, does not connect to the target device when the application is re-executed. You need to close the application running in the background before re-executing the application.
And since the "unregisterReceiver(myDiscoverer);" is not used anymore, I also removed lines like "Activity act;" and "act.unregisterReceiver(myDiscoverer);" from the coding.
private static final int RESULT_OK = 1; -- is added in the group of codes.
public static final int MESSAGE_WRITE = 1;
public static final int MESSAGE_READ = 2;
private static final int RESULT_OK = 1; // Added
String readMessage="";
SendReceiveBytes sendReceiveBT=null;
protected void was changed to public void in the indicated line
public void onActivityResult(int requestCode, int resultCode, Intent data) {
"this.getActivity()" was added in the indicated lines
if (bluetooth.isEnabled()) {
this.getActivity().registerReceiver(myDiscoverer, new IntentFilter(BluetoothDevice.ACTION_FOUND));
this.getActivity().registerReceiver(checkIsConnected, new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED));
void ToastMaster(String textToDisplay) {
Toast myMessage = Toast.makeText(this.getActivity().getApplicationContext(), //changed
textToDisplay,
Toast.LENGTH_LONG);
myMessage.setGravity(Gravity.CENTER, 0, 0);
myMessage.show();
}
Now code can already be used to create your own GUI apps, for bluetooth communication with an Android Device and arduino.
@myrds===
P3 continues to run in background because you don't unregister...
which is against the android principles; that s why i have told you unregister in onPause()
Like I said before, I am not very good at coding bluetooth, please feel free to do the necessary corrections, and I don't know how to unregister it efficiently.
If anyone knows a solution regarding the unregistering of bluetooth codes when application is placed on the background, please post your codes and I will test if it would work or not. Thank you.
if(discoveredDeviceName.equals("HMSoft")) {//name of device
ToastMaster("Connecting you Now !!!");
public void onPause(){
super.onPause();
if(myDiscoverer != null){
getActivity().unregisterReceiver(myDiscoverer);
myDiscoverer = null;
}
connectBT = new ConnectToBluetooth(discoveredDevice);
new Thread(connectBT).start();
if(discoveredDeviceName.equals("HMSoft")) {//name of device
ToastMaster("Connecting you Now !!!");
if(myDiscoverer != null){
getActivity().unregisterReceiver(myDiscoverer);
myDiscoverer = null;
}
connectBT = new ConnectToBluetooth(discoveredDevice);
new Thread(connectBT).start();
and this:
if(discoveredDeviceName.equals("HMSoft")) {//name of device
ToastMaster("Connecting you Now !!!");
getActivity().unregisterReceiver(myDiscoverer);
connectBT = new ConnectToBluetooth(discoveredDevice);
new Thread(connectBT).start();
after I press home or back, it does not play on the background either, it even does not reconnect to the target device.
It seems we won't be able to unregister it using processing 3 and just let it run on the background than letting it unable to reconnect when home or back is pressed. I think this is a consequence when created with P3. Either or, everything is working as it should be.
I think this answer all the questions, thanks for the help guys, specially akenaton for all his contributions, atleast we are able to run and make bluetooth communicate with the device.
My apologies, but I do not now how to insert in on the P2 codes or in the current P3 codes, that is why I was requesting for you to post the codes for me to be able to understand better.
The code still does not work in P3 guys, even if I create the codes in a different method. I think that the only method that the developer can do, is to give a warning or instruction to the user regarding the running of the application in the background.
Also for those who are trying to access the files in the google drive, the files will not be accessible anymore. This is due to the reason that the code use there is in P2 and already obsolete.
By the way guys this is the error. The function onPause(), does not exist. Or guys, do you know any code that I can use to prevent the application from playing on the background?
Guys observation problem, I was just able to observe it properly today, I think there is no problem using the pause or unregister using P3. This is currently my observation.
When I play the app, it opens the app and runs properly. And when I press the Home button ans re-access the application, either I re-access it from the backround or reopen application from its icon, it re-opens and runs properly.
When I play the app and runs properly, pressing the Back button will place the application in the background, but when I re-access it, either from the background or re-opening application from the icon, the application tries to reconnect to the device, but since the device is already connected, it won't be able to detect it.
In conclustion, I think there is no more problems with unregistering the device, the only issue left is the response of the application when the back button is pressed.
Do you have sample codes that I could use that closes the application when the back button is pressed? I think this is a good solution on the issue regarding the pressing of the back button.
When you unregister the application, do you do it onPause? Maybe you should do it onStop instead. I am interested to work with your code but I have my hands tight doing some other work and unfortunately I won't be able to try your code but until the weekend, not suggesting I am an expert but more to try to figure it out how to get it run properly.
@ === kfrajer I am trying any suggestion, you can suggest any solution anytime you can, a lot of problems already fixed.
I also apologize specially to @akenaton, it took a while for us to solve the unregistering, wherein I was able to make some bad observations, I generalized the problem involving the home button, but the problem was just with the back button all along.
I was able to run codes without any errors, but still the same problem after pressing the back button. Still could not re-access/reconnect with bluetooth device.
I think the only solution is to close the application after pressing the back button.
when you call onPause() you unregister either with the home or back button.
what have you tried with onResume()???? - Put the code you used. It s here that you have to register again, testing of course wether your discoverer is == null.
it must work (i have tested and got all the messages that it works)
as for System.exit() it s a bad idea, and why 1 (which means "an error occurred); why are you not using "finish()"?
System.exit(someInt) works with 1,0,-1 (and other values i think); these values do not change anything.
as for finish() i have already told you that you are in a fragment!
this getActivity().finish()
as for onResume() i repeat::: what code have you used which mahes your app crashing (as you said)
Does not work either, kindly post your workable code.
But since the only problem lies on the time when the back button is pressed, I prefer using the codes with the "public void onBackPressed(" . Please post any countermeasure aside from the used of "System.exit(0);" .
as for finish() are you getting "finish() does not exist" as you got before??? - i dont think!
as for on resume you try to unregister; and it is wrong, you have to REGISTER and the best way could be to create some method for that in order to call when onCreate is called or when onResume is called (for registering IF your receiver is null)
i dont know wether you know how to code for bluetooth but till you dont look at the android lifecycle you cannot code for android.
When you call finish() the app is firstly onPause() then onStop then onDestroy() so your receiver is null; when you comme back (without having called finish() and without your backPressed() method which has not any reason to be called) onResume is automatically called.
I have tested your code many times (without the arduino but it does not matter) added what is necessary for fragment, added the onPause() and onResume() methods and when you hit the home or back button and your app is not in the foreground receiver is null but when i come again receiver is here.
For the finish(), used for fragment, I was able to run it but same problem. For the use of onPause(), onDestroy(), onResume, etc, kindly post the whole code and I will test it.
If there no more codes to be shared, let us stick with the code that works with arduino.
For further improvements you want to do with the code, just post your code and I will test it and I will post feedbacks with regards on the testing. Or if you are capable of testing it as well in your end that works with bluetooth connection with arduino, then much better.
Guys, I've been reviewing akenaton's posts, and I was wandering why does some of the codes work on his end, but does not work in my end. Now I was able to detect the problem. When I used P2 before, P2 created android manifest, and when I used P3, the code, suddenly relies some of its process on the android manifest.
So after I made a new code and tested it, it does not run it on the other code, wherein connection losts. So I tried to delete the android manifest on the P3 code we are using here in order for it to create a new android manifest when we run the program. And now GUI automatically stops. The codes automatically stops onStop, onPause etc.
Another problem, now I see why it works with akenaton's coding. But when tested here in my end, it does not.
Answers
Actually I don't know how it calls it, from P2, I just run it without any warnings and even if I off or on my GUI, it can reconnect over and over again for it always closes and in-progress connection and close the socket. Unlike on the revised code for P3, it does not, that is the reason why you won't be able to reconnect on the second time. I am not very good with the bluetooth programming, I just got it from multiple references, unfortunately I could not find any good reference that works for P3.
you can refer on the very first code I placed on the post.
I tried testing the old code with P2, and it still works. I don't know how you call it, but I will try comparing the old codes and the new codes for P3, maybe I will get how the code is called.
This line:
unregisterReceiver(myDiscoverer);
was removed from the old P2 code, in the revised P3 code. But when I added in the P3 code, it says that the function does not exist.
when I tried using "act.unregisterReceiver(myDiscoverer);" it executes program but does not run and stops the application.
Here is the P2 Code:
Here is the P3 Code:
Here is the P3 Code that does not execute the GUI:
I think this is the part that affects the execution, the running, closing and reconnection of the GUI.
This is currently the part where I am working on, it only stops the GUI or it does does not reconnect. The target is it should connect onto the device with a new thread after unregistering the Receiver (myDiscoverer).
@myrds===
it s really difficult to help you without knowing EXACTLY which code you are NOW using. Please put it completely: from some of your posts i can guess that you have made changes, sometimes i know what they were, sometimes i dont know if you have only tried something then left for another change and so on. As for the last post eg, i dont understand why you are unregistering when the device is not found...
I did not change anything, just made some tests, What I am trying to do now is on the last post, I was just doing some tests that is why it is false, but either it its true all false, it does not work properly. Need to create the whole code for P3 to work like the old one in P2. I already made tests and comparison and it all directs me to the last posted code.
This is the P2 Code:
If we could only find an equivalent code for P3, I think the code will already be finished (especially the " unregisterReceiver(myDiscoverer); " part)
After a lot of experimental coding. I think there are no problems regarding the codes, but only different responses of the GUI when the android device back button and home button is pressed. I will be doing a couple of hardware testing and will be posting the results.
Tests already finished, indicated are the observations.
P2 codes when executed with or without the unregisterReceiver(myDiscoverer); have the same behavior. So since problems are encountered when this line of code is added, I decided to remove it.
Response of the P2 and P3 codes when the back button and home button is pressed. P2 hides on the background and disconnects connection with the bluetooth. While P3 hides on the background while still playing the application. This is the reason why when P2 is placed on background, it re-executes / reconnects to the target device. While P3, while placed on the background, does not connect to the target device when the application is re-executed. You need to close the application running in the background before re-executing the application.
And since the "unregisterReceiver(myDiscoverer);" is not used anymore, I also removed lines like "Activity act;" and "act.unregisterReceiver(myDiscoverer);" from the coding.
Changes made on the P2 codes to be executed in P3.
Indicated imports are already removed from the coding, due to the reason that this imports are already hard coded in P3.
private static final int RESULT_OK = 1; -- is added in the group of codes.
protected void was changed to public void in the indicated line
public void onActivityResult(int requestCode, int resultCode, Intent data) {
"this.getActivity()" was added in the indicated lines
Now code can already be used to create your own GUI apps, for bluetooth communication with an Android Device and arduino.
@myrds=== P3 continues to run in background because you don't unregister... which is against the android principles; that s why i have told you unregister in onPause()
Like I said before, I am not very good at coding bluetooth, please feel free to do the necessary corrections, and I don't know how to unregister it efficiently.
If anyone knows a solution regarding the unregistering of bluetooth codes when application is placed on the background, please post your codes and I will test if it would work or not. Thank you.
@myrds===
something like that:
for testing hit the home button.
There was an error, but this is what I did:
Error: unexpected token: public
I also tried this:
and this:
after I press home or back, it does not play on the background either, it even does not reconnect to the target device.
@myrds=== you cannot use the code i have send (I have tried and it works) as you have done . OnPause is a method from the android lifecycle....
It seems we won't be able to unregister it using processing 3 and just let it run on the background than letting it unable to reconnect when home or back is pressed. I think this is a consequence when created with P3. Either or, everything is working as it should be.
I think this answer all the questions, thanks for the help guys, specially akenaton for all his contributions, atleast we are able to run and make bluetooth communicate with the device.
@myrds=== can you post the final code with my onPause() method?
I already posted all 3 codes, just scan everything from above.
@myrds==== where is the onPause() in these codes you put???? - I can see it INSIDE your code but NOT as the onPause() method from android lifeCycle.
@akenaton === can you post how should the onPause() be inserted on the codes?
@myrds=== add it as a separate method, exa
https://forum.processing.org/two/search?Search=onpause
onPause will be similar to onCreate or onResume functions, all part of the app's life cycle.
Kf
My apologies, but I do not now how to insert in on the P2 codes or in the current P3 codes, that is why I was requesting for you to post the codes for me to be able to understand better.
But I will still try to review the link you sent me :)
The code still does not work in P3 guys, even if I create the codes in a different method. I think that the only method that the developer can do, is to give a warning or instruction to the user regarding the running of the application in the background.
Also for those who are trying to access the files in the google drive, the files will not be accessible anymore. This is due to the reason that the code use there is in P2 and already obsolete.
By the way guys this is the error. The function onPause(), does not exist. Or guys, do you know any code that I can use to prevent the application from playing on the background?
Guys observation problem, I was just able to observe it properly today, I think there is no problem using the pause or unregister using P3. This is currently my observation.
When I play the app, it opens the app and runs properly. And when I press the Home button ans re-access the application, either I re-access it from the backround or reopen application from its icon, it re-opens and runs properly.
When I play the app and runs properly, pressing the Back button will place the application in the background, but when I re-access it, either from the background or re-opening application from the icon, the application tries to reconnect to the device, but since the device is already connected, it won't be able to detect it.
In conclustion, I think there is no more problems with unregistering the device, the only issue left is the response of the application when the back button is pressed.
Do you have sample codes that I could use that closes the application when the back button is pressed? I think this is a good solution on the issue regarding the pressing of the back button.
When you unregister the application, do you do it onPause? Maybe you should do it onStop instead. I am interested to work with your code but I have my hands tight doing some other work and unfortunately I won't be able to try your code but until the weekend, not suggesting I am an expert but more to try to figure it out how to get it run properly.
Kf
@ === kfrajer I am trying any suggestion, you can suggest any solution anytime you can, a lot of problems already fixed.
I also apologize specially to @akenaton, it took a while for us to solve the unregistering, wherein I was able to make some bad observations, I generalized the problem involving the home button, but the problem was just with the back button all along.
Are you unregistering right on the onPause() function?
Kf
I just tried using this codes:
and
I was able to run codes without any errors, but still the same problem after pressing the back button. Still could not re-access/reconnect with bluetooth device.
I think the only solution is to close the application after pressing the back button.
@myrds===
be sure that it is unregistered adding some log or println to your on pause (if myDiscoverer == null)....
try to register in onResume()
@akenaton ===
Can you post the whole code, I am also currently researching on how to make the backbutton function like a homebutton.
@myrds===
I tried onPause() and onStop(), but it does not re-access the app, I tried onResume(), but the app automatically stops.
I was able to find the solution, atlast case closed.
I added this code and it worked, but instead of just re-accessing, it re-connects to the device and accesses it.
By the way guys, since the coding here using bluetooth with android is already ok, does anyone know how to do some coding using wi-fi with android?
@myrds===
when you call onPause() you unregister either with the home or back button.
what have you tried with onResume()???? - Put the code you used. It s here that you have to register again, testing of course wether your discoverer is == null.
it must work (i have tested and got all the messages that it works)
as for System.exit() it s a bad idea, and why 1 (which means "an error occurred); why are you not using "finish()"?
I tried using finish(); but P3 detects it as "the function does not exist", I just copied the "1" and tested it.
I already posted onPause or onResume, it did not work, please post the whole code so that I could check.
But I prefer using the onBackPressed coding, it is more efficient, if you could give me a code better than System.exit(), then I will use it.
By the way, I just tested using System.exit(0); it also works.
@myrds===
System.exit(someInt) works with 1,0,-1 (and other values i think); these values do not change anything. as for finish() i have already told you that you are in a fragment!
this getActivity().finish()
as for onResume() i repeat::: what code have you used which mahes your app crashing (as you said)
@akenaton ===
I used this code:
still same problem
I also tried this
Does not work either, kindly post your workable code.
But since the only problem lies on the time when the back button is pressed, I prefer using the codes with the "public void onBackPressed(" . Please post any countermeasure aside from the used of "System.exit(0);" .
Like I informed you before, I am not expert in handling the bluetooth codes. Kindly post your codes for me to easily understand the coding.
@myrds===
as for finish() are you getting "finish() does not exist" as you got before??? - i dont think!
as for on resume you try to unregister; and it is wrong, you have to REGISTER and the best way could be to create some method for that in order to call when onCreate is called or when onResume is called (for registering IF your receiver is null)
i dont know wether you know how to code for bluetooth but till you dont look at the android lifecycle you cannot code for android.
When you call finish() the app is firstly onPause() then onStop then onDestroy() so your receiver is null; when you comme back (without having called finish() and without your backPressed() method which has not any reason to be called) onResume is automatically called.
I have tested your code many times (without the arduino but it does not matter) added what is necessary for fragment, added the onPause() and onResume() methods and when you hit the home or back button and your app is not in the foreground receiver is null but when i come again receiver is here.
For the finish(), used for fragment, I was able to run it but same problem. For the use of onPause(), onDestroy(), onResume, etc, kindly post the whole code and I will test it.
If there no more codes to be shared, let us stick with the code that works with arduino.
For further improvements you want to do with the code, just post your code and I will test it and I will post feedbacks with regards on the testing. Or if you are capable of testing it as well in your end that works with bluetooth connection with arduino, then much better.
Guys, I've been reviewing akenaton's posts, and I was wandering why does some of the codes work on his end, but does not work in my end. Now I was able to detect the problem. When I used P2 before, P2 created android manifest, and when I used P3, the code, suddenly relies some of its process on the android manifest.
So after I made a new code and tested it, it does not run it on the other code, wherein connection losts. So I tried to delete the android manifest on the P3 code we are using here in order for it to create a new android manifest when we run the program. And now GUI automatically stops. The codes automatically stops onStop, onPause etc.
Another problem, now I see why it works with akenaton's coding. But when tested here in my end, it does not.