Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Bluetooth Low Energy
Started by Lerg Oct 21 2016 08:55 PM

38 replies to this topic
release android ios
[TOPIC CONTROLS]
Page 1 of 2 1 2
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

Lerg

[GLOBAL: userInfoPane.html]
Lerg
  • Contributor

  • 538 posts
  • Corona Staff

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

Documentation



[TOPIC: post.html]
#2

jalemanyf

[GLOBAL: userInfoPane.html]
jalemanyf
  • Enthusiast

  • 71 posts
  • Corona SDK

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 ;) Thanks!


[TOPIC: post.html]
#3

Lerg

[GLOBAL: userInfoPane.html]
Lerg
  • Contributor

  • 538 posts
  • Corona Staff

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



[TOPIC: post.html]
#4

jalemanyf

[GLOBAL: userInfoPane.html]
jalemanyf
  • Enthusiast

  • 71 posts
  • Corona SDK

Great News Lerg!

 

Now it's working like a charm!!!

 

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



[TOPIC: post.html]
#5

andreseduardop

[GLOBAL: userInfoPane.html]
andreseduardop
  • Observer

  • 3 posts
  • Corona SDK

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

[TOPIC: post.html]
#6

Lerg

[GLOBAL: userInfoPane.html]
Lerg
  • Contributor

  • 538 posts
  • Corona Staff

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.



[TOPIC: post.html]
#7

andreseduardop

[GLOBAL: userInfoPane.html]
andreseduardop
  • Observer

  • 3 posts
  • Corona SDK

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

[TOPIC: post.html]
#8

Lerg

[GLOBAL: userInfoPane.html]
Lerg
  • Contributor

  • 538 posts
  • Corona Staff

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.



[TOPIC: post.html]
#9

zhg0121

[GLOBAL: userInfoPane.html]
zhg0121
  • Observer

  • 3 posts
  • Corona SDK

<type 'function' is not supported by JSON.>



[TOPIC: post.html]
#10

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,331 posts
  • Enterprise

@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



[TOPIC: post.html]
#11

zhg0121

[GLOBAL: userInfoPane.html]
zhg0121
  • Observer

  • 3 posts
  • Corona SDK

@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



[TOPIC: post.html]
#12

anto80

[GLOBAL: userInfoPane.html]
anto80
  • Observer

  • 1 posts
  • Corona SDK

Hi,

 

when we can test it on iOS devices?

 

tnx



[TOPIC: post.html]
#13

Lerg

[GLOBAL: userInfoPane.html]
Lerg
  • Contributor

  • 538 posts
  • Corona Staff

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



[TOPIC: post.html]
#14

zhg0121

[GLOBAL: userInfoPane.html]
zhg0121
  • Observer

  • 3 posts
  • Corona SDK

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



[TOPIC: post.html]
#15

rsp355

[GLOBAL: userInfoPane.html]
rsp355
  • Observer

  • 10 posts
  • Corona SDK

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!



[TOPIC: post.html]
#16

rsp355

[GLOBAL: userInfoPane.html]
rsp355
  • Observer

  • 10 posts
  • Corona SDK

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


[TOPIC: post.html]
#17

gc34

[GLOBAL: userInfoPane.html]
gc34
  • Observer

  • 12 posts
  • Corona SDK

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.



[TOPIC: post.html]
#18

gc34

[GLOBAL: userInfoPane.html]
gc34
  • Observer

  • 12 posts
  • Corona SDK

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.


[TOPIC: post.html]
#19

Lerg

[GLOBAL: userInfoPane.html]
Lerg
  • Contributor

  • 538 posts
  • Corona Staff

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



[TOPIC: post.html]
#20

gc34

[GLOBAL: userInfoPane.html]
gc34
  • Observer

  • 12 posts
  • Corona SDK

Ok, Lerg.



[TOPIC: post.html]
#21

CesarHernandez

[GLOBAL: userInfoPane.html]
CesarHernandez
  • Enthusiast

  • 80 posts
  • Corona SDK

Hi dear,

 

I'm testing your plugging due I need a way to connect one device with other. The idea is to transfer a little amount of data, and, if possible, perhaps a file with about 10-20Kbyts.

 

Can your plugging allow to connect 2 devices (android or iOS in future), that run the very same lua application, and can send/receive information between two devices?

 

Thanks again,



[TOPIC: post.html]
#22

Lerg

[GLOBAL: userInfoPane.html]
Lerg
  • Contributor

  • 538 posts
  • Corona Staff

Yes, that's the goal of the plugin.



[TOPIC: post.html]
#23

emi

[GLOBAL: userInfoPane.html]
emi
  • Enthusiast

  • 43 posts
  • Corona SDK

I'm trying to connect to my Polar H7 device with my Android phone without success.
 
When the App starts I can see the Init event: {"name" "init", "isError": false}. I've switched all "print" to "showAlert" in order to see them on my device.
 
After that, if I tap the "Start Scan" button, nothing happens. "Is active?" button says "Yes".
 
I've seen that gc34 accessed succesfully to the H7, so maybe am I missing something?
 
UPDATE:
 
I've tried it with another device and it worked. I have no idea what's wrong with my Z3...


[TOPIC: post.html]
#24

rsp355

[GLOBAL: userInfoPane.html]
rsp355
  • Observer

  • 10 posts
  • Corona SDK

Could someone kindly guide me on how to send a character once connected to a device? I can't seem to get this working.

[TOPIC: post.html]
#25

CesarHernandez

[GLOBAL: userInfoPane.html]
CesarHernandez
  • Enthusiast

  • 80 posts
  • Corona SDK

Yes, that's the goal of the plugin.


Dear, could you share a little chunk of code where we can understund how to connect 2 devices (2 smartphones running corona app) and send from one to other some file?

As mentioned, i need to send from one smartphone to other a file, and im testing with this bt and also thinking in nfc plugging. For example, remember the bump app where installing it in both smartphone you cand share and send photos or other from one device to other? Thats what i need for my app. Possible? Thanks so much for your great job and support.


[topic_controls]
Page 1 of 2 1 2
 
[/topic_controls]

Also tagged with one or more of these keywords: release, android, ios