2017년 6월 21일 수요일

Bluetooth app issues when installed as .apk (runs flawlessly on AI Companion)


Because nothing seems to be wrong on the background, im desperately calling out to hear your opinion:

I made an app that talks to an arduino via bluetooth, it consists in 3 screens: 

- Screen 1: Main screen, welcome image. Tap image and takes you to screen 2.
- Screen 2: Log In screen. Type down your username and password and you'll be taken to the 3rd screen.
- Screen 3: It initializes connecting via bluetooth to an arduino. If you log out from this screen, it takes you back to Screen 1, and disconnects the bluetooth.

Now the issue:

When i run the app through the AI Companion, it works nice and smooth. No random bluetooth disconnections, no funny error messages or whatsoever.
BUT!! When i export the apk file and install it on my device, weird things start to happen: 

1) When im on Screen 3 (connected to the Arduino), the app suddenly crashes (or something), and an alert pops up saying: Error 515: Not connected to a Bluetooth device, taking me back to Screen 1 (and leaving the bluetooth module with the "connected" light steady on, as if it was still connected)
2) When im on Screen 1, which by the way has almost nothing, (just 1 block: "When canvas1.touched-> take me to Screen 2 there is a kind of loop, that every 2 minutes refreshes the screen, popping the same 515 error, and then showing an alert that is supposed to be shown on screen 3! And this goes on over and over again, how is this be even possible? How 2 screens apparently are still "connected"?

These being said... what do you guys think that the problem might be (qualitatively speaking of course). Why is it possible that the app runs ok on the companion but not on the device.

Hope to hear your opinions, thanks a lot everyone!

--
A huge guess is that when something causes screens to change, you use the Bluetooth connection.  Are you aware when you change from the screen that has the Bluetooth client to another screen, the bluetooth auto-disconnects?      The possible remedy is to use only a single screen with the Bluetooth; then use vertical or horizontal layouts with your controls in them and use them as virtual screens.  That keeps the app at all times on Screen1 and probably will allow you app to work.

Try some blocks Wilfred.  You did not post a screen capture of your blocks, so the people here do not know what your code looks like, so the huge guess.

--
Thanks for your quick answer! (or the huge guess, everything is useful and that's what i need, because the issue im having is quite odd.. the fact that the app automatically leaves screen 3 to go to screen 1 -supposedly due to a "connection problem"-, but then at the same time it shows alerts that are supposed to appear in screen 3 only), and overall, the fact that the app works fine when i run it through the AI Companion..

 I didn't post the screen capture of the blocks because so far its a mess.. but i do have one "disconnect procedure" when i leave the screen with the bluetooth connection. (ill post them now, they are half in spanish and half in english though :/ ). 




--
never use the block open another screen together with the value "Screen1" except you are using Tribbelhunter's method
to find out more about the Runtime Error, you can use Logcat

I normally use Eclipse and Logcat there, but if you have installed the App Inventor Software (see also Installing and Running the Emulator in AI2), you already have everything you need to use logcat...

How to use Logcat

1. connect your device using USB with your computer
2. in File Manger go to the App Inventor directory, which is  C:\Program Files\App Inventor or similar
3. press Shift and right mouse click the subdirectory commands-for-Appinventor to get the context menu
4. select "open command window here" and you will get a command window of that subdirectory
5. enter adb logcat and the logcat will start running

To copy your log, right click, click "select all" and enter to copy the complete log into the clipboard, then open Notepad and paste it using ctrl-v.

--
Thanks a lot for your help, looks like that did the trick. The bluetooth is no longer crashing as i close Screen 3 after disconnecting from the bluetooth. The strange thing is, that when i close the 3rd screen, it goes back to screen 2 but after a couple of seconds, the app crashes and closes. So i tried closing Screen 2 right after it opens screen 3, so that when i close Screen 3, the only remaining screen would be Screen 1, but i get the same problem, after closing Screen 3, it goes back to Screen 1, and after a couple of seconds, that app stops. 
Any idea why this might be happening?

--
are you now using Screen1 as manager screen as recommended?

It would really help if you provided a screenshot of your relevant blocks, so we can see what you are trying to do, and where the problem may be.

--
Thanks again for your good disposition and kindness. 
I am using Screen 1 as manager, but now that i think about it, there's 1 process i might be doing wrong, but i wouldn't know how to fix it.

Im attaching a diagram with my logic rout.

From what you are saying, im guessing that to go from Screen 2 to Screen 3, I should go through Screen 1 first  (2 --> 1 --> 3). (is this the problem?)

But! when i go from Screen 2 to Screen 3, im exporting some variables that im using in Screen 3, so i would need to go through screen1 and then to screen3 and take those variables with me, but then what would happen when I only want to go from Screen2 to Screen1, due to the timeout timer? (Im i being clear enough?)






--
From what you are saying, im guessing that to go from Screen 2 to Screen 3, I should go through Screen 1 first  (2 --> 1 --> 3). (is this the problem?)
yes, exactly

But! when i go from Screen 2 to Screen 3, im exporting some variables that im using in Screen 3, 
in Screen2 use TinyDB to store these values and read them again in Screen3

also you should learn to work with lists...

--
Perfect!
I will try that tomorrow. 
Thanks again for everything Taifun, i'll let you know how did it go!

--
Hi Taifun, how are you?
So far going OK with the TinyDB and Lists, but i would have no idea on how to make the following:

How can the code know, when you are coming back from Screen2 to Screen1, if you are doing it as a stop in order to open Screen3 immediately after, or if you are coming from Screen's2 timeout timer so to stay in Screen1? Can any kind of "mark" be used when closing screen2, so that Screen1 know if you have to stay there of if you have to go to Screen3?

--
you could have learned this in my manager screen example
for your convenience let me add that link again https://puravidaapps.com/manager.php

Screen2
if you use the close screen block, you will be back in Screen1 and the Screen1.OtherScreenClosed event will fire

but if you use the close screen with value "Screen3" block instead, you will be back in Screen1 and again the Screen1.OtherScreenClosed event will fire. And there you can check, if the provided result is empty or something, e.g. "Screen3", and if it's something then open that screen instead of staying in Screen1...

so what about studying that example a little bit and also learning about the limitations of the companion app as mentioned there?

--
Thanks for your patience, and im really sorry for not paying enough attention to your tutorial. After your clarification i went back to the tutorial and it really is all there, my bad.

I did all as you said, my Screen1 is the manager now and from Screen2 i can now successfully go to Screen1 or Screen3 depending on what you do, but i've been struggling with a problem since yesterday: when i go from Screen3 to Screen1, after a few seconds on Screen1, the app crashes and closes. (pops message: Unfortunately, MyApp has stopped). I've tried different things to avoid this, like closing Screen3 with value "Screen1" to force the app to stay in Screen1 after closing Screen3, or keeping the app busy by telling it to do a dummy loop on Screen1 after Screen3 is closed, but nothing. The app keeps closing every time. The weird thing is that the app has no problem when closing Screen2, it goes back to the main screen normally.

Any thoughts?






--
 I've tried different things to avoid this, like closing Screen3 with value "Screen1"
this would open the already opened Screen1 again, which would be stupid

The weird thing is that the app has no problem when closing Screen2, it goes back to the main screen normally.
probably there is a clock component in Screen3 you forgot to stop before closing Screen3?

to find out more about the Runtime Error, you can use Logcat

I normally use Eclipse and Logcat there, but if you have installed the App Inventor Software (see also Installing and Running the Emulator in AI2), you already have everything you need to use logcat...

How to use Logcat

1. connect your device using USB with your computer
2. in File Manger go to the App Inventor directory, which is  C:\Program Files\App Inventor or similar
3. press Shift and right mouse click the subdirectory commands-for-Appinventor to get the context menu
4. select "open command window here" and you will get a command window of that subdirectory
5. enter adb logcat and the logcat will start running

To copy your log, right click, click "select all" and enter to copy the complete log into the clipboard, then open Notepad and paste it using ctrl-v.

--
There were indeed a couple of clocks that i wasn't stopping, but that didn't solve it. 
I run Logcat and the last lines of report (when the app actually crashes) are the following:

E/AndroidRuntime(  267):        at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(  267):        at android.app.ActivityThread.main(ActivityThrea
d.java:4627)
E/AndroidRuntime(  267):        at java.lang.reflect.Method.invokeNative(Native
Method)
E/AndroidRuntime(  267):        at java.lang.reflect.Method.invoke(Method.java:5
21)
E/AndroidRuntime(  267):        at com.android.internal.os.ZygoteInit$MethodAndA
rgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime(  267):        at com.android.internal.os.ZygoteInit.main(Zygot
eInit.java:626)
E/AndroidRuntime(  267):        at dalvik.system.NativeStart.main(Native Method)


W/ActivityManager(   61):   Force finishing activity appinventor.ai_echaurren.Lo
nchera9Agosto/.Screen1

D/dalvikvm(   61): GC_FOR_MALLOC freed 12380 objects / 612144 bytes in 85ms
W/ActivityManager(   61): Activity pause timeout for HistoryRecord{44076f00 appi
nventor.ai_echaurren.Lonchera9Agosto/.Screen1}
D/dalvikvm(  166): GC_EXPLICIT freed 7400 objects / 418752 bytes in 65ms
W/ActivityManager(   61): Activity destroy timeout for HistoryRecord{44076f00 ap
pinventor.ai_echaurren.Lonchera9Agosto/.Screen1}

What should i be looking for here? 

--
the first part of the error is missing, you should look for  FATAL EXCEPTION and the following lines

--
Found it!

E/AndroidRuntime(  473): FATAL EXCEPTION: main

E/AndroidRuntime(  473): java.lang.IllegalArgumentException: View not attached t
o window manager
E/AndroidRuntime(  473):        at android.view.WindowManagerImpl.findViewLocked
(WindowManagerImpl.java:355)
E/AndroidRuntime(  473):        at android.view.WindowManagerImpl.removeView(Win
dowManagerImpl.java:200)
E/AndroidRuntime(  473):        at android.view.Window$LocalWindowManager.remove
View(Window.java:432)
E/AndroidRuntime(  473):        at android.app.Dialog.dismissDialog(Dialog.java:
278)
E/AndroidRuntime(  473):        at android.app.Dialog.access$000(Dialog.java:71)

E/AndroidRuntime(  473):        at android.app.Dialog$1.run(Dialog.java:111)
E/AndroidRuntime(  473):        at android.app.Dialog.dismiss(Dialog.java:268)
E/AndroidRuntime(  473):        at com.google.appinventor.components.runtime.Fus
iontablesControl$QueryProcessorV1.onPostExecute(FusiontablesControl.java:951)
E/AndroidRuntime(  473):        at com.google.appinventor.components.runtime.Fus
iontablesControl$QueryProcessorV1.onPostExecute(FusiontablesControl.java:765)
E/AndroidRuntime(  473):        at android.os.AsyncTask.finish(AsyncTask.java:41
7)
E/AndroidRuntime(  473):        at android.os.AsyncTask.access$300(AsyncTask.jav
a:127)
E/AndroidRuntime(  473):        at android.os.AsyncTask$InternalHandler.handleMe
ssage(AsyncTask.java:429)
E/AndroidRuntime(  473):        at android.os.Handler.dispatchMessage(Handler.ja
va:99)
E/AndroidRuntime(  473):        at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(  473):        at android.app.ActivityThread.main(ActivityThrea
d.java:4627)
E/AndroidRuntime(  473):        at java.lang.reflect.Method.invokeNative(Native
Method)
E/AndroidRuntime(  473):        at java.lang.reflect.Method.invoke(Method.java:5
21)
E/AndroidRuntime(  473):        at com.android.internal.os.ZygoteInit$MethodAndA
rgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime(  473):        at com.android.internal.os.ZygoteInit.main(Zygot
eInit.java:626)
E/AndroidRuntime(  473):        at dalvik.system.NativeStart.main(Native Method)

W/ActivityManager(   60):   Force finishing activity appinventor.ai_echaurren.Lo
ncheraSinBluetooth/.Screen1

What do you reckon? Can you tell by this where the bug is?
Cheers!!



--

댓글 없음:

댓글 쓰기