Bluetooth Low Energy

Bluetooth plugin provides direct access to the Bluetooth Low Energy capabilities of a device.
 

Documentation

I downloaded the sample to test it. I’m going to share my problem/experiences.

First of all, I have to add two permissions on the build.settings file:

android = 

{

usesPermissions = {

“android.permission.BLUETOOTH”,

“android.permission.ACCESS_COARSE_LOCATION”,

},

},

Now the app starts without errors and seems to work but, when I pulse any button unexpectedly the App closes and goes to the system.

Any help is welcome :wink: Thanks!

jalemanyf, updated the plugin, you don’t need now to include those permissions.
Try running again, if your app crashes again, please provide logs.

Great News Lerg!

Now it’s working like a charm!!!

Thanks for your quick answer. Now I have a lot to test and learn…

Hi.
Does this plugin is compatible with Bluetooth Classic devices?

andreseduardop, no, it only works with Bluetooth 4.0+ devices. Android itself has API for classic bluetooth, but iOS does not, so to make the plugin crossplatform it only makes sense to support Bluetooth Low Energy.

Ok.
Thanks, Lerg.
But, Is it possible to connect with this Bluetooth Android API from lua?

andreseduardop, to access Bluetooth Classic API on Android using Corona SDK a different plugin has to be made.
My plugin supports Bluetooth Low Energy only.

<type ‘function’ is not supported by JSON.>

@zhg0121. Welcome to the forums.

Did you have a question? Your post above doesn’t really put the message you typed into any context that we can really help answer a question. Can you provide a bit more information about what you’re trying to do, when you get the message? Are you having a problem?

FWIW, just looking at <type ‘function’ is not supported by JSON.> would seem to be a message output by the json.prettify() function frequently used to print out table values.  Tables can contain functions and userdata types that are not valid for JSON, so it’s just letting you know that it can’t output a function (which doesn’t make sense, function’s are not data).

Rob

@Rob Miracle

It’s always show

“writeDescriptor”:"<type ‘function’ is not supported by JSON.>",

“beginReliableWrite”:"<type ‘function’ is not supported by JSON.>",

                    

include <type ‘function’ is not supported by JSON.> so many times.

another question 

I don’t know where to find out whether I really connect to the device

Hi,

when we can test it on iOS devices?

tnx

anto80, hi, current goal is by the end of the year.

Hi

I have a problem 

I used your sample-file

when I connect to my BLE device

it will disconnect after about one minute

and I can’t reconnect or scan if I don’t open the app again

sorry that I don’t know if you really understand my question

Hi Lerg, 

Good work on getting this plugin set up!

I am a beginner with Corona SDK and app development in general so this may be a very basic question for you. I’m keen on setting up an arduino (microcontroller) project to turn some LEDs ON or OFF over BLE using Corona SDK. 

I have managed to establish a connection with my BLE module using your example app but I can’t really figure out how to send basic serial character commands to the device. 

Your input will be really appreciated mate!

Just to make my previous post clearer, I have created a new button in the example BLE app provided by Lerg dedicated for turning an LED on. My Arduino has been programmed to accept a non zero serial value to turn the LED on or a zero value to turn the LED off. I have tested this with a pre-built Evothings app and it works fine. 

 

I just have no idea of how to enable the example BLE app to send a value of 1 or 0 to the HM10 ble module that I using for my project. 

 

Here is the code for the button, not sure what to add to the onRelease section: 

 

local ledOn = widget.newButton

    { width = 250,

      height = 40,

      defaultFile = “buttonbackground.png”,

      overFile = “buttonbackground1.png”,

      shape = “roundedRect”,

      labelColor = { default={ 0, 0, 0 }, over={ 0, 0, 0 } },

      font = native.systemFontBold,

      fillColor = { default={ 1, 1, 1, 1 }, over={ 0.65, 0.65, 0.65, 1 } },

        label = ‘LED ON’,

        – onRelease 

        NEED HELP WITH THIS SECTION TO ALLOW FOR 1 TO BE SENT OVER THE BLE CONNECTION, PLEASE.      

      }

 

        ledOn.x = display.contentCenterX

        ledOn.y = display.contentHeight- 20

Greetings.

I have successfully accessed to a BLE device (Polar H7) with Hearth Rate sensor.

When discovering services, I get them, their characteristics, and I am able to obtain data if their property includes “Read”, using the “onCharacteristicRead” event, and the "event.characteristic:getValue() method.

But, if the property of characteristic is “Notify”, data cannot be accessed using “onCharacteristicRead”. I think I must first enable this characteristic notification, and then obtain data of this notification.

Have you any (or simply part of it) example to manage this “Notify” characteristic?

Thanks.

Trying to activate notifications:

… (more code)

characteristics={}

… (more code) 

– ONCE CONNECTED WE DISCOVER SERVICES

if (event.name==‘onServicesDiscovered’) and (not event.isError) then

   – GET DISCOVERED SERVICES

   local services=event.gatt:getServices()

   – ITERATING OVER SERVICES

   for i=1,#services do

     – GET SERVICE CHARACTERISTICS

     characteristics=services[i]:getCharacteristics()

     – ITERATING OVER SERVICE CHARACTERISTICS 

     for j=1,#characteristics do

       – 2A37 IS HEARTH RATE MEASUREMENT

       if (string.sub(characteristics[j].uuid,5,8)==‘2a37’) then

         timer.performWithDelay(j*3000, function()

         – ACTIVATE NOTIFICATION LOCALLY

         event.gatt:setCharacteristicNotification(characteristics[j],true)

         – GET CHARACTERISTIC DESCRIPTOR

         – FROM CLIENT_CHARACTERISTIC_CONFIG UUID

         descriptor=characteristics[j]:getDescriptor(“00002902-0000-1000-8000-00805f9b34fb”)

         – NEXT LINE MUST CHANGE DESCRIPTOR ATTRIBUTES TO ENABLE_NOTIFICATION_VALUE

         – IT WILL CHANGE BIT 0 (OF 16 BITS) TO “1” (IT IS “0” BY DEFAULT)

         – THIS LINE HAVE NOT BEEN CORRECTLY IMPLEMENTED STILL

            – descriptor:setValue(de.ENABLE_NOTIFICATION_VALUE)

         – NEXT LINE MUST WRITE THE UPDATED DESCRIPTOR WITH ENABLED NOTIFICATION

            – event.gatt:writeDescriptor(descriptor)

        end)

      end

    end

  end

end

This code works OK discovering services, characteristics, iterating over characteristics and filtering “2a37” characteristic. But when I try to get descriptor:

      descriptor=characteristics[j]:getDescriptor(“00002902-0000-1000-8000-00805f9b34fb”)

This descriptor is always null (descriptor=nil).

What am I doing wrong? Any help?

Thanks.

Currently I don’t have time to work on this plugin. Maybe in a couple weeks. Remind me then please.

Ok, Lerg.