Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Bluetooth Low Energy plugin — Gideros Forum

Bluetooth Low Energy plugin

octotodoctotod Member
edited February 2014 in Plugins
Hello all! I am trying to make Gideros talk to a sensor peripheral (accelerometer and such) via BLE. I'm using the Red Bear Labs BLEmini board (redbearlab.com/blemini/), and I can successfully talk with it using Red Bear's sample Xcode project (github.com/RedBearLab/iOS). Searching around this forum / the rest of the internet, it seems the way to add Bluetooth functionality to a Lua script framework is via a plugin. I used @OZApps very nice plugin tutorial to get started (howto.oz-apps.com/2013/11/creating-plug-in-for-lua-framework.html), and can successfully send data to and from the test.mm plugin.

All good so far! So I rebuilt GiderosiOSPlayer with the BLE (github.com/RedBearLab/iOS/tree/master/BLEFramework/BLE) and CoreBluetooth frameworks. However when I try to initialize a new BLE object I run into trouble. I added

#import "BLE.h"

at the top of test.mm, and then attempt to init the BLE object using

bleShield = [[BLE alloc] init];

from inside the test() function in test.mm. When calling

local t = require "mytest"
t.test()

from Gideros Studio, I get the following error:

main.lua:33: Error calling 'init' on ''
Could not find function named "init" associated with object BLE(0x15e66b50).(It may have been released by the GC)
stack traceback: main.lua:33: in main chunk

I'm definitely out of my depth here hacking around with the C++ / ObjC code. Can anyone point me towards a resource which might get me closer to BLE integration? I think this would be quite a nice addition to the world of Gideros, and I am more than happy to share my progress.

Thank you!

Likes: OZApps

+1 -1 (+1 / -0 )Share on Facebook

Comments

  • @octotod,
    you mean that you are able to run this as a pure C++/Obj-C implementation but the moment you try to wrap it in a plug-in, you are faced with these issues, am I correct in that understanding?
    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
  • @ozapps, yes I can run the test app from Red Bear, called "BLE Chat", on my iPod5 just fine. When I start copy-pasting code from the ViewController.m file of BLE Chat into the test.mm file of your plugin tutorial, I see this initiation problem. I think at least part of the problem is this "delegate" concept which I do not understand. Looking at the code for BLE Chat, the ViewController.h file starts with

    @interface ViewController : UIViewController {
    BLE *bleShield;
    }

    and BLE.h starts with

    @protocol BLEDelegate

    I'm guessing something about this delegate object type tells the Xcode compiler how to do the init. Probably I need to go back to basics and learn more about the keywords delegate, @interface, and @implementation!

    I should also mention I tried using Wax to initialize a CBPeripheralManager, which did not work, perhaps because CoreBluetooth is not yet part of Wax?
  • @octotod,
    a couple of things

    1. I have been facing issues in trying to get BT working for over a year now. I was trying with an Android and Java using a 3rd party Library that *just* works, still no go.

    2. The @protocol BLEDelegate is used to communicate with the module, sort of to set a call back.

    3. WAX will not be of much help as it does not automatically map all of the protocols, delegates, etc

    4. Yes it will help to catch up on the basics of objective-c classes and the keywords. interface and implementation are more OO related.

    Let me see when I get the time to go through this code.
    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
  • octotodoctotod Member
    edited February 2014
    @ozapps

    Ok, thank you very much for looking at it!

    I found another Xcode project which wraps this Red Bear SDK, this one for PhoneGap Cordova (github.com/don/BluetoothSerial/tree/master/src/ios). Instead of a ViewController, the main wrapping code is called MEGBluetoothSerial. In MEGBluetoothSerial.h we find CDVPlugin instead of ViewController
    <a href="https://forum.giderosmobile.com/profile/interface" rel="nofollow">@interface</a> MEGBluetoothSerial : CDVPlugin <BLEDelegate> {
        BLE *_bleShield;
        NSString* _connectCallbackId;
        NSString* _subscribeCallbackId;
        NSMutableString *_buffer;
        NSString *_delimiter;
    }
    From the iOS docs, "The syntax to indicate that a class adopts a protocol again uses angle brackets. This means that any instance of MEGBluetoothSerial will respond not only to the methods declared specifically in the interface, but that MEGBluetoothSerial also provides implementations for the required methods in BLEDelegate. There’s no need to redeclare the protocol methods in the class interface—the adoption of the protocol is sufficient."

    So, to fit this pattern it seems that the plugin should be implemented as a class, and that class should adopt the BLEDelegate protocol. I'm confused about what to use instead of CDVPlugin or ViewController in the @interface line. Some Gideros core class?
  • @octotod,
    how embarassing and frustrating, despite having more iOS gadgets than I can handle, and one running iOS7, I found that to run BT LTE, I need an iPad Mini or higher, a 4S or higher or the new iPad and higher.

    I have an iPhone5 so I have a single device, however I do not have another device to experiment with (I have another new iPad but cannot use it for testing, out of bounds) :(

    Will have to rethink getting a new ipad Air or something. Plus I am still running 10.7.5 on my main machine (need to upgrade that to 10.9.1) however most of my software that works might not continue to work on that. Grrrr..... crazy Apple hell, I thought that Microsoft DLL versioning was crazy, this Apple thing is worse :(
    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
  • and with the rant out of the way,....

    The code above is a custom encapsulation of the BT communication. Generally the device creates a service and starts broadcasting. Then the other device (generally your iOS device) will register to listen for all broadcast UUIDs and connect to one accordingly and then read/write data with that UUID.
    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
  • octotodoctotod Member
    I just found this iOS library called LGBluetooth (on github by DavidSahakyan) which aims to make CoreBluetooth easier to use. I ran the project on my ipod5, and indeed it can see BLE peripherals. I am now using this with Wax to talk to BLE devices, it works quite well. I had to learn about blocks, really glad @bowerandy added that functionality!

    My code is pretty messy, but someday I hope to turn it into a library (when I learn more about that process!). If anyone is trying to speak BLE, let me know and I will try to help.

    Next up: talking to BLE on Android!
  • @octopod @OZApps guys is there any successful attempt to include BLE support in Gideros for Android so far?
    I am digging around but cant find useful approach to the problem. I have project that needs connection between Bluetooth device and Android phone and really prefer to use Gideros if possible. Any info will be of great help.
  • hgy29hgy29 Maintainer
    Hi @plamen,

    I actually wrote a BLE plugin for Gideron on Android and iOS one year ago or so, so it is indeed possible. But it mysteriously stopped working a few month ago for Android, I suppose due to some change in Android permission stuff or API. Note that is was BLE only, not generic bluetooth!
  • Hi @hgy29 , is the plugin available, what is the price can i use it, do you need a hand to investigate the issue. So many questions, the bottom line is i am trying to do Android project with Gideros and before i give up and move to python, want to be sure... you know. The device i want to connect is BLE.
  • hgy29hgy29 Maintainer
    Looks like I will need it again soon myself, so I'll do some tests today to see what is going on with it.

    Likes: plamen

    +1 -1 (+1 / -0 )Share on Facebook
  • Great news @hgy29! My code is pretty crusty at this point, and only worked on iOS. A cross-platform plugin would be really nice. I had moved to Ionic for app making (with Cordova), but it's really great to see all the action here on Gideros forum.

    Likes: simwhi

    +1 -1 (+1 / -0 )Share on Facebook
  • Cross-platform plugin will be dream come true.
Sign In or Register to comment.