Newbie88 A

unread,
Jul 25, 2017, 4:10:44 AM7/25/17
to MIT App Inventor Forum
I have followed someone elses app in appinventor 2 to connect to a BLE device to turn a LED on and off. The app itself seems to be correct. The URI with its 2 buttons looks good. I can download it to my PC and then to my Android phone OneplusOne with Marshmallow 6.0 on it. I can open the app on my phone, click on the icon "ListPicker" but it won't find any BLE devices. I then paired my BLE device (Feather 32u4) from Adafruit to my phone. Then when I click on list picker it will find the device but still won't connect to it. "Error: 507 unable to connect. Is the device turned on?". However, I can connect to the BLE device with the Adafruit app just fine. Btw, MIT has written that code for Adafruit's board (Feather 32u4).
I did find out about the "Extensions" but can only find one for Arduino not for my board? Also under "Blocks" I picked "ListPicker 1" because that's what the app I'm following does. I don't know if that's the right component? Under "Blocks" I also found "Any component" and then "Any Bluetooth Client". I just have no idea which one would be the correct one for my BLE device?
I would definitely need help with connecting to my BLE device "Feather 32u4".I did attach the code blocks to this Forum for your convenience.
Under "Designer" the Firsts Text box is just a "Label" to show whether its connected or not - below that is the "List Picker 1" - below that is 2 "Buttons" and below that is just another "Label" - I did attach that view as well.
Thanks for helping and hopefully clarifying things for me.
Thanks,
Nick
P.S.: I did complete the beginners apps like "Talk To Me", "Ball Bounce" and "Digital Doodle". All those apps work great on my phone
AppInventor_CodeSnippet.PNG
Designer view of AppInventor App.PNG

gerrikoio

unread,
Jul 25, 2017, 6:05:09 AM7/25/17
to mitappinv...@googlegroups.com
Did you test your Arduino sketch to see that it works with Adafruit's Bluefruit App. This will at least confirm you have that end sorted. NOTE this app is actually quite essential to download as it will always check to see that you have the latest Bluefruit firmware loaded.

Then, assuming all correct and App Inventor still not seeing your device, you do have options to change the way that the Bluefruit device advertises itself for other Bluetooth Low Energy devices via the Bluefruit AT command set.

gerrikoio

unread,
Jul 25, 2017, 8:03:40 AM7/25/17
to mitappinv...@googlegroups.com
It's your lucky day :-). I just happened to be working on an app myself using BLE and I happen to have an Adafruit Feather 32u4 Bluefruit LE on my desk, so thought to check what happens as I've never tried before.

Well with a few tricks I could get App Inventor to see the Bluefruit device in my scan list (using the "call StartScanning" method - don't forget to use the stopscanning method once you've got your list). Note you sometimes need to rescan for the app to find the device. This is the most often the case if you have multiple BLE devices present, as the "BLE DeviceFound" event will trigger as soon as it finds a device. So it's a case of try and try again until your BLE name is found. This is common with all BLE scanning apps. 

I then could also get App Inventor to connect to the Bluefruit device. Looking at your attached code snippet, I see you are using a clock timer and an IF statement. You do not need to do this as there is a "when BLE connected" method available.

That's as far as I took it. 

Now the fun stuff... In order to write (i.e. send) values or strings to your Bluefruit BLE device you need to tell the app which BLE service UUID and BLE characteristic UUID you want to use.

You can get these using the "call BLE Supported Services" and the "call BLE Supported Characteristics" methods once your app is connected to the BLE device.

Now I am not sure why, but when I called these methods it provided me with a list but "unknown services" and "unknown characteristics".  I've attached my output. 

Maybe someone else (@evan?) could comment as to whether this is normal or a bug.



gerrikoio

unread,
Jul 25, 2017, 8:55:34 AM7/25/17
to MIT App Inventor Forum
Sorry forgot to state the obvious when looking at your code snippet...

You are trying to use standard Bluetooth 2.x (i.e. Bluetooth Client / Server) blocks in your App. The Adafruit Bluefruit module is Bluetooth 4.xx (bluetooth smart / low energy). So to get your app to connect with Bluefruit you need to instead add in the App Inventor BLE extension. 


Evan Patton

unread,
Jul 25, 2017, 8:59:17 AM7/25/17
to MIT App Inventor Forum
We only have a fixed number of services and characteristics programmed into the BLE extension right now. Long term we are planning to have it so that such a list can be dynamically loaded from the web, but I don't have an ETA on that. That being said, some of those service and characteristic UUIDs ought to be known since they are required by all BLE devices so we will need to look into that.

Evan

gerrikoio

unread,
Jul 25, 2017, 10:30:02 AM7/25/17
to MIT App Inventor Forum
@Evan, I did some more testing my end using 3 different BLE scanning apps (see attached screenshots). Looks to me that this issue is partly manufacturer driven (Bluefruit uses a Nordic BLE device), regarding implementation of how services are described when advertised. It is then how each app deals with the Nordic interpretation. 

I also tested using a Cypress BLE app (CySmart) and this app also showed a mix of unknown services plus a "Device Information Service" and a "Gatt DB" service (I've no screenshot).


Screenshot_2017-07-25-15-14-13.png
Screenshot_2017-07-25-15-15-01.png
Screenshot_2017-07-25-15-16-56.png

Evan Patton

unread,
Jul 25, 2017, 1:34:50 PM7/25/17
to MIT App Inventor Forum
Correct. However, BLE uses UUIDs, which are a 128-bit value space. Anyone can simply pick a UUID to provide a service and you are not required to register it. For example, we did this for the IOT release for our Arduino 101 services. We also reused some from Scratch but there are also predefined UUIDs, such as the GATT service, that are fixed by the Bluetooth SIG. All of the latter UUIDs ought to be supported/known, but my guess is that we haven't fully enumerated them in the extension sources. That was my concern. In general though, we won't be able to know every service name unless Bluetooth provides some standard mechanism in a future version of the protocol for broadcasting names for UUIDs.

Evan

gerrikoio

unread,
Jul 25, 2017, 1:36:17 PM7/25/17
to mitappinv...@googlegroups.com
@Evan I also noticed, a flaw in how the SupportedCharacteristics method is implemented.

At present you are listing everything when this method is called, so it is quite difficult / time consuming to work out which characteristic is linked to which service. 

As I'm sure you're aware, characteristics are usually associated with a particular GATT service. So the SupportedCharacteristics method really requires a service parameter in order to filter out non valid characteristics.

I suspect if the wrong characteristic is chosen and you try and use with another service it may cause a crash.

gerrikoio

unread,
Jul 25, 2017, 1:45:18 PM7/25/17
to MIT App Inventor Forum
@Evan, yes that's right. I found it relatively straightforward to strip out / parse the standard GATT services as per spec (https://www.bluetooth.com/specifications/gatt/services) such as 0x1800, 0x180a, etc. I then had to figure out the non standard options from Nordic (such as their UART service) and then Adafruit specific such as the firmware OTA option. The latter option for Adafruit must use a Nordic specific format as it was only the Nordic BLE app that was able to tell me what the 0x1530 service was about (maybe there is a text string descriptor linked to the UUID.

Evan Patton

unread,
Jul 25, 2017, 2:00:00 PM7/25/17
to MIT App Inventor Forum
Gerrikoio,

If you are using the latest BLE extension from our IOT site, you can use GetCharacteristicsForService with a service UUID and it will only give you the associated characteristics for that service. There is also the DeviceCharacteristics property which gives a list of triplets (service, characteristic, name) that you can then filter by service UUID.

Evan

gerrikoio

unread,
Jul 25, 2017, 3:04:28 PM7/25/17
to MIT App Inventor Forum
Thanks. Gosh, hadn't realised my test app was still using an old version of the BLE extension. Well that solves that then. :-)

Newbie88 A

unread,
Jul 26, 2017, 9:34:46 AM7/26/17
to MIT App Inventor Forum
Hi Evan, Nick here..thx for your help with the latest extensions link and the tutorial with BLE connect..the app now connects to the Feather 32u4 just fine..but like u mentioned earlier there is nothing I can do there..I would actually need to connect with a characteristic and it's uuid..you wouldn't happen to have a tutorial on that, would you? That BLE connect tutorial was so easy to follow and it made kind of sense too..b really nice to have one to connect to a service and characteristic..I'll attach some screen shots which windows I have to go through just to get the control pad..i don't know the uuid for it just yet..but I'm trying to find out..if there is no tutorial maybe gerrikio or somebody else can walk me through which blocks to use to connect to the control pad..
Unfortunately I can't find an attach link on my mobile device anywhere..have to wait til I get back home on my computer Win10 to do it..
Thx
Nick

gerrikoio

unread,
Jul 26, 2017, 10:33:25 AM7/26/17
to mitappinv...@googlegroups.com
Hi Nick, pleased to hear that you could get the app to connect. I've done a bit more work testing my end and I was able to get the app to communicate with the Bluefruit device. My little test app can now turn the LED (on pin 13) on and off. I had to modify the Arduino bleuart_cmdmode.ino example to get it all to work properly. 

From the app side you can do the following once the Bluefruit device is connected.

I used a listpicker to list out the services using the "call BLE SupportedServices" method. Note that the returned output is a csv string and not a list. So use "list from csv row" method to convert to a list for the listpicker.

As an earlier screenshot had showed, there are 5 services listed. The one you need to select is the one starting with "6e400001...."

When you select the correct service you can use the listpicker "SelectionIndex" value to tell BLE which service UUID to use.

There are then only two characteristics linked to the "6e400001" UART service. One is for RX and the other is for TX. To get these characteristics use the following blocks (I used a global to store but this may not be necessary)


I then used the "RegisterforStrings" method to tell the App how to listen for incoming text messages. Note that Arduino program used "ble.print" which outputs text strings. To send messages to the Bluefruit device you use the "WriteStrings" method. I used utf-8 rather than utf-16.

This is what my blocks looked like (note that in my example I send out a "status" string to bluefruit as my starting request, but you can use whatever you want here):


Then to process any messages sent from Bluefruit to the app you simply use the "when BLE StringsReceived" method.

Hope this helps. 





gerrikoio

unread,
Jul 26, 2017, 12:25:53 PM7/26/17
to MIT App Inventor Forum
Images attached
Bluefruit_block2.png
Bluefruit_block1.png

Newbie88 A

unread,
Jul 27, 2017, 11:08:56 AM7/27/17
to MIT App Inventor Forum
Hi gerrikoio, the Bluefruit block1 pic makes sense but I can't find the first block in orange..the "set global CharacteristicUUID" wonder where you got that from? And the second one, where do you hook it up to? It needs to be connected to one of the brown ones but which one? This is all very confusing to me..I guess I would need a tutorial which can take me step-by-step through it..if you look at the attached pic you'll see where I'm at.. and from here I would need step-by-step instructions exactly which blocks to use to get connected to a characteristic..and then how to send a few characters to the BLE..I already asked Evan for such a tutorial but I got no answer back..
Screenshot_20170727-170511.png

gerrikoio

unread,
Jul 27, 2017, 12:17:35 PM7/27/17
to MIT App Inventor Forum
Hi Nick

Regarding tutorials, I will write something up and post it on https://www.hackster.io/ sometime tomorrow. Once done I will send you a link via this thread. 

gerrikoio

unread,
Jul 28, 2017, 9:09:22 AM7/28/17
to MIT App Inventor Forum

Newbie88 A

unread,
Jul 29, 2017, 5:39:42 AM7/29/17
to MIT App Inventor Forum
Hi gerrikoio, Nick here..great tutorial, works great and does what it claims..turns LED on and off and even blinks it..I like the UI it looks great..I've changed the PIN to 12 and connected a little motor to it and it works the motor as well...the only thing I'm missing is a disconnect from BLE button..but I will study the code and am sure I can manage it myself..I also have to study the entire code since all this BLE stuff is kind of new and a bit complex to me..It'll take me a while to study it..However, it works great and I'm really glad that you helped me out there..
Thanks a lot
Nick
Reply all
Reply to author
Forward
0 new messages