BLE scan works from IDE (MIT AI2 Companion QRS scan) but not from APK build

400 views
Skip to first unread message
Assigned to ewpa...@gmail.com by chrisw...@gmail.com

Frank R. Nichols

unread,
Apr 28, 2019, 2:15:28 PM4/28/19
to MIT App Inventor Forum
On April 24, downloaded example Heart Rate Monitor
after reading this "karen's blog" article

Added the BLE extension to AI2

and all was well. (or so I thought).

Everything ran just dandy from my Galaxy Note 9 (One UI v1.0, Android v9, Kernel v4.9.112 (#2 Mon Apr 1, 2019) - (AI2 BLE extension 2, downloaded Apr 24)
Dandy
perfect.
Sample code above worked great and showed my BLE data.


I set about making my own project, based on what I'd learned.
Getting data via the BLE on my own Arduino project which reads a couple of buttons instead of my heart - and passes the data byte using the HRM GATT. - and that worked too.

Wonderful

UNTIL I tried to run the APK .

I did "Build -> App (save apk to my computer)" and saved it to my own Google Drive.
Installed the APK on the same phone I'd been using
and it will NOT find my Arduino.

Removed app from my phone
Restarted my phone
Re-installed the APK
Same ! - Program runs, screen/UI looks great, buttons work and everything - but it will NOT find my Arduino during SCAN

Step 2
Went back to AI2 over WiFi
and it WORKS!

Step 3
Went back to original example code from Jose Luis Núñez (karen's blog guest blogger)
Works fine over WiFi from AI2
His 2016 APK works fine (with warning when I load it to my phone that it was for an "Older Version" of Android)

Step 4
Download his project in AI2 via WiFi (companion)
Works fine

Step 5
Build his project "Build -> App (save apk to my computer)" and saved it to my own Google Drive.
Installed the APK on the same phone I'd been using
and it will NOT find my Arduino.

So it's not the source files, his or mine.

For some reason
1. SCAN for BLE devices works fine if loaded in development mode via WiFi using AI2 companion
but
2. will NOT work at all if build and install APK.


Any help is appreciated!

Frank in Oceanside, CA





Frank R. Nichols

unread,
Apr 28, 2019, 4:03:16 PM4/28/19
to MIT App Inventor Forum
I've also tried two more versions of BLE link 
using direct MAC address instead of choosing from list picker
which both worked fine using Companion over WiFi

They also fail if using APK
via download to my computer
via download from QRS code link on the web

The apks successfully install and run with no error messages
all screens look good
they just fail to connect with my BLE device

while continuing to work great in the non-apk development mode via WiFi while running the AI2 IDE

Thanks 

Frank R. Nichols

unread,
Apr 28, 2019, 6:35:54 PM4/28/19
to MIT App Inventor Forum
I read the "workaround" from the forums, about dragging a regular Bluetooth service into the design window, even if not using it.
Something about missing comma in the extension for permissions - while the AI2 Companion had ALL the permissions.
Didn't help
Also invoked one of the Bluetooth blocks to force the compiler to try to include the Bluetooth code
Didn't help
Every change I made ,every modification to the source was tested
In Compainion - worked fine
In APK - everything worked EXCEPT connecting to my BLE device

A sad state of affairs, indeed.

I suppose it's time to look for a different IDE?

SteveJG

unread,
Apr 28, 2019, 7:04:09 PM4/28/19
to MIT App Inventor Forum
Different IDE?   Maybe.    You might wait for comments by MIT staff or others here in the Forum to post before you capitulate. You posted four hours ago and on a weekend. Most people are otherwise occupied rather than surfing the Forums.

Android Studio always works (or does it not?)  Sorry you have issues, you are dealing with a specialized piece of hardware so perhaps just check back tomorrow or the next day and see what suggestions people who work with BlueTooth might have.   Consider posting the Blocks and or an aia from the additional Project versions you created here in the Forum since they don't really know what code you use.

Best of luck with a different IDE.  There might be an App Inventor 2 solution.  If the Project works with Companion, there is probably a reason it is not working with a compiled apk.  This might be an  issue w.r.t. your Android version or not having the Arduino paired.  If you have a second Android device, you might try that.

Regards,
Steve

Chris Ward

unread,
Apr 28, 2019, 8:27:14 PM4/28/19
to mitappinv...@googlegroups.com
Hello Frank

I understand your frustration, been there done that.

The Heart Rate Monitor example you have tried is not the best place to start, it's based on a very early version of App Inventor 2 and a lot has changed since then (including a better Heart Rate Monitor example by gerrikoio). Yet you made good progress with it, so in my opinion the difference between success and failure in this case must be something very small and subtle.

Please attach your App Inventor Project (.aia file) and your Arduino Sketch here and we can try to find the whereabouts of the needle........

https://www.professorcad.co.uk/appinventortips#tipaia   How to export a Project File (.aia) and add it to your Forum Post.

Note, Google Groups prevent a .aia file that includes an extension from being attached to a new post. However, there is a work-around. You can edit your last post and add attachments to it :) 

Edit: Frank, is your phone set to Developer/Debug mode?


Chris Ward

unread,
Apr 28, 2019, 8:36:03 PM4/28/19
to MIT App Inventor Forum
Karen Lang 

Is it possible that the new Google Android security measures are at play in this case, given that the Phone is Android v9 and the App works as expected in Companion Mode?
Message has been deleted
Message has been deleted
Message has been deleted

BodyMindPower

unread,
Apr 29, 2019, 3:55:02 AM4/29/19
to MIT App Inventor Forum

The app was created in the pre-era of runtime permissions (API<23).
So, I assume that your issues are caused by these (runtime) permissions.

 

I once took a look at the manifest (project from Jose Luis Núñez):



Something is going wrong there ...


see here: https://developer.android.com/guide/topics/connectivity/bluetooth-le


 

Anke

BodyMindPower

unread,
Apr 29, 2019, 4:07:26 AM4/29/19
to MIT App Inventor Forum
Take a look at the app's settings (APK) from your device and see which permissions have been granted (probably "none"). Therefore, nothing can be found when searching for BLE devices.

Anke

BodyMindPower

unread,
Apr 29, 2019, 6:23:50 AM4/29/19
to MIT App Inventor Forum
... did you update the BLE extension to the last version? http://appinventor.mit.edu/extensions/
It should actually work with the current version.

Anke

PS: Heart Rate Monitor: "Extension Version: 2"
current Version is: Extension Version: 20181124

Frank R. Nichols

unread,
Apr 29, 2019, 12:49:58 PM4/29/19
to MIT App Inventor Forum
yes I was using that version from the git go
Thank You

Frank R. Nichols

unread,
Apr 29, 2019, 1:07:47 PM4/29/19
to MIT App Inventor Forum
This is SUPER advice.
No, I did not look for, nor was I aware that I, as a user of the platform, could grant permissions that were not requested by the app.
I will try it right away

As I thought, the Permissions in the App Settings says No permissions required, and when I select that, I get a screen that simply says No permissions

When the Permissions screen is open, viewing the possible permissions merely shows, greyed out, "Other app capabilities"
- have full network access
access Bluetooth settings
contol vibration
(etc)


But tapping or holding on any of them, merely brings up a popup which describes them.

It apparently won't Set or Enable any of them because 
- returning to main Permissions shows No permissions
- running the app again cannot locate or connect with my BLE device

Running any other BLE scanner finds it right away.

Thanks

Frank R. Nichols

unread,
Apr 29, 2019, 1:08:46 PM4/29/19
to MIT App Inventor Forum
Thank you very much Anke
I will go read your link right away

Frank R. Nichols

unread,
Apr 29, 2019, 1:12:05 PM4/29/19
to MIT App Inventor Forum
Thanks again for the link
I've been writing for BLE devices for 5 years now
but I don't see how, in the App Inventor environment
I would have access to the manifest
?

The 2016 "workaround" (putting a regular Bluetooth client in the Design) did not help for some reason

Any workarounds you can think of would be nice

Frank R. Nichols

unread,
Apr 29, 2019, 1:14:50 PM4/29/19
to MIT App Inventor Forum
I DID find a project that did a regular Bluetooth audio connect, somewhere on YouTube
I'll try building That
and see if the APK from the builder gives me Bluetooth permissions

If that goes well, 
then I'll slowly add some BLE and see if it scans and connects


Frank R. Nichols

unread,
Apr 29, 2019, 1:20:17 PM4/29/19
to MIT App Inventor Forum
NOPE
same problem
It has BluetoothClient1 in the Design window
but when installed via APK download/install
it has the same "No permissions" in the APP settings
 :-(

Frank R. Nichols

unread,
Apr 29, 2019, 1:26:47 PM4/29/19
to MIT App Inventor Forum
I'm perfectly happy to put together a brand new project, from scratch as it were.
If anyone can tell me any workaround to get an APK that give me BLE permissions
???

I don't need to save any existing work - I'd just be happy if I could get this wonderful AI2 tool to put up a single screen and connect to my BLE device

Anyone have any ideas on how to start over?

BodyMindPower

unread,
Apr 29, 2019, 1:33:01 PM4/29/19
to MIT App Inventor Forum

so, you may have to adjust the Manifest ...

may be you have also to ask for permissions in your blocks (when something (events/functions) from the BLE extension is called). I have no experience with it.




Actually, the current BLE extension should add these permissions (except the red one) to the manifest. Whether these are then also requested at runtime, you must try.

Otherwise add


Anke


PS: Suggestion: post the aia or upload it to Google Drive and post the link

Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted

BodyMindPower

unread,
Apr 29, 2019, 2:50:12 PM4/29/19
to MIT App Inventor Forum
try the aia to check if your BLE device connects ...

the important (dangerous) permission should be asked when you open the app
Build the APK, install and try ...

Frank R. Nichols

unread,
Apr 29, 2019, 4:13:38 PM4/29/19
to MIT App Inventor Forum

(Back from 2 hour break)

Thank you, Anke.
You are the most helpful

I posted my aia last night, but think the server was down for maintenance.

You speak of tweaking the manifest.
Please tell me how I have access to the manifest ?

I will try adding your .AskForPermission first,
then get back with results. If it still fails, I will post the aia that failed.


Chris Ward

unread,
Apr 29, 2019, 4:27:59 PM4/29/19
to MIT App Inventor Forum
Hi Frank

The deleted messages - was that you trying to post your .aia? If so, read my first post.

Frank R. Nichols

unread,
Apr 29, 2019, 4:35:55 PM4/29/19
to MIT App Inventor Forum
Thank you BMP, I'll try it immediately

Frank R. Nichols

unread,
Apr 29, 2019, 4:44:00 PM4/29/19
to mitappinv...@googlegroups.com
THANK YOU

THIS WORKS !!

I can connect to my device now.
Thank you all for helping me 
So glad to move ahead with my project now.

Mark this issue as closed/complete.

(I wonder what documentation I missed that told me to include that screen initialize permission?)

Frank in Oceanside, CA

***********

For Chris Ward (many thanks)
​​
Thank you Chris,

I'm in the habit of saving 'incremental' milestones. (Been a programmer since 1980). So when I built and tested-as-an-APK and was happy that I hadn't broken anything - I saved as AIA to my computer, closed the IDE window in Chrome, and went on my break.
Came back, logged into the IDE, and opened "Blocks" to make a few more additions
Unexpectedly - upon opening - before making any changes to Design or Blocks - there were about 6 errors and 6 warnings in the Blocks.
Hovering over two of the errored blocks, a popup informed me "Delete this block - undefined" for each one.
Hmmmmmm
So I performed import project (.aia) from my computer (had to delete same project name from my own workspace first, then successful import)
and got the same errors.
Then I downloaded the extension again (and again just now from the link in your kind and gracious email) under a modified filename on my computer
performed a binary file compare
and all three .aix files are identical.

No matter

I'm going to the dentist in an hour and will start all over again from the code shared with me on the forum which has no errors, when I return.

Goal:
Link with my known device without listPicker
update UI when I receive changed byte values (using HRM services/profile) - which then update text on labels or buttons and invokes text to speech to announce the score (a little scoreboard to keep track of points earned whenever someone presses a physical button on my arduino BLE server - passed as HRM data because it's a ubiquitous profile)

My suspicions.

When I was making edits to my project, there was 'legacy' code remaining in the environment, defining blocks that I needed even though I'd deleted some during an editing session.
But when I exited the IDE, the legacy definitions were lost/wiped. Then coming back and reloading, things I needed were no longer there.

It seems I will need to exit and re-start the IDE each and every time I remove blocks (such as the code around ListPicker) - to prove that I haven't also removed something important.


I will start all over again with the aia from the forum help - which worked, and be more careful when editing, exiting IDE and re-starting for each modification until I learn my way around the "rules" for this wonderful tool.

I will now attempt to attach the offending aia 

Thanks again for your attention to this
Frank 


Connects_03.aia

Chris Ward

unread,
Apr 29, 2019, 5:19:50 PM4/29/19
to MIT App Inventor Forum
You haven't missed any documents Frank - this has only just be added in response to Google's relatively sudden introduction of new security measures. The dust has yet to settle on the changes and no doubt there will be some bumps to smooth out.

Frank R. Nichols

unread,
Apr 29, 2019, 10:25:46 PM4/29/19
to mitappinv...@googlegroups.com
CHANGED LIBRARY on me?

So I left for a meal
came back just now
and procedures that worked FINE are "not defined"

Here's the aia that worked 4 hours ago, but has many errors now
reloaded the aia
same result

Example:
call BluetoothLE1.RegisterForBytes is missing, not defined (but it's still there if I visit the IoT reference guide page)
but
the 'old?' when BluetoothLE1 ByteValueChanged came back after not being there earlier today.

Is someone messing with the libraries while I'm trying to build?

Thanks for your help.



oooops - attached aia to wrong post



​​
Thank you Chris,

I'm in the habit of saving 'incremental' milestones. (Been a programmer since 1980). So when I built and tested-as-an-APK and was happy that I hadn't broken anything - I saved as AIA to my computer, closed the IDE window in Chrome, and went on my break.
Came back, logged into the IDE, and opened "Blocks" to make a few more additions
Unexpectedly there were about 6 errors and 6 warnings.
Hovering over two of the errored blocks, a popup informed me "Delete this block - undefined" for each one.
Hmmmmmm
So I performed import project (.aia) from my computer (had to delete same project name from my own workspace first, then successful import)
and got the same errors.
Then I downloaded the extension again (and again just now from the link in your kind and gracious email) under a modified filename on my computer
performed a binary file compare
and all three .aix files are identical.

No matter

I'm going to the dentist in an hour and will start all over again from the code shared with me on the forum which has no errors, when I return.

Goal:
Link with my known device without listPicker
update UI when I receive changed byte values (using HRM services/profile) - which then update text on labels or buttons and invokes text to speech to announce the score (a little scoreboard to keep track of points earned whenever someone presses a physical button on my arduino BLE server - passed as HRM data because it's a ubiquitous profile)

My suspicions.

When I was making edits to my project, there was 'legacy' code remaining in the environment, defining blocks that I needed even though I'd deleted some during an editing session.
But when I exited the IDE, the legacy definitions were lost/wiped. Then coming back and reloading, things I needed were no longer there.

It seems I will need to exit and re-start the IDE each and every time I remove blocks (such as the code around ListPicker) - to prove that I haven't also removed something important.


I will start all over again with the aia from the forum help - which worked, and be more careful when editing, exiting IDE and re-starting for each modification until I learn my way around the "rules" for this wonderful tool.

I will now go and attempt to attach the offending aia in the forum
Connects_03.aia
Message has been deleted
Message has been deleted
Message has been deleted

BodyMindPower

unread,
Apr 30, 2019, 3:27:55 AM4/30/19
to MIT App Inventor Forum
As I said, I'm not familar with BLE, but try this (the reight one instead of the left):

and / or post your aia (upload it to Google Drive and post the link, because it is not possible to post an aia with an extension here).

Chris Ward

unread,
Apr 30, 2019, 6:58:47 AM4/30/19
to MIT App Inventor Forum
Hello Frank

but the 'old?' when BluetoothLE1 ByteValueChanged came back after not being there earlier today.

 So between the time you reported back here to say everything was working fine and now reporting that some Procedures are "not defined", did you make any changes?

There has not been an update to App Inventor this week (Help, Show Splash Screen should show release nb175b) and the BLE Extension was last updated on 24-11-2018.

To be crystal clear, the file name for the latest BLE extension is edu.mit.appinventor.ble-20181124.aix and it should be downloaded from the MIT site:

To upload your Project File here, edit your most recent post:

EditPost.png


And use "Attach a file". This will allow a .aia containing extensions to be uploaded, whereas it cannot be done when writing a new post/new reply.




Frank R. Nichols

unread,
May 2, 2019, 1:13:40 PM5/2/19
to MIT App Inventor Forum
DISCOVERED A BUG IN THE IDE?

I have proven that most of my problems stem from "right mouse click" in the Blocks work area. I corrupts the blocks !!!

I right click in the 'white' area to bring up the menu (Undo, Redo, Clean up Blocks, Download Blocks as Image, etc.) and 5 or six blocks are Instantly corrupted.

I did not select any of these pop up menu actions yet. Even left-click in white area, which causes the popup dialog to disappear, leaves the blocks corrupted and I am awash in Warnings and Errors.

Hovering over one of the new Error procedures I get a message like "Delete this block - not defined".

Huh? It was just fine 20 seconds ago. In fact the code was running!

I am using a 64-bit Dell i7, with Win 8.1 in Chrome Version 73.0.3683.103 (Official Build) (64-bit).

The good news is, if I've saved the aia to my local drive, I can then 1) delete the corrupted project, 2) reload the aia, 3) avoid right clicking in the Blocks work area - and all is well again.

Thoughts?

Chris Ward

unread,
May 2, 2019, 2:06:19 PM5/2/19
to MIT App Inventor Forum
Hello Frank

This should really be a new Forum Subject, but hey ho.

My first thought is that it is somehow something to do with your set up or your Project File. I can't reproduce it here and usually if a bug as obvious as your report crops up, we get a lot of posts about it in a short space of time.

This may prove difficult to trace. You can help in the first instance by:
  • Capturing a screenshot (only use keyboard Print Screen in this case) so we can see what you see.
  • Telling us what you actually mean by corrupted Blocks - how would you know? Is the image distorted in some way?


Chris Ward

unread,
May 2, 2019, 2:09:52 PM5/2/19
to MIT App Inventor Forum
....let's not forget that the IDE is working via a remote Server and there are thousands of Users at any one time. So your Broadband bandwidth needs to be in good shape, as indeed does your Browser.

Frank R. Nichols

unread,
May 3, 2019, 2:35:34 AM5/3/19
to MIT App Inventor Forum
Thank you
I will start a new topic
and show the corruption
which, by the way
occurs when loading a perfectly good AIA from my local computer
Reloading it cured the problem
so some interaction with browser? I'm 22mpbs download, See attached SpeedTest.jpg

Here are screenshots of the SAME project
"One" is prior corruption
"Two" is corrupted without ANY editing or moving of blocks by me.Hovering over an error block the popup tells me to delete the block because it is "undefined".
Never touched Design window, never touched or moved or inserted any block in any way.
Two causes I've found so far:
1) right click in white area of Blocks screen
2) sometimes corrupted upon opening the IDE (right  after logging in). Fix: reload the saved AIA from previous session.

I CAN LIVE WITH this (above problem) - It's just a nuisance. I've dealt with worse in my career.

However...
My ORIGINAL problem remains

Thanks to Anke, I will put a shared Google Drive Link here
He told me not to post files here which contain extensions
so I created a shared drive folder which contains:

Two AIAs
"FORUM..." connects to my device with either Companion or APK - all is well.
"HardCoded" connects to my device, always, with Companion....but NEVER with APK

Same problem I was facing when I started this thread.

Thanks you guys !!

No rush.

I have put 


SpeedTest.JPG
One.JPG
Two.JPG

Chris Ward

unread,
May 3, 2019, 6:19:38 AM5/3/19
to MIT App Inventor Forum
Hi Frank

Your before and after screenshots make the issue very clear. Never seen that happen before and I'm wondering if the .aia is in fact corrupted in some way. If you can post that, I can have a look (within the .aia, which is in fact a collection of files).

Something you can try when the issue occurs:

If for example you are looking at the Blocks for Screen 1, reselect the screen. This should perform a refresh:

ReselectScreen.png


Chris Ward

unread,
May 3, 2019, 6:21:14 AM5/3/19
to MIT App Inventor Forum
By the way, I think the name Anke is of Dutch or German origin, and is female. 

Chris Ward

unread,
May 3, 2019, 7:13:17 AM5/3/19
to MIT App Inventor Forum
Hi Frank

So, the "Forum" version works, but strangely, it has to split the BLE address found:

ShouldNotSplitAddress.png


Normally, you would not have to do that, the List selection should comprise of only the Device Address. So:

  • What is Index 2, Device Name or something? 
  • Did you try BLE Connect With Address without splitting the List value?

Given that this version works, I think there might be a non-displayable character in the original Address, which is retained when the Split is performed but lost when hard coded in your other Project.





Chris Ward

unread,
May 3, 2019, 7:20:01 AM5/3/19
to MIT App Inventor Forum
Back again :)

Instead of hardcoding the address, you could:
  • Get the BLE Device list, but populate a (Blocks) List instead of asking the User to pick via a ListPicker (you wish to avoid that, right?)
  • Lookup the device in the List via a For Loop, using the "contains text" block to find the required item
  • Split at spaces as before

ABG

unread,
May 3, 2019, 11:38:55 AM5/3/19
to MIT App Inventor Forum
Comparing images One.JPG and TWO.JPG,
it looks like the BLE extension had been removed from
the .aia file.

With the recent complaints about Chrome and Google Groups
disallowing extensions in posts and downloads,
have you tried working with
any other apps that have extensions?

Maybe this is something in your
development environment that is
trying to remove "unsafe" code?

ABG

Frank R. Nichols

unread,
May 3, 2019, 7:51:40 PM5/3/19
to mitappinv...@googlegroups.com
Thank you
Perhaps
I can live with it
but it's very annoying
I might try another browser soon.

Reloading my aia from my local computer seems to cure it most of the time.
Here's what I've noticed:
  1. upon opening the IDE for the first time, the 'current' project automatically loads from my 'projects' folder on your server
    1. sometimes it's fine
    2. sometimes it's corrputed
  2. upon right clicking on white space in the Blocks working area
    1. sometimes it brings up the menu okay
    2. sometimes it corrupts the blocks as I've shown you in those jpg images
To me, this doesn't seem like a 'security' daemon blocking anything - because it's inconsistent.

Thanks again for any ideas you come up with.


To answer your question: BLE is the only extension I've tried so far

Frank R. Nichols

unread,
May 3, 2019, 7:53:34 PM5/3/19
to MIT App Inventor Forum
Oops.
Sorry, Anke.

Entschuldigung für meine schlechte Annahme.

Frank R. Nichols

unread,
May 3, 2019, 7:58:31 PM5/3/19