Back to Home

Bada Web-based application: C ++ and Javascript interactions

bada · javascript

Bada Web-based application: C ++ and Javascript interactions

    Hi, habradrug!

    Briefly about the main thing: as you know, from C ++ code it is enough to call the EvaluateJavascriptN method to transfer data or trigger an event. But from Javascript, calling the C ++ method is not yet possible. To do this, we will have to use very scrap iron. But if it’s more convenient for you to develop an application in html / css / Javascript , but at the same time you need access to the native code and local files, then you definitely have to cat. There we will examine this crowbar in more detail and prepare a Project Template for a quick start.

    First, I apologize for my gaps in knowledge of the Russian language, but I will make every effort to ensure that the text is error-free.

    A little bit of lyrics


    Last week we received an order to develop many small applications for Bada. I myself work in the web areas, but the boss, recalling my old knowledge in C ++ and my experience in .NET, asked to help my colleagues, they say you will remember / understand, and you need to help - otherwise we will not have time. And so I got the first two tasks:
    • application with a dozen buttons and playing sounds
    • application with some articles to read

    The beginning of the way


    Rolling up my sleeves quickly downloaded the Bada SDK . And creating a new project, I was interested in the template “Web-Based Application” . It created a web control in which a local html file was loaded , and a search on the Internet suggested that the bada has a -webkit engine and these facts made me very happy. But my joy was hasty: audio is not played and it is impossible to receive the local file cotent via ajax due to security. This is where it occurred to us that we need to somehow connect Javascript and C ++, so in the first case, at the click of a button, we will send the corresponding path to the file in the native code and play the sound from there. Well, in the second, we will send again the path to the file and get its content, alaXmlHttpRequest .

    And here is the scrap


    Having looked more closely at web control, it turns out that we have at our disposal only ILoadingListener . A quick reference to the more encouraging IWebUiListener is mentioned in the code . But the developers have not yet gotten their hands on it, so let's get back to ILoadingListener . Fortunately, it works as it should, that is, if we add an iframe to the body or call window.frames.myFrame.location.reload (), we get load events in the code. I should note that we can not even specify the src attribute of the iframe . Well, now everything is very simple, change hash , make iframe reload and get in OnLoadingRequestedurl of the page and react accordingly. However, when I saw that the web control has the GetTitle method , I settled on changing the page title and getting it on OnLoadingStarted . Thus, we have no problems with URL encoding.

    Decorate crowbar - get framework


    There was a desire to create a ready-made database with which you can start writing this kind of application. Here are the main code examples / tasks:

    JavaScript:


    bada.sendData({key:'Value',secondKey:'secondValue'});

    this function will pass in C ++ "key = Value & secondKey = secondValue" and there it will be parsed in WebCppProxy :: Data :: StringDictionary .
    Other examples requiring no explanation:
    bada.getFile('/Res/help.html',function(responseText){ $('#helpDialog').html(responseText)});
    bada.playAudio('/Res/sound.mp3');


    For better testing, a console was created that will be available if you set the debug = 'true' attribute in the body tag

    C ++


    Here the counselor is the class WebCppProxy :: WebProxyForm - this is the same web control, but in which the relationship with Javascript is implemented. The counselor receives the class inherited from WebCppProxy :: ICommandListener from the current application and calls certain methods during execution.
    virtual void OnCommand(Osp::Base::String command) = 0;
    virtual void OnCommand(WebCppProxy::Data::StringDictionary* dictionary) = 0;
    virtual void OnReady() = 0; // ondomready


    Reading a file and playing mp3 happens behind the scenes. There are drafts of code for working with the database, but after the lack of audio, I was pleasantly surprised that localStorage is implemented in Bada .

    Here, in fact, he very briefly described the goal and implementation, and the prepared project can be downloaded / viewed here: Assembla

    The code is provided for reference only, and not because of copyright, but because of security / stability / productivity considerations, because it is still very damp.

    I hope I helped someone in writing applications for Bada. Although using html5 / css / javascript you can easily port them to other platforms and vice versa.

    Read Next