NFC Write Sample

1.9k Views Asked by At

Please, i have a problem passing $scope data into an nfc.write function, but if i pass in a literal hard code value it write, 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 from $scope 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($scope, $cordovaNfc) {

$scope.loginData = {}; // This should get the data from view to be used below

$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.

1

There are 1 best solutions below

0
On

In the template your model is userData (=> $scope.userData). In your controller you want to write $scope.loginData to console what is not defined (or NULL maybe). So both your log and payloadData variable will be empty.