NFC Write Sample

Please, i need sample solution of how to write users input data to an NFC Tag, All i have try was not successful, All i could do is to add code the users data.

This is all i have done

HTML

<ion-view view-title="Chats" data-ng-controller="chat as vm">
<ion-content>

    <div class="list">
        <label class="item item-input">
            <span class="input-label">Username</span>
            <input type="text" ng-model="userData.userName">
        </label>

        <label class="item item-input">
            <span class="input-label">Description</span>
            <input type="text" ng-model="userData.description">
        </label>
    </div>

    <fieldset style="clear:both;">
        <input data-ng-click="login()" type="submit" name="submit"
               value="Login" class="more blue" />
    </fieldset>

    {{userData}}
</ion-content>

The Controller:

.controller(‘chat’, function($rootScope, $scope, $cordovaNfc, Chats, nfcService) {

$scope.loginData = {};

$cordovaNfc.then(function(nfc){

    // I need to find a way to pass in the userData this point
    console.log($scope.loginData); // Here am not getting the data;
    var payloadData = $scope.loginData; // And i did get anything in the payload

    //Use the plugins interface as you go, in a more "angular" way
    nfc.addNdefListener(function(nfcEvent){

        var tnf = ndef.TNF_EXTERNAL_TYPE,   
            recordType = "application/json", 
            payload = payloadData,                
            record,                               
            message = [
               ndef.mimeMediaRecord(recordType, payload)  
            ];            

        nfc.write(
           message,   // write the record itself to the tag
           function (success) { 
               console.log("Wrote data to tag.");
           },
           // this function runs if the write command fails:
           function (reason) {
               alert("There was a problem " + reason);
           }
        );

    }).then(
    //Success callback
    function(nfcEvent){
        console.log("bound success");
    },
    //Fail callback
    function(err){
        console.log("error");
        alert("error");
    });
});

})

Thanks.

I will appreciate if someone could please give me a simple sample that can pass the scope data to the nfc.write.

Thanks

Hey i tested something like this:

i built a gist for you… keep in mind i was using com.chariotsolutions.nfc.plugin plugin

Thanks for your response, I so much appreciate it, I will look into it and work on it.

Thanks.

One things i noticed from the example posted,

  1. The write payload return undefined
  2. The write section of the nfc.write console.log(‘written’); is not being triggered, so i added alert(‘written’); nothing happen.

Please help me review it, because i need the callback function to process other functions.

Thanks.