Home | History | Annotate | Download | only in nfc
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.nfc;
     18 
     19 import android.util.Log;
     20 
     21 /**
     22  * Native interface to the NFC tag functions
     23  */
     24 public class NativeNfcTag {
     25     static final boolean DBG = false;
     26 
     27     private int mHandle;
     28 
     29     private String mType;
     30 
     31     private byte[] mPollBytes;
     32 
     33     private byte[] mActivationBytes;
     34 
     35     private byte[] mUid;
     36 
     37     private final String TAG = "NativeNfcTag";
     38 
     39     private PresenceCheckWatchdog mWatchdog;
     40     private class PresenceCheckWatchdog extends Thread {
     41 
     42         private boolean isPresent = true;
     43         private boolean isRunning = true;
     44 
     45         public void reset() {
     46             this.interrupt();
     47         }
     48 
     49         public void end() {
     50             isRunning = false;
     51             this.interrupt();
     52         }
     53 
     54         @Override
     55         public void run() {
     56             if (DBG) Log.d(TAG, "Starting background presence check");
     57             while (isPresent && isRunning) {
     58                 try {
     59                     Thread.sleep(1000);
     60                     isPresent = doPresenceCheck();
     61                 } catch (InterruptedException e) {
     62                     // Activity detected, loop
     63                 }
     64             }
     65             // Restart the polling loop if the tag is not here any more
     66             if (!isPresent) {
     67                 Log.d(TAG, "Tag lost, restarting polling loop");
     68                 doDisconnect();
     69             }
     70             if (DBG) Log.d(TAG, "Stopping background presence check");
     71         }
     72     }
     73 
     74     private native boolean doConnect();
     75     public synchronized boolean connect() {
     76         boolean isSuccess = doConnect();
     77         if (isSuccess) {
     78             mWatchdog = new PresenceCheckWatchdog();
     79             mWatchdog.start();
     80         }
     81         return isSuccess;
     82     }
     83 
     84     private native boolean doDisconnect();
     85     public synchronized boolean disconnect() {
     86         if (mWatchdog != null) {
     87             mWatchdog.end();
     88         }
     89         return doDisconnect();
     90     }
     91 
     92     private native byte[] doTransceive(byte[] data);
     93     public synchronized byte[] transceive(byte[] data) {
     94         if (mWatchdog != null) {
     95             mWatchdog.reset();
     96         }
     97         return doTransceive(data);
     98     }
     99 
    100     private native boolean doCheckNdef();
    101     public synchronized boolean checkNdef() {
    102         if (mWatchdog != null) {
    103             mWatchdog.reset();
    104         }
    105         return doCheckNdef();
    106     }
    107 
    108     private native byte[] doRead();
    109     public synchronized byte[] read() {
    110         if (mWatchdog != null) {
    111             mWatchdog.reset();
    112         }
    113         return doRead();
    114     }
    115 
    116     private native boolean doWrite(byte[] buf);
    117     public synchronized boolean write(byte[] buf) {
    118         if (mWatchdog != null) {
    119             mWatchdog.reset();
    120         }
    121         return doWrite(buf);
    122     }
    123 
    124     private native boolean doPresenceCheck();
    125     public synchronized boolean presenceCheck() {
    126         if (mWatchdog != null) {
    127             mWatchdog.reset();
    128         }
    129         return doPresenceCheck();
    130     }
    131 
    132     private NativeNfcTag() {
    133     }
    134 
    135     public int getHandle() {
    136         return mHandle;
    137     }
    138 
    139     public String getType() {
    140         return mType;
    141     }
    142 
    143     public byte[] getUid() {
    144         return mUid;
    145     }
    146 
    147     public byte[] getPollBytes() {
    148         return mPollBytes;
    149     }
    150 
    151     public byte[] getActivationBytes() {
    152         return mActivationBytes;
    153     }
    154 
    155 }
    156