Question: I have an app that allows users to buy things from my store, how can i interface with Mycelium for payments? Is there an API or something?
First, please test this using our testnet builds.of course,there you must use testnet addresses.
https://play.google.com/store/apps/details?id=com.mycelium.testnetwalletthe code samples were written without running them once
- so there might be an error
The most straightforward way to do it, is create a bitcoin uri.
see
https://github.com/bitcoin/bips/blob/master/bip-0021.mediawiki for example
bitcoin:15f3mXNSY1oBnhaZDb7ifEYJvfg62XTtYe?amount=0.12345678(we currently ignore label and message, this is a planned feature)
next, you send out a URI intent
String uri = "bitcoin:15f3mXNSY1oBnhaZDb7ifEYJvfg62XTtYe?amount=0.12345678";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(bitcoinuri));
this.startActivityForResult(intent,SOME_RESULT_CODE);
after a successful transaction you can check the transaction id in onActivityResult
String transaction = data.getStringExtra("transaction_hash");
i just wrote a full sample for you ready to copy-paste.
also, note that the same code should work with any decent android wallet, it is not mycelium-specific
(note: this should go into the FAQ)
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class SimpleBitcoinIntegration extends Activity {
private static final int MY_RESULT_CODE = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
//todo create UI as usual
Button button = (Button) findViewById(R.id.btSend);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String bitcoinuri = "bitcoin:15f3mXNSY1oBnhaZDb7ifEYJvfg62XTtYe?amount=0.12345678";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(bitcoinuri));
startActivityForResult(intent, MY_RESULT_CODE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if ((requestCode == MY_RESULT_CODE) && (resultCode == RESULT_OK)) {
String transaction = data.getStringExtra("transaction_hash");
Toast.makeText(this, "received bitcoins with txid: " + transaction, Toast.LENGTH_LONG).show();
} else {
//something else happened..
}
}
}