1

I deployed a smart contract with solidity along with a GUI by Javascript and HTML. This project works well on my computer.

Now I want to invoke functions of Smart Contract from Android phone.

Is there any possibility to do this? And if so, how to do?

Ans also do I need to write my smart contract with Java ? or Solidity version is OK ?

My solidity smart contract code :

pragma solidity 0.4.23; 

contract RFID {

    struct StateStruct {
        bytes32 description;
        mapping(bytes32 => bytes32) sub_state;
    }

    struct ObjectStruct {
        StateStruct state;
        address owner; 
        bool isObject;

        bytes32 review;
    }

    mapping(bytes32 => ObjectStruct) objectStructs;
    bytes32[] public objectList;

    event LogNewObject(address sender, bytes32 indexed id, bytes32 sub_states_types, bytes32 sub_states_values, address owner);
    event LogChangeObjectState(address sender, bytes32 indexed id, bytes32 sub_states_types, bytes32 sub_states_values);
    event LogChangeObjectOwner(address sender, bytes32 indexed id, address newOwner);

    event LogNewObjectReview(address sender, bytes32 indexed _id, bytes32 _review,  address _owner);
    event LogChangeObjectStateReview(address sender, bytes32 indexed id, bytes32 _review);

    function isObject(bytes32 _id) public view returns(bool isIndeed) {
        return objectStructs[_id].isObject;
    }

    function getObjectCount() public view returns(uint count) {
        return objectList.length;
    }

    /*function setArraySize(uint256 _number_of_sub_states) public {

        number_of_sub_states = _number_of_sub_states;

    }

    function getArraySize() view public returns (uint256) {
       return number_of_sub_states;
    }*/

    function newObject(bytes32 _id, uint256 number_of_sub_states, bytes32[10] sub_states_types, bytes32[10] sub_states_values, address _owner) public returns(bool success) {
        require(!isObject(_id));

        uint256 counter=0;
        for(counter; counter < number_of_sub_states; counter++) {

            objectStructs[_id].state.sub_state[sub_states_types[counter]] = sub_states_values[counter];

            emit LogNewObject(msg.sender, _id, bytes32(sub_states_types[counter]), bytes32(sub_states_values[counter]), _owner);

        }

        objectStructs[_id].owner = _owner;
        objectStructs[_id].isObject = true;

        objectList.push(_id);

        return true;
    }

    function newObjectReview(bytes32 _id, bytes32 _review,  address _owner) public returns(bool success) {
        require(!isObject(_id));

        objectStructs[_id].owner = _owner;
        objectStructs[_id].isObject = true;

        objectStructs[_id].review = _review;

        emit LogNewObjectReview(msg.sender, _id, _review, _owner);

        objectList.push(_id);

        return true;
    }

    function changeObjectState(bytes32 _id, uint256 number_of_sub_states, bytes32[10] sub_states_types, bytes32[10] sub_states_values) public returns(bool success) {
        require(isObject(_id));
        uint256 counter=0;
        for(counter; counter < number_of_sub_states; counter++) {

            objectStructs[_id].state.sub_state[sub_states_types[counter]] = sub_states_values[counter];

            emit LogChangeObjectState(msg.sender, _id, bytes32(sub_states_types[counter]), bytes32(sub_states_values[counter]));

        }

        return true;
    }

    function changeObjectStateReview(bytes32 _id, bytes32 _review) public returns(bool success) {
        require(isObject(_id));

        objectStructs[_id].review = _review;

        emit LogChangeObjectStateReview(msg.sender, _id, _review);

        return true;
    }

    function changeObjectOwner(bytes32 _id, address _newOwner) public returns(bool success) {
        require(isObject(_id));
        objectStructs[_id].owner = _newOwner;
        emit LogChangeObjectOwner(msg.sender, _id, _newOwner);
        return true;
    }

}

What I want to do is invoking functions of my contract by android phone.

Note : I did sign up for Infura, however honestly I do not know how to use it on my android phone to invoke functions of this smart contract from my android phone ?

5 Answers 5

3

Two things you need for this:

  1. Infura
  2. Web3js

Infura will allow you to connect to the ethereum network (and is free). Then you can use web3 to interact with your contract.

Hope it helps.

4
  • Thank you. I did sign up for Infura, however honestly I do not know how to use it on my android phone ? BTW, the second requirement you mentioned in your answer Web3js, do you mean Web3j or Web3js ? Thanks.
    – Questioner
    Commented May 11, 2018 at 22:28
  • Do I need to install Infura on my android phone ? Thanks
    – Questioner
    Commented May 11, 2018 at 22:50
  • Here you can find how to get started with infura and web3 (go to the example of creating a website that connects t the ethereum network). If I answer your question, please accept the answer.
    – Jaime
    Commented May 12, 2018 at 8:00
  • Web3j and Web3js are both viable options, depending on whether you want the app to be native or HTML. But be aware that if your users can make transactions (rather than just making read-only calls), then you'll need to store their private keys in your app, which makes it much more attractive to hackers.
    – James_pic
    Commented May 31, 2018 at 15:29
2

If you are asking how to run DApps from an Android mobile, you can load the Toshi app. It's basically a browser with a built in wallet and an Ethereum node in the back and will run all your web pages based on web3.js.

9
  • Thank you. I installed Toshi from HERE However, I do not know how can I invoke functions of my contract to send a transaction ? I added my contract source code to my question. Thanks.
    – Questioner
    Commented May 11, 2018 at 22:44
  • Basically the same way as you do it on your PC. You just need to deploy your code somewhere on a server to access it from Toshi. Could be a web server on your PC, like XAMPP for example.
    – gisdev_p
    Commented May 12, 2018 at 6:04
  • Thank you, In fact I deployed my contract on a linux server with ip address : 18.221.104.179 .However I don't know how to find my contract in Toshi ? Thanks again.
    – Questioner
    Commented May 12, 2018 at 13:59
  • Just enter the URL to your HTML file that interfaces to your smart contract into the field that says "Search or enter DApp URL". So in your case it should be 18.221.104.179/<your HTML file>
    – gisdev_p
    Commented May 12, 2018 at 15:13
  • Thank you, but when i want to go to 18.221.104.179 i receive 502 bad gateway screen shot meanwhile i honestly did not understand exactly what Toshi does ? I mean when we upload an index.html file on a server, it is not possible to interact with smart contract by ex. google chrome browser instead of Toshi ? Thanks
    – Questioner
    Commented May 12, 2018 at 15:40
1

You should look into web3j over at https://web3j.io/ They have a Java library that interacts with the Ethereum blockchain similar to the JavaScript web3.js library. It has an Android version as well as a vanilla Java version.

1

Geth can be run both on Android as well as iOS. The nightly builds of the develop branch always include the Android .aar archives that you can link against an Android Studio project and the iOS framework that you can use in XCode.

Since Geth does not yet feature the light client functionality (that one actually needs Ethereum global P2P protocol updates), it's quite heavyweight, but it will be the official way to run mobile DApps, and AFAIK only Geth has even a remote concept about light client support.

More info can be found at this question.

0

If you are trying to connect to the Ethereum network from a native mobile application, check out this Geth tuorial for Android. This is their Github link.

I have also encountered this library, Ethereum Android, but unsure of its capabilities yet.

A third option that I found promising is Web3j.

Although they all look like very promising and there have been development on them for quite some time it is a little weird that there aren't enough tutorials and documentations about them or even implementations on mobile apps that connect to the Ethereum Network.

I am also pursuing the same goal so feel free to contact me further.

Edit: So after further research and referring to Jaime's answer, I was able to use web3j library to create Java objects of my smart contract provided that I had the source code and web3j also connects me to the client which in my case was an Infura client which is a link that you get when you sign up with Infura for free. I was able to use Java objects, which represented the smart contract, and invoking its methods which interacted with the Ethereum main net (or test net if that's what you want).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.