2014년 11월 27일 목요일

Interest in developing BLE (bluetoooth 4.0) support / boston area dev(s) for a weekend hacksession?


I'm interested in immediately working on bluetooth low energy (bluetooth 4.0) support in appinventor. Would anyone in the Boston area already familiar with the app inventor source be interested in joining me in a weekend hack-session in a classroom at MIT or at my workplace (p.irateship.com) sometime soon? I could potentially provide food in exchange if it's just 2 or 3 people including myself. If it's more, we could all chip in for food!

We're developing low-cost BLE robots* for classroom and individual use (in STEM education) and want to be able to teach programming without the immediate steep learning curve of jumping straight into text-based android programming. Learned about appinventor recently from a teacher who's been helping us and it seemed like a great potential solution to this dilemma.

*http://gfycat.com/LightAlertArthropods here is a gif of one robot following a shape you draw on the screen. sorry about the bad video cropping with my face in it :P Maybe one day we can do multi-robot formations using the webcam on the smartphone instead of a USB webcam, but I don't expect that to be programmable in appinventor :)

LightAlertArthropods

--
I know near to nothing about BLE, but can help you out with the AI side.

How familiar are you with the Android SDK and with the App Inventor sources?

I won't be available the new two weekends, but we could arrange some other time to meet at some point this week (probably not next week) if that works. Let me know. 

--
Have you made some developments for  bluetooth low energy (bluetooth 4.0) support in appinventor, I'm a Portuguese teacher and I also interested on this.

--
I think the request for support of SensorTag could be a good entry point. I have started a design document: AI2 BLE component

--
can you please allow for comments in the document?

Why an extension to the Web component instead of a new one?

-- 
The web component was a misunderstanding from my side.
Commenting is enabled, happy to give write access.
--
Thanks; comments are good so we can keep the convo going! :)

--

Let's fire this topic back up!  Anyone smart enough to develop the BLE components for AI2? 
What happened with the weekend hack-session? In progress?  Next time I'm up in Boston I'd love to participate.  Lot's of cool BLE projects arising with the wave of BLE sensors and innovations like the BLE robots described below.... there has to be a BLE AI2 geek out there somewhere wanting to impress us all....
If I can help. I want to support.  Someone mentioned funding the interface?  How much would we be talking here to fund the BLE components?  I've read enough interest online that I bet we could crowd source a good amount of money for the effort.

--
Getting things started:
So, first we need to go here: http://appinventor.mit.edu/appinventor-sources/ 

I'm going to look into how we can put up an instance of appinventor and a buildserver running the code, so we can try it out after our awesome Appinventor BLE Hero steps up to the challenge!?              
⦁ Objective number #1 for a BLE AI2 Appinventor Component: Allow a client or peripheral to read the BLE broadcast advertising custom 31 byte message being sent from a Central (server) BLE device!  iBeacon inserts a custom payload of data in this 31 byte advertisement using the Manufacturer Specific Data Field. 

⦁ Note, Objective number #1 is pretty straight forward for a first cut BLE app inventor component, but differs greatly from Classic Bluetooth because it doesn't require a paired connection before receiving at least 31 bytes of advertised data which is different than the current Appinventor Classic Bluetooth client component which only list the MAC address and Name of paired Bluetooth devices before connecting.

Here is a REALLY awesome educational link for those joining this initiative to learn the fundamentals behind BLE.  https://learn.adafruit.com/introduction-to-bluetooth-low-energy/gap

Introduction to Bluetooth Low Energy

--
There is some code on github, but I failed to get my dev environment setup.

--
Oh, I think I did also something on the API. Hm, have to check why I haven't committed it.

--
That's awesome! I'll try and setup a working environment with your code and if you have anything for the API. Let's take a look at it!

--
I am also working on similar project. It is great to see this thread.

--
I sourced Müffke github appinventor project into a Google server instance I paid for last week.

So, I have our own google server AI24BLE.

But, I can't seem to build Muffke's version of the project on google server.

If anyone knows how, send me your gmail and I'll add you to the google server account I created for this project.

Müffke, any progress or updated to your github source code will be sourced from the github link you provided.

--
I was looking through the Android API, and starting with API 18 (Android 4.3), you get the native support for Bluetooth LE.  I have been able to successfully download the app inventor source, and compile locally on my linux machine, but it defaults to Android 4.2.2.  Does anyone know if you can easily upgrade ai2 to use the API Android 18/19, etc?   I think it will make the BLTE development much easier. 

--
you can certainly change the tools, but note that there are two things
you would need to do to work with native support:
1) update the SDK tools: you can do this by modifying build files and
adding the new tools. Right now the 4.2.2 tools live at
appinventor/lib/android/
2) when writing components that don't work on lower APIS, it's a good idea to both handle older phones graciously, and abstract your code in a way that it becomes obvious that certain functionality is only
available to devices on higher API levels. This document explains how
we do that: Supporting older devices while using newer versions of the
Android SDK in App Inventor
(Supporting older and newer devices in App Inventor)

It might also be a good idea to modify certain things in the manifest
(like a target SDK maybe?). You can do in the class Compiler.java
(https://github.com/mit-cml/appinventor-sources/blob/master/appinventor/buildserver/src/com/google/appinventor/buildserver/Compiler.java)

Please keep us posted on your progress! 

--
I actually was able to switch to 4.3.1 android.java (downloaded the SDK, create a new lib/android_4.3.1 and remapped the various build.xml files), and successfully build a small app for my 4.4.1 android device.  I was able to "see" the BluetoothManager class in the app, but I haven't been able to do much more than that. 

My real goal is to be able to connect to this device:

It's a small BTLE device that can return temp/humidity/accelerator/compass data.  Ultimately, i would like to host this building with a "WICEDSense" component so that you can easily attach and query this standalone device, and really start building some cool apps for classrooms, teachers, etc.  I think it's a stepping stone to a bigger BTLE development...

I'm getting tripped up now on the callbacks required to run bluetoothAdapter.startLeScan(BluetoothAdapter.LeScanCallback).  In tracing the code, it seems that in the Component runtime files, you can't really see the android headers, and you need to use Reflection to get access to methods?  

Are their another components that are similar so that I can see the examples?   I've got all the changes in my local git clone, at some point I can push them into branch if people are interested..

--
Awesome job!!!  Would there be any benefit in having you try using my Google App Server I paid for this project? Named AI24BLE.
I'm curious how hard it would be to create an ai2 component for the first BLE step to passively listen for a UUID LE advertisement non-connectable.
The components would allow user to set scan intervale time and get UUID list of scanned LE only advertisements.
I'm still not 100% positive what we would listen for in the Bluetooth announcement to distinguish a LE advertisement from a classic Bluetooth advertisement?

--

I'm using this as a guide, I think you can either search for BT, or BTLE, but not both at the same time.  

It's a bit early to share the app itself, since it doesn't do anything, but I can share the source to get Android 4.3.3 compiling. I'm a relatively novice GIT user (git add/commit locally is fine), but Forking and creating branches on github could be dangerous for me. 

--
That actually seems very straight forward.  I was unwrapping the actual 0x0008 Advertising byte structure and thinking we would have to dissect a scan result or right a scan function from scratch.  It looks like the basic work to scan for BLE is done!
Appinventor components might look like:
CALL StartLEscan
CALL StopLEscan
GET BLE_ScanResults.deviceList [Manufacture ID,UUID, , Major and Minor UUIDs, RSSI]

--
So using the example component, BluetoothClient.java, I don't see how you can easily define the callback required for the LeStartScan() method. 

In every case, when connecting to the Android class BluetoothAdapter, it's done through the utils/BluetoothReflection.java adapter:
...
Object bluetoothAdapter = BluetoothReflection.getBluetoothAdapter();
...

In the file BluetoothReflection.java,  you define the class as generic, search for it at runtime using Class.forName, and then apply the methods through the invokeStaticMethod().  It appears to me that the AI components can't see the headers of the Android SDK to be able to use them directly. 

--> $1mil question,  does anyone have examples how to use reflection to get into the callbacks, and other methods. 

  public static Object getBluetoothAdapter() {
    // return BluetoothAdapter.getDefaultAdapter();
    Class bluetoothAdapterClass;
    try {
      bluetoothAdapterClass = Class.forName("android.bluetooth.BluetoothAdapter");
    } catch (ClassNotFoundException e) {
      // Bluetooth is not available on this Android device.
      return null;
    }
    return invokeStaticMethod(getMethod(bluetoothAdapterClass, "getDefaultAdapter"));
  }

--
I have done it in my github repo

--
Between David and Friedger there seems to be a serious BLE appinventor solution brewing!  I wish I knew how to help, but for now I'll keep acting as cheerleader and see if we move this thing along.

David, did you want to try and get the server working on Google Application Server?  I'm not sure how easy that is to install... I sourced Friedger's code from github and couldn't figure out how to build it, so I'm guessing it might not be that easy.

Keep up the good work.  When you are ready, I'd love to be the first to test BLEscan and read my BLE advertisement UUID, which I finally got working today using raspberry pi as a BLE beacon!

--
Sure thing. I have the LeScan callbacks at least compiling, but the app crashes on my phone for now.   Still trying to ramp-up on Java, AppInventor and BTLE all at once...

--
A quick update, I was able to get some additional parts of the code working, and build a working Android App with BLTE:

⦁ BTLE scan successfully lists all the devices available
⦁ Connect to  BLTE with the BluetoothGatt client
⦁ Read the device RSSI through the BluetoothGattCallback @override   onReadRemoteRssi()

So basically, I think the flow-flush is there, and it's just a matter of implementing many of the BLTE callbacks, and discovery modes. I think it's not *really* stable, and needs some effort to deal gracefully with onPause(), onStop() methods, and checks for errors in the code, Android Intent, etc... 

I guess the next step is that I will fork the code, and upload something to Github. Give me a little bit of time (I need to get back to my day job for a little bit..)

--
I am happy to take on where you left it..

--
Kudos to David!!! Great job. Friedger, what do you need from David to continue where he left off?

David, does the LEcallbac list BLE advertised devices UUID?

--
great work. I want to support this idea and contribute from my side.
Is there any source code which I could integrate into WICEDSensePRO (my App)? Please see on GooglePlay:
https://play.google.com/store/apps/details?id=com.broadcom.app.wicedsensepro

WICEDSensePRO

BTW:
There are two standards for doing such one: IPv6 over BT-LE, IETF and GAP REST API (also called RESTful API).
I like the REST, very simple, similar to HTTP "get" and "put" methods.
When implementing, extending MIT AI 2 - could we go this way, with REST API?

It would give great benefit: very generic and many cloud services support and use REST. So, such a BLE device can be integrated easily into other Apps, e.g. MIT AP 2 or cloud solutions.

What do you think about REST API?
What is the detailed approach of your solution?

--
I finally got everything compiling nicely, and I've forked the AI source over to here


All the compilation instructions are the same, and it includes:

⦁ Android 4.3.1
⦁ BluetoothLE Component
⦁ WICEDSense Component

For now, both WICEDSense and BluetoothLE are the same component, but I plan on making WICEDSense specific to the WICEDSense tag (includes  multiple sensors data). The BluetoothLE can be made more generic to support all the device discovery, etc. 

For now, it's got these functions:
supports LE scan 
supports LE connect/disconnect

--
Sorry, accidently submitted the post too early (cont):

Supports 

⦁ support LE scan
⦁ Supports LE connect/disconnect (with @SimpleEvent for connection status)
⦁ Support discoverServices() callback (with @SimpleEvent when services UIUDs are ready in a text list)

It does NOT support yet:

⦁ Read/writing characteristics, or subscribing to notifications, etc. 

Sorry, just a partial implementation so that you can at least see the starting point, and play around with Android 4.3.1

--
You are my AppInventor hero! You too Friedger!
Does this mean, my raspberry pi advertising a BLE message like an iBeacon ...reference: Build your OWN Apple iBeacon with a Raspberry Pi
I will be able to now use your appinventor build to detect the UUID in this advertisement string?
1E 02 01 1A 1A FF 4C 00 02 15 [ 92 77 83 0A B2 EB 49 0F A1 DD 7F E3 8C 49 2E DE ] [ 00 00 ] [ 00 00 ] C5 00
Note that the square brackets are NOT part of the string. That's where the UUID, Major then Minor codes go. The ‘C5’ after them is a value representing transmitted power level.
Will this work!?

--

Anyone have this version built and available online somewhere? I've tried to build on google app engine and digital ocean.  Both instances fail build during YaClientApp.
YaClientApp:
     [java] Compiling module com.google.appinventor.YaClient
     [java]    Validating units:
     [java]       Ignored 4 units with compilation errors in first pass.
     [java] Compile with -strict or with -logLevel set to TRACE or DEBUG to see all errors.
     [java] Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x00000000da666000, 102752256, 0) failed; error='Cannot allocate memory' (errno=12)
     [java] #
     [java] # There is insufficient memory for the Java Runtime Environment to continue.
     [java] # Native memory allocation (malloc) failed to allocate 102752256 bytes for committing reserved memory.
     [java] # An error report file with more information is saved as:
     [java] # /root/appinventor-sources/appinventor/appengine/hs_err_pid4260.log
BUILD FAILED
/root/appinventor-sources/appinventor/build.xml:19: The following error occurred while executing this line:
/root/appinventor-sources/appinventor/appengine/build.xml:354: Java returned: 1
Any ideas why?

--

Solved the memory leak issue by upgrading my Google App Engine VM to a 2 CPU and 8Gig of RAM box. So it doesn't crash on memory anymore but fails the BUILD here:
[java] WARNING: ____Execution failure: [java] java.io.IOException: Cannot run program "/tmp/aapt7179927811381424841": error=2, No such file or directory [java] at java.lang.ProcessBuilder.start(ProcessBuilder.java:1047) [java] at java.lang.Runtime.exec(Runtime.java:617) [java] at com.google.appinventor.buildserver.Execution.execute(Execution.java:111) [java] at com.google.appinventor.buildserver.Compiler.runAaptPackage(Compiler.java:1053) [java] at com.google.appinventor.buildserver.Compiler.compile(Compiler.java:596) [java] at com.google.appinventor.buildserver.ProjectBuilder.build(ProjectBuilder.java:164) [java] at com.google.appinventor.buildserver.Main.main(Main.java:82) [java] Caused by: java.io.IOException: error=2, No such file or directory [java] at java.lang.UNIXProcess.forkAndExec(Native Method) [java] at java.lang.UNIXProcess.<init>(UNIXProcess.java:186) [java] at java.lang.ProcessImpl.start(ProcessImpl.java:130) [java] at java.lang.ProcessBuilder.start(ProcessBuilder.java:1028) [java] ... 6 more [java] [java] Dec 13, 2014 5:07:46 PM com.google.appinventor.buildserver.Compiler runAaptPackage [java] WARNING: YAIL compiler - AAPT execution failed.BUILD FAILED/home/mr_birmingham_gmail_com/appinventor-sources/appinventor/build.xml:24: The following error occurred while executing this line:/home/mr_birmingham_gmail_com/appinventor-sources/appinventor/buildserver/build.xml:162: Java returned: 1

--

Although the build failed, it failed on line 162 out of 165 in the build.xml.  So, I went ahead an launched the app engine and does anyone else find this attached screenshot completely amazing?! or is it just me!?  Awesome!!

Now let's see how it works.

--

Has anyone been able to build an app using the new BLE component David made?  I made it this far, and actually removed the do while block so their are no warnings or errors before the build and I still get this error.

--
Sorry for the rapid fire of group post, but I'm done for the day....  

If anyone interested in playing with the first cut of App Inventor 2 with BLE working, smash your mouse here: http://130.211.247.197:8888

--
You can also find offline version with instructions here:
Thank you Jacob, I tried to use App Inventor to interface to a RFDuido device but I experienced the same issue of BLE support. I tried to test your link but seems that it doesn't work on my android device (Huawei Ascend P7). I succesfully connected Ai2 Companion but I see an empty page, without any controller. Any idea?

--
I'm not sure I have the AI2 companion working yet. I'll have to look into it. I was trying to just build an app to download.

--
I just pushed in some more updates to this fork:


1. Added form.dispatchError...  so that users without Android 4.3.1 get a warning
2. Setup the code so immediately upon the device connect, the class issues the discoverServices() command
3. I can now reliably run getServices() and return the UUIDs supported on the BTLE device
4. Can issue a UUID read, and return the bytes through the BluetoothGattCharacteristics callbacks and classes

I've enclosed a simple program to go through the function calls (SHA 91aaad7bab9f64bbca2eb86f910b71dd4900f864 from the AI2 sources fork).  

The main problem I have right now is my @SimpleEvents are not firing at all.. One of the core things with BTLE programming is everything is done through asynchronous callbacks, and a fully generic BTLE components needs many @SimpleEvents to track results. 


--
Sourced update.
Started David's appinventor server here:

I still can't build apk. Not sure what is wrong... getting a YAIL compiles - AAPT execution error.

 Screen Shot 2014-12-15 at 12.42.50 AM.png

--
Not sure, I've only tested on 64-bit Linux, RHEL6 machines. javac -version = 1.7.0_65

--
Does anyone else have a public server running David's version of appinventor with BLE scan for UUID advertisements working?  

Does anyone want to try and get it working on my Google App Server running linux Debian Wheezy with Java Oracle version 7 JDK?

I keep getting an error in the build at the VERY end.  

[java] Dec 17, 2014 6:07:29 AM com.google.appinventor.buildserver.Execution execute [java] WARNING: ____Execution failure: [java] java.io.IOException: Cannot run program "/tmp/aapt1115454077627075918": error=2, No such file or directory [java] at java.lang.ProcessBuilder.start(ProcessBuilder.java:1047) [java] at java.lang.Runtime.exec(Runtime.java:617) [java] at com.google.appinventor.buildserver.Execution.execute(Execution.java:111) [java] at com.google.appinventor.buildserver.Compiler.runAaptPackage(Compiler.java:1053) [java] at com.google.appinventor.buildserver.Compiler.compile(Compiler.java:596) [java] at com.google.appinventor.buildserver.ProjectBuilder.build(ProjectBuilder.java:164) [java] at com.google.appinventor.buildserver.Main.main(Main.java:82) [java] Caused by: java.io.IOException: error=2, No such file or directory [java] at java.lang.UNIXProcess.forkAndExec(Native Method) [java] at java.lang.UNIXProcess.<init>(UNIXProcess.java:186) [java] at java.lang.ProcessImpl.start(ProcessImpl.java:130) [java] at java.lang.ProcessBuilder.start(ProcessBuilder.java:1028) [java] ... 6 more [java] [java] Dec 17, 2014 6:07:29 AM com.google.appinventor.buildserver.Compiler runAaptPackage [java] WARNING: YAIL compiler - AAPT execution failed. [java] Result compacted from 7351.1KiB to 5783.4KiB to save 1567.7KiB [java] Merged dex A (3668 defs/5725.4KiB) with dex B (34 defs/66.9KiB). Result is 3702 defs/5783.4KiB. Took 0.3sBUILD FAILED/home/mr_birmingham_gmail_com/appinventor-sources/appinventor/build.xml:24: The following error occurred while executing this line:/home/mr_birmingham_gmail_com/appinventor-sources/appinventor/buildserver/build.xml:162: Java returned: 1

--
Any luck on the build?  I was able to get the builder server running on Amazon EC2, but it required the 32-bit machines.  

I've put more updates into the wicedfast fork, and it should support listing the UUID services from a connected BTLE device. The WICEDSense component has the example code from BTLE writeDescriptors, along with code for OnDiscovery, OnRemoteRSSI, and setCharacteristic calls from the Android API (level 18). 

--
What happens with this command on your machine?

java -version

I just downloaded Java 7u72 from Oracle's website, and installed the jdk-7u72-linux-i586.rpm. Then I override on the terminal to make sure it doesn't pick up the OpenJDK kite

export JAVA_HOME=/usr/java/jdk1.7.0_72
export PATH=$JAVA_HOME/bin:$PATH

java -version
java version "1.7.0_72"

--
I've tried everything. I really don't know why I can't obtain a successful build.

I could just give you access to my redhead box if you would be so kind to poke around and see if you can get a build working for me. I'm extremely anxious to test out your new version of app inventor with BLE, but can not get past step 1.  Build successfully! 

--
Were you able to list UUIDs for BLE devices advertising the 31 byte ADV message?   There isn't a connection during this advertisement, but there is UUID and advertisement data being announced.

--
What's the latest?? Anyone have a BLE working version of AI2 accessible on a public server yet?  I'm still looking to simply snag the UUID from a broadcast advertisement without requiring any BLE connectivity.  Kind of like an iBeacon.

--
you do not have enough memory and you need to set swap for linux

--


댓글 3개:

  1. Hi there!

    I would like to use AI2 to program a BLE-related app. Could you have something working?

    Thanks!

    Moisés

    답글삭제
  2. Hi,
    Did you get any support form someone?
    If you can create the BLE component for non-programmers in MIT App Inventor that will be great.

    답글삭제
  3. Hi,
    Did you get any support form someone?
    If you can create the BLE component for non-programmers in MIT App Inventor that will be great.

    답글삭제