NFC API revision round 2.

- Add the second half of the new NFC API: NfcAdapter, Tag, NdefTag,
  RawTagConnection, NdefTagConnection.
- Add implementations for all of the new NFC API.

Change-Id: I8da9fd1d2971ed2c8b76f2a93f207fa9e305b0f6
Signed-off-by: Nick Pelly <npelly@google.com>
This commit is contained in:
Nick Pelly
2010-10-12 13:00:50 -07:00
parent 45738f6700
commit 590b73bc5b
48 changed files with 2439 additions and 2157 deletions

View File

@ -1,34 +0,0 @@
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.trustedlogic.trustednfc.android;
import com.trustedlogic.trustednfc.android.LlcpPacket;
/**
* TODO
*
* {@hide}
*/
interface ILlcpConnectionlessSocket
{
void close(int nativeHandle);
int getSap(int nativeHandle);
LlcpPacket receiveFrom(int nativeHandle);
int sendTo(int nativeHandle, in LlcpPacket packet);
}

View File

@ -1,32 +0,0 @@
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.trustedlogic.trustednfc.android;
/**
* TODO
*
* {@hide}
*/
interface ILlcpServiceSocket
{
int accept(int nativeHandle);
void close(int nativeHandle);
int getAcceptTimeout(int nativeHandle);
void setAcceptTimeout(int nativeHandle, int timeout);
}

View File

@ -1,41 +0,0 @@
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.trustedlogic.trustednfc.android;
/**
* TODO
*
* {@hide}
*/
interface ILlcpSocket
{
int close(int nativeHandle);
int connect(int nativeHandle, int sap);
int connectByName(int nativeHandle, String sn);
int getConnectTimeout(int nativeHandle);
int getLocalSap(int nativeHandle);
int getLocalSocketMiu(int nativeHandle);
int getLocalSocketRw(int nativeHandle);
int getRemoteSocketMiu(int nativeHandle);
int getRemoteSocketRw(int nativeHandle);
int receive(int nativeHandle, out byte[] receiveBuffer);
int send(int nativeHandle, in byte[] data);
void setConnectTimeout(int nativeHandle, int timeout);
}

View File

@ -1,32 +0,0 @@
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.trustedlogic.trustednfc.android;
import com.trustedlogic.trustednfc.android.NdefMessage;
/**
* TODO
*
* {@hide}
*/
interface INdefTag
{
NdefMessage read(int nativeHandle);
boolean write(int nativeHandle, in NdefMessage msg);
}

View File

@ -1,61 +0,0 @@
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.trustedlogic.trustednfc.android;
import com.trustedlogic.trustednfc.android.ILlcpSocket;
import com.trustedlogic.trustednfc.android.ILlcpServiceSocket;
import com.trustedlogic.trustednfc.android.ILlcpConnectionlessSocket;
import com.trustedlogic.trustednfc.android.INfcTag;
import com.trustedlogic.trustednfc.android.IP2pTarget;
import com.trustedlogic.trustednfc.android.IP2pInitiator;
/**
* Interface that allows controlling NFC activity.
*
* {@hide}
*/
interface INfcManager
{
ILlcpSocket getLlcpInterface();
ILlcpConnectionlessSocket getLlcpConnectionlessInterface();
ILlcpServiceSocket getLlcpServiceInterface();
INfcTag getNfcTagInterface();
IP2pTarget getP2pTargetInterface();
IP2pInitiator getP2pInitiatorInterface();
void cancel();
int createLlcpConnectionlessSocket(int sap);
int createLlcpServiceSocket(int sap, String sn, int miu, int rw, int linearBufferLength);
int createLlcpSocket(int sap, int miu, int rw, int linearBufferLength);
int deselectSecureElement();
boolean disable();
boolean enable();
int getOpenTimeout();
String getProperties(String param);
int[] getSecureElementList();
int getSelectedSecureElement();
boolean isEnabled();
int openP2pConnection();
int openTagConnection();
int selectSecureElement(int seId);
void setOpenTimeout(int timeout);
int setProperties(String param, String value);
}

View File

@ -1,38 +0,0 @@
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.trustedlogic.trustednfc.android;
import com.trustedlogic.trustednfc.android.NdefMessage;
/**
* TODO
*
* {@hide}
*/
interface INfcTag
{
int close(int nativeHandle);
int connect(int nativeHandle);
String getType(int nativeHandle);
byte[] getUid(int nativeHandle);
boolean isNdef(int nativeHandle);
byte[] transceive(int nativeHandle, in byte[] data);
NdefMessage read(int nativeHandle);
boolean write(int nativeHandle, in NdefMessage msg);
}

View File

@ -1,32 +0,0 @@
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.trustedlogic.trustednfc.android;
/**
* TODO
*
* {@hide}
*/
interface IP2pInitiator
{
byte[] getGeneralBytes(int nativeHandle);
int getMode(int nativeHandle);
byte[] receive(int nativeHandle);
boolean send(int nativeHandle, in byte[] data);
}

View File

@ -1,33 +0,0 @@
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.trustedlogic.trustednfc.android;
/**
* TODO
*
* {@hide}
*/
interface IP2pTarget
{
byte[] getGeneralBytes(int nativeHandle);
int getMode(int nativeHandle);
int connect(int nativeHandle);
boolean disconnect(int nativeHandle);
byte[] transceive(int nativeHandle, in byte[] data);
}

View File

@ -24,7 +24,9 @@ package com.trustedlogic.trustednfc.android;
import java.io.IOException;
import com.trustedlogic.trustednfc.android.internal.ErrorCodes;
import android.nfc.ErrorCodes;
import android.nfc.ILlcpConnectionlessSocket;
import android.nfc.LlcpPacket;
import android.os.RemoteException;
import android.util.Log;
@ -32,19 +34,19 @@ import android.util.Log;
/**
* LlcpConnectionlessSocket represents a LLCP Connectionless object to be used
* in a connectionless communication
*
*
* @since AA02.01
* @hide
*/
public class LlcpConnectionlessSocket {
private static final String TAG = "LlcpConnectionlessSocket";
/**
* The handle returned by the NFC service and used to identify the LLCP connectionless socket in
* every call of this class.
*
*
* @hide
*/
protected int mHandle;
@ -52,15 +54,15 @@ public class LlcpConnectionlessSocket {
/**
* The entry point for LLCP Connectionless socket operations.
*
*
* @hide
*/
protected ILlcpConnectionlessSocket mService;
/**
* Internal constructor for the LlcpConnectionlessSocket class.
*
*
* @param service The entry point to the Nfc Service for LLCP Connectionless socket class.
* @param handle The handle returned by the NFC service and used to identify
* the socket in subsequent calls.
@ -73,7 +75,7 @@ public class LlcpConnectionlessSocket {
/**
* Send data to a specific LLCP Connectionless client
*
*
* @param packet Service Access Point number related to a LLCP
* Connectionless client and a data buffer to send
* @throws IOException if the LLCP link has been lost or deactivated.
@ -93,7 +95,7 @@ public class LlcpConnectionlessSocket {
/**
* Receive data from a LLCP Connectionless client
*
*
* @return data data received from a specific LLCP Connectionless client
* @throws IOException if the LLCP link has been lost or deactivated.
* @see LlcpPacket
@ -106,7 +108,7 @@ public class LlcpConnectionlessSocket {
return packet;
}else{
// Handle potential errors
throw new IOException();
throw new IOException();
}
} catch (RemoteException e) {
Log.e(TAG, "RemoteException in receiveFrom(): ", e);
@ -116,7 +118,7 @@ public class LlcpConnectionlessSocket {
/**
* Close the created Connectionless socket.
*
*
* @since AA02.01
*/
public void close() {
@ -129,13 +131,13 @@ public class LlcpConnectionlessSocket {
/**
* Returns the local Service Access Point number of the socket
*
*
* @return sap
* @since AA02.01
*/
public int getSap() {
int sap = 0;
try {
sap = mService.getSap(mHandle);

View File

@ -1,19 +0,0 @@
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.trustedlogic.trustednfc.android;
parcelable LlcpPacket;

View File

@ -1,119 +0,0 @@
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* File : LLCPPacket.java
* Original-Author : Trusted Logic S.A. (Daniel Tomas)
* Created : 25-02-2010
*/
package com.trustedlogic.trustednfc.android;
import android.os.Parcel;
import android.os.Parcelable;
/**
* Represents a LLCP packet received in a LLCP Connectionless communication;
*
* @since AA02.01
* @hide
*/
public class LlcpPacket implements Parcelable {
private int mRemoteSap;
private byte[] mDataBuffer;
/**
* Creator class, needed when implementing from Parcelable
* {@hide}
*/
public static final Parcelable.Creator<LlcpPacket> CREATOR = new Parcelable.Creator<LlcpPacket>() {
public LlcpPacket createFromParcel(Parcel in) {
// Remote SAP
short sap = (short)in.readInt();
// Data Buffer
int dataLength = in.readInt();
byte[] data = new byte[dataLength];
in.readByteArray(data);
return new LlcpPacket(sap, data);
}
public LlcpPacket[] newArray(int size) {
return new LlcpPacket[size];
}
};
/**
* Creates a LlcpPacket to be sent to a remote Service Access Point number
* (SAP)
*
* @param sap Remote Service Access Point number
* @param data Data buffer
* @since AA02.01
*/
public LlcpPacket(int sap, byte[] data) {
mRemoteSap = sap;
mDataBuffer = data;
}
/**
* @hide
*/
public LlcpPacket() {
}
/**
* Returns the remote Service Access Point number
*
* @return remoteSap
* @since AA02.01
*/
public int getRemoteSap() {
return mRemoteSap;
}
/**
* Returns the data buffer
*
* @return data
* @since AA02.01
*/
public byte[] getDataBuffer() {
return mDataBuffer;
}
/**
* (Parcelable) Describe the parcel
* {@hide}
*/
public int describeContents() {
return 0;
}
/**
* (Parcelable) Convert current object to a Parcel
* {@hide}
*/
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(mRemoteSap);
dest.writeInt(mDataBuffer.length);
dest.writeByteArray(mDataBuffer);
}
}

View File

@ -24,7 +24,9 @@ package com.trustedlogic.trustednfc.android;
import java.io.IOException;
import com.trustedlogic.trustednfc.android.internal.ErrorCodes;
import android.nfc.ErrorCodes;
import android.nfc.ILlcpSocket;
import android.nfc.ILlcpServiceSocket;
import android.os.RemoteException;
import android.util.Log;
@ -32,7 +34,7 @@ import android.util.Log;
/**
* LlcpServiceSocket represents a LLCP Service to be used in a
* Connection-oriented communication
*
*
* @since AA02.01
* @hide
*/
@ -43,19 +45,19 @@ public class LlcpServiceSocket {
/**
* The handle returned by the NFC service and used to identify the LLCP
* Service socket in every call of this class.
*
*
* @hide
*/
protected int mHandle;
/**
* The entry point for LLCP Service socket operations.
*
*
* @hide
*/
protected ILlcpServiceSocket mService;
private ILlcpSocket mLlcpSocketService;
private final ILlcpSocket mLlcpSocketService;
static LlcpException convertErrorToLlcpException(int errorCode) {
return convertErrorToLlcpException(errorCode, null);
@ -83,7 +85,7 @@ public class LlcpServiceSocket {
/**
* Internal constructor for the LlcpServiceSocket class.
*
*
* @param service
* The entry point to the Nfc Service for LlcpServiceSocket
* class.
@ -101,14 +103,14 @@ public class LlcpServiceSocket {
/**
* Wait for incomming connection request from a LLCP client and accept this
* request
*
*
* @return socket object to be used to communicate with a LLCP client
*
*
* @throws IOException
* if the llcp link is lost or deactivated
* @throws LlcpException
* if not enough ressources are available
*
*
* @see LlcpSocket
* @since AA02.01
*/
@ -124,7 +126,7 @@ public class LlcpServiceSocket {
throw convertErrorToLlcpException(handle);
}
}
// Build the public LlcpSocket object
return new LlcpSocket(mLlcpSocketService, handle);
} catch (RemoteException e) {
@ -136,7 +138,7 @@ public class LlcpServiceSocket {
/**
* Set the timeout for the accept request
*
*
* @param timeout
* value of the timeout for the accept request
* @since AA02.01
@ -151,7 +153,7 @@ public class LlcpServiceSocket {
/**
* Get the timeout value of the accept request
*
*
* @return mTimeout
* @since AA02.01
*/
@ -166,7 +168,7 @@ public class LlcpServiceSocket {
/**
* Close the created Llcp Service socket
*
*
* @since AA02.01
*/
public void close() {

View File

@ -24,7 +24,8 @@ package com.trustedlogic.trustednfc.android;
import java.io.IOException;
import com.trustedlogic.trustednfc.android.internal.ErrorCodes;
import android.nfc.ErrorCodes;
import android.nfc.ILlcpSocket;
import android.os.RemoteException;
import android.util.Log;
@ -32,7 +33,7 @@ import android.util.Log;
/**
* LlcpClientSocket represents a LLCP Connection-Oriented client to be used in a
* connection-oriented communication
*
*
* @since AA02.01
* @hide
*/
@ -43,14 +44,14 @@ public class LlcpSocket {
/**
* The handle returned by the NFC service and used to identify the LLCP
* socket in every call of this class.
*
*
* @hide
*/
protected int mHandle;
/**
* The entry point for LLCP socket operations.
*
*
* @hide
*/
protected ILlcpSocket mService;
@ -84,7 +85,7 @@ public class LlcpSocket {
/**
* Internal constructor for the LlcpSocket class.
*
*
* @param service
* The entry point to the Nfc Service for LlcpServiceSocket
* class.
@ -100,7 +101,7 @@ public class LlcpSocket {
/**
* Connect request to a specific LLCP Service by its SAP.
*
*
* @param sap
* Service Access Point number of the LLCP Service
* @throws IOException
@ -128,7 +129,7 @@ public class LlcpSocket {
/**
* Connect request to a specific LLCP Service by its Service Name.
*
*
* @param sn
* Service Name of the LLCP Service
* @throws IOException
@ -156,7 +157,7 @@ public class LlcpSocket {
/**
* Set the timeout for the connect request
*
*
* @param timeout
* timeout value for the connect request
* @since AA02.01
@ -171,7 +172,7 @@ public class LlcpSocket {
/**
* Get the timeout value of the connect request
*
*
* @return mTimeout
* @since AA02.01
*/
@ -187,7 +188,7 @@ public class LlcpSocket {
/**
* Disconnect request to the connected LLCP socket and close the created
* socket.
*
*
* @throws IOException
* if the LLCP has been lost or deactivated.
* @since AA02.01
@ -206,7 +207,7 @@ public class LlcpSocket {
/**
* Send data to the connected LLCP Socket.
*
*
* @throws IOException
* if the LLCP has been lost or deactivated.
* @since AA02.01
@ -220,12 +221,12 @@ public class LlcpSocket {
}
} catch (RemoteException e) {
Log.e(TAG, "RemoteException in send(): ", e);
}
}
}
/**
* Receive data from the connected LLCP socket
*
*
* @param receiveBuffer
* a buffer for the received data
* @return length length of the data received
@ -242,14 +243,14 @@ public class LlcpSocket {
}
} catch (RemoteException e) {
Log.e(TAG, "RemoteException in send(): ", e);
}
}
return receivedLength;
}
/**
* Returns the local Service Access Point number of the socket
*
*
* @return localSap
* @since AA02.01
*/
@ -264,7 +265,7 @@ public class LlcpSocket {
/**
* Returns the local Maximum Information Unit(MIU) of the socket
*
*
* @return miu
* @since AA02.01
*/
@ -279,7 +280,7 @@ public class LlcpSocket {
/**
* Returns the local Receive Window(RW) of the socket
*
*
* @return rw
* @since AA02.01
*/
@ -296,7 +297,7 @@ public class LlcpSocket {
* Returns the remote Maximum Information Unit(MIU) of the socket.
* <p>
* This method must be called when the socket is in CONNECTED_STATE
*
*
* @return remoteMiu
* @throws LlcpException
* if the LlcpClientSocket is not in a CONNECTED_STATE
@ -320,7 +321,7 @@ public class LlcpSocket {
* Returns the remote Receive Window(RW) of the connected remote socket.
* <p>
* This method must be called when the socket is in CONNECTED_STATE
*
*
* @return rw
* @throws LlcpException
* if the LlcpClientSocket is not in a CONNECTED_STATE

View File

@ -1,19 +0,0 @@
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.trustedlogic.trustednfc.android;
parcelable NdefMessage;

View File

@ -1,160 +0,0 @@
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* File : NDEFMessage.java
* Original-Author : Trusted Logic S.A. (Jeremie Corbier)
* Created : 05-10-2009
*/
package com.trustedlogic.trustednfc.android;
import java.util.LinkedList;
import java.util.ListIterator;
import android.os.Parcel;
import android.os.Parcelable;
/**
* Represents an NDEF message as specified by the <a
* href="http://www.nfc-forum.org/">NFC Forum</a>.
*
* @see NdefRecord
*
* @since AA01.04
* @hide
*/
public class NdefMessage implements Parcelable {
/* Flag values */
private static final int FLAG_MB = 0x80;
private static final int FLAG_ME = 0x40;
private static final int FLAG_CF = 0x20;
private static final int FLAG_SR = 0x10;
private static final int FLAG_IL = 0x08;
/**
* Array of {@link NdefRecord} composing this message.
*/
private NdefRecord[] mRecords;
/**
* Builds an NDEF message.
*
* @param data raw NDEF message data
*
* @throws NFCException
*/
public NdefMessage(byte[] data) throws NfcException {
if (parseNdefMessage(data) == -1)
throw new NfcException("Error while parsing NDEF message");
}
/**
* Builds an NDEF message.
*
* @param records
* an array of already created NDEF records
*/
public NdefMessage(NdefRecord[] records) {
mRecords = new NdefRecord[records.length];
System.arraycopy(records, 0, mRecords, 0, records.length);
}
/**
* Returns the NDEF message as a byte array.
*
* @return the message as a byte array
*/
public byte[] toByteArray() {
if ((mRecords == null) || (mRecords.length == 0))
return null;
byte[] msg = {};
for (int i = 0; i < mRecords.length; i++) {
byte[] record = mRecords[i].toByteArray();
byte[] tmp = new byte[msg.length + record.length];
/* Make sure the Message Begin flag is set only for the first record */
if (i == 0)
record[0] |= FLAG_MB;
else
record[0] &= ~FLAG_MB;
/* Make sure the Message End flag is set only for the last record */
if (i == (mRecords.length - 1))
record[0] |= FLAG_ME;
else
record[0] &= ~FLAG_ME;
System.arraycopy(msg, 0, tmp, 0, msg.length);
System.arraycopy(record, 0, tmp, msg.length, record.length);
msg = tmp;
}
return msg;
}
/**
* Returns an array of {@link NdefRecord} composing this message.
*
* @return mRecords
*
* @since AA02.01
*/
public NdefRecord[] getRecords(){
return mRecords;
}
private native int parseNdefMessage(byte[] data);
/**
* (Parcelable) Describe the parcel
* {@hide}
*/
public int describeContents() {
return 0;
}
/**
* (Parcelable) Convert current object to a Parcel
* {@hide}
*/
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(mRecords.length);
dest.writeTypedArray(mRecords, 0);
}
/**
* Creator class, needed when implementing from Parcelable
* {@hide}
*/
public static final Parcelable.Creator<NdefMessage> CREATOR = new Parcelable.Creator<NdefMessage>() {
public NdefMessage createFromParcel(Parcel in) {
int recordsLength = in.readInt();
NdefRecord[] records = new NdefRecord[recordsLength];
in.readTypedArray(records, NdefRecord.CREATOR);
return new NdefMessage(records);
}
public NdefMessage[] newArray(int size) {
return new NdefMessage[size];
}
};
}

View File

@ -1,19 +0,0 @@
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.trustedlogic.trustednfc.android;
parcelable NdefRecord;

View File

@ -1,293 +0,0 @@
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* File : NdefRecord.java
* Original-Author : Trusted Logic S.A. (Jeremie Corbier)
* Created : 05-10-2009
*/
package com.trustedlogic.trustednfc.android;
import android.location.Location;
import android.os.Parcel;
import android.os.Parcelable;
/**
* An NDEF record as specified by the <a href="http://www.nfc-forum.org/">NFC
* Forum</a>.
*
* @see NdefMessage
*
* @since AA01.04
* @hide
*/
public class NdefRecord implements Parcelable {
/**
* Type Name Format - Empty record
*/
public static final short TNF_EMPTY = 0x0;
/**
* Type Name Format - NFC Forum-defined type
*/
public static final short TNF_WELL_KNOWN_TYPE = 0x1;
/**
* Type Name Format - RFC2045 MIME type
*/
public static final short TNF_MIME_MEDIA_TYPE = 0x2;
/**
* Type Name Format - Absolute URI
*/
public static final short TNF_ABSOLUTE_URI = 0x3;
/**
* Type Name Format - User-defined type
*/
public static final short TNF_EXTERNAL_TYPE = 0x4;
/**
* Type Name Format - Unknown type
*/
public static final short TNF_UNKNOWN = 0x5;
/**
* Type Name Format - Unchanged. This TNF is used for chunked records, so
* that middle records inherits from the first record's type.
*/
public static final short TNF_UNCHANGED = 0x6;
/**
* NFC Forum-defined Type - Smart Poster
*/
public static final byte[] TYPE_SMART_POSTER = { 0x53, 0x70 };
/**
* NFC Forum-defined Type - Text
*/
public static final byte[] TYPE_TEXT = { 0x54 };
/**
* NFC Forum-defined Type - URI
*/
public static final byte[] TYPE_URI = { 0x55 };
/**
* NFC Forum-defined Global Type - Connection Handover Request
*/
public static final byte[] TYPE_HANDOVER_REQUEST = { 0x48, 0x72 };
/**
* NFC Forum-defined Global Type - Connection Handover Select
*/
public static final byte[] TYPE_HANDOVER_SELECT = { 0x48, 0x73 };
/**
* NFC Forum-defined Global Type - Connection Handover Carrier
*/
public static final byte[] TYPE_HANDOVER_CARRIER = { 0x48, 0x63 };
/**
* NFC Forum-defined Local Type - Alternative Carrier
*/
public static final byte[] TYPE_ALTERNATIVE_CARRIER = { 0x61, 0x63 };
/* Flag values */
private static final int FLAG_MB = 0x80;
private static final int FLAG_ME = 0x40;
private static final int FLAG_CF = 0x20;
private static final int FLAG_SR = 0x10;
private static final int FLAG_IL = 0x08;
/**
* Record Flags
*/
private short mFlags = 0;
/**
* Record Type Name Format
*/
private short mTnf = 0;
/**
* Record Type
*/
private byte[] mType = null;
/**
* Record Identifier
*/
private byte[] mId = null;
/**
* Record Payload
*/
private byte[] mPayload = null;
/**
* Creates an NdefRecord given its Type Name Format, its type, its id and
* its.
*
* @param tnf
* Type Name Format
* @param type
* record type
* @param id
* record id (optional, can be null)
* @param data
* record payload
*/
public NdefRecord(short tnf, byte[] type, byte[] id, byte[] data) {
/* generate flag */
mFlags = FLAG_MB | FLAG_ME;
/* Determine if it is a short record */
if(data.length < 0xFF)
{
mFlags |= FLAG_SR;
}
/* Determine if an id is present */
if(id.length != 0)
{
mFlags |= FLAG_IL;
}
mTnf = tnf;
mType = (byte[]) type.clone();
mId = (byte[]) id.clone();
mPayload = (byte[]) data.clone();
}
/**
* Appends data to the record's payload.
*
* @param data
* Data to be added to the record.
*/
public void appendPayload(byte[] data) {
byte[] newPayload = new byte[mPayload.length + data.length];
System.arraycopy(mPayload, 0, newPayload, 0, mPayload.length);
System.arraycopy(data, 0, newPayload, mPayload.length, data.length);
mPayload = newPayload;
}
/**
* Returns record as a byte array.
*
* @return record as a byte array.
*/
public byte[] toByteArray() {
return generate(mFlags, mTnf, mType, mId, mPayload);
}
private native byte[] generate(short flags, short tnf, byte[] type,
byte[] id, byte[] data);
/**
* (Parcelable) Describe the parcel
* {@hide}
*/
public int describeContents() {
return 0;
}
/**
* (Parcelable) Convert current object to a Parcel
* {@hide}
*/
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(mTnf);
dest.writeInt(mType.length);
dest.writeByteArray(mType);
dest.writeInt(mId.length);
dest.writeByteArray(mId);
dest.writeInt(mPayload.length);
dest.writeByteArray(mPayload);
}
/**
* Creator class, needed when implementing from Parcelable
* {@hide}
*/
public static final Parcelable.Creator<NdefRecord> CREATOR = new Parcelable.Creator<NdefRecord>() {
public NdefRecord createFromParcel(Parcel in) {
// TNF
short tnf = (short)in.readInt();
// Type
int typeLength = in.readInt();
byte[] type = new byte[typeLength];
in.readByteArray(type);
// ID
int idLength = in.readInt();
byte[] id = new byte[idLength];
in.readByteArray(id);
// Payload
int payloadLength = in.readInt();
byte[] payload = new byte[payloadLength];
in.readByteArray(payload);
return new NdefRecord(tnf, type, id, payload);
}
public NdefRecord[] newArray(int size) {
return new NdefRecord[size];
}
};
/**
* Returns record TNF
*
* @return mTnf
*/
public int getTnf(){
return mTnf;
}
/**
* Returns record TYPE
*
* @return mType
*/
public byte[] getType(){
return mType;
}
/**
* Returns record ID
*
* @return mId
*/
public byte[] getId(){
return mId;
}
/**
* Returns record Payload
*
* @return mPayload
*/
public byte[] getPayload(){
return mPayload;
}
}

View File

@ -1,126 +0,0 @@
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* File : NDEFTag.java
* Original-Author : Trusted Logic S.A. (Jeremie Corbier)
* Created : 04-12-2009
*/
package com.trustedlogic.trustednfc.android;
import java.io.IOException;
import android.os.RemoteException;
import android.util.Log;
/**
* NdefTag represents tags complying with the NFC Forum's NFC Data Exchange
* Format.
*
* @since AA01.04
* @hide
*/
public class NdefTag extends NfcTag {
private static final String TAG = "NdefTag";
public NdefTag(NfcTag tag){
super(tag.mService,tag.mHandle);
this.isConnected = tag.isConnected;
this.isClosed = tag.isClosed;
tag.isClosed = false;
}
/**
* Internal constructor for the NfcNdefTag class.
*
* @param service The entry point to the Nfc Service for NfcNdefTag class.
* @param handle The handle returned by the NFC service and used to identify
* the tag in subsequent calls.
* @hide
*/
NdefTag(INfcTag service, int handle) {
super(service, handle);
}
/**
* Read NDEF data from an NDEF tag.
*
* @return the NDEF message read from the tag.
* @throws NfcException if the tag is not NDEF-formatted.
* @throws IOException if the target has been lost or the connection has
* been closed.
* @see NdefMessage
*/
public NdefMessage read() throws NfcException, IOException {
// Check state
checkState();
//Check if the tag is Ndef compliant
if(isNdef != true){
isNdef = isNdef();
if(isNdef != true) {
throw new NfcException("Tag is not NDEF compliant");
}
}
// Perform transceive
try {
NdefMessage msg = mService.read(mHandle);
if (msg == null) {
throw new IOException("NDEF read failed");
}
return msg;
} catch (RemoteException e) {
Log.e(TAG, "RemoteException in read(): ", e);
return null;
}
}
/**
* Write NDEF data to an NDEF-compliant tag.
*
* @param msg NDEF message to be written to the tag.
* @throws NfcException if the tag is not NDEF formatted.
* @throws IOException if the target has been lost or the connection has
* been closed.
* @see NdefMessage
*/
public void write(NdefMessage msg) throws NfcException, IOException {
// Check state
checkState();
//Check if the tag is Ndef compliant
if(isNdef != true){
isNdef = isNdef();
if(isNdef != true) {
throw new NfcException("Tag is not NDEF compliant");
}
}
// Perform transceive
try {
boolean isSuccess = mService.write(mHandle, msg);
if (!isSuccess) {
throw new IOException("NDEF write failed");
}
} catch (RemoteException e) {
Log.e(TAG, "RemoteException in write(): ", e);
}
}
}

View File

@ -1,656 +0,0 @@
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* File : NfcManager.java
* Original-Author : Trusted Logic S.A. (Jeremie Corbier)
* Created : 26-08-2009
*/
package com.trustedlogic.trustednfc.android;
import java.io.IOException;
import com.trustedlogic.trustednfc.android.internal.ErrorCodes;
import android.annotation.SdkConstant;
import android.annotation.SdkConstant.SdkConstantType;
import android.media.MiniThumbFile;
import android.os.Handler;
import android.os.RemoteException;
import android.util.Log;
//import android.util.Log;
/**
* This class provides the primary API for managing all aspects of NFC. Get an
* instance of this class by calling
* Context.getSystemService(Context.NFC_SERVICE).
* @hide
*/
public final class NfcManager {
/**
* Tag Reader Discovery mode
*/
private static final int DISCOVERY_MODE_TAG_READER = 0;
/**
* NFC-IP1 Peer-to-Peer mode Enables the manager to act as a peer in an
* NFC-IP1 communication. Implementations should not assume that the
* controller will end up behaving as an NFC-IP1 target or initiator and
* should handle both cases, depending on the type of the remote peer type.
*/
private static final int DISCOVERY_MODE_NFCIP1 = 1;
/**
* Card Emulation mode Enables the manager to act as an NFC tag. Provided
* that a Secure Element (an UICC for instance) is connected to the NFC
* controller through its SWP interface, it can be exposed to the outside
* NFC world and be addressed by external readers the same way they would
* with a tag.
* <p>
* Which Secure Element is exposed is implementation-dependent.
*
* @since AA01.04
*/
private static final int DISCOVERY_MODE_CARD_EMULATION = 2;
/**
* Used as Parcelable extra field in
* {@link com.trustedlogic.trustednfc.android.NfcManager#NDEF_TAG_DISCOVERED_ACTION}
* . It contains the NDEF message read from the NDEF tag discovered.
*/
public static final String NDEF_MESSAGE_EXTRA = "com.trustedlogic.trustednfc.android.extra.NDEF_MESSAGE";
/**
* Broadcast Action: a NDEF tag has been discovered.
* <p>
* Always contains the extra field
* {@link com.trustedlogic.trustednfc.android.NfcManager#NDEF_MESSAGE_EXTRA}.
* <p class="note">
* <strong>Note:</strong> Requires the NFC_NOTIFY permission.
*/
@SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
public static final String NDEF_TAG_DISCOVERED_ACTION = "com.trustedlogic.trustednfc.android.action.NDEF_TAG_DISCOVERED";
/**
* Used as byte array extra field in
* {@link com.trustedlogic.trustednfc.android.NfcManager#TRANSACTION_DETECTED_ACTION}
* . It contains the AID of the applet concerned by the transaction.
*/
public static final String AID_EXTRA = "com.trustedlogic.trustednfc.android.extra.AID";
/**
* Broadcast Action: a transaction with a secure element has been detected.
* <p>
* Always contains the extra field
* {@link com.trustedlogic.trustednfc.android.NfcManager#AID_EXTRA}
* <p class="note">
* <strong>Note:</strong> Requires the NFC_NOTIFY permission
*/
@SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
public static final String TRANSACTION_DETECTED_ACTION = "com.trustedlogic.trustednfc.android.action.TRANSACTION_DETECTED";
/**
* LLCP link status: The LLCP link is activated.
*
* @since AA02.01
*/
public static final int LLCP_LINK_STATE_ACTIVATED = 0;
/**
* LLCP link status: The LLCP link is deactivated.
*
* @since AA02.01
*/
public static final int LLCP_LINK_STATE_DEACTIVATED = 1;
/**
* Used as int extra field in
* {@link com.trustedlogic.trustednfc.android.NfcManager#LLCP_LINK_STATE_CHANGED_ACTION}
* . It contains the new state of the LLCP link.
*/
public static final String LLCP_LINK_STATE_CHANGED_EXTRA = "com.trustedlogic.trustednfc.android.extra.LLCP_LINK_STATE";
/**
* Broadcast Action: the LLCP link state changed.
* <p>
* Always contains the extra field
* {@link com.trustedlogic.trustednfc.android.NfcManager#LLCP_LINK_STATE_CHANGED_EXTRA}.
* <p class="note">
* <strong>Note:</strong> Requires the NFC_LLCP permission.
*/
@SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
public static final String LLCP_LINK_STATE_CHANGED_ACTION = "com.trustedlogic.trustednfc.android.action.LLCP_LINK_STATE_CHANGED";
private static final String TAG = "NfcManager";
private Handler mHandler;
private INfcManager mService;
private INfcTag mNfcTagService;
private IP2pTarget mP2pTargetService;
private IP2pInitiator mP2pInitiatorService;
private ILlcpSocket mLlcpSocketService;
private ILlcpConnectionlessSocket mLlcpConnectionlessSocketService;
private ILlcpServiceSocket mLlcpServiceSocketService;
static NfcException convertErrorToNfcException(int errorCode) {
return convertErrorToNfcException(errorCode, null);
}
static NfcException convertErrorToNfcException(int errorCode, String message) {
if (message == null) {
message = "";
} else {
message = " (" + message + ")";
}
switch (errorCode) {
case ErrorCodes.ERROR_BUSY:
return new NfcException("Another operation is already pending" + message);
case ErrorCodes.ERROR_CANCELLED:
return new NfcException("Operation cancelled" + message);
case ErrorCodes.ERROR_TIMEOUT:
return new NfcException("Operation timed out" + message);
case ErrorCodes.ERROR_SOCKET_CREATION:
return new NfcException("Error during the creation of an Llcp socket:" + message);
case ErrorCodes.ERROR_SAP_USED:
return new NfcException("Error SAP already used:" + message);
case ErrorCodes.ERROR_SERVICE_NAME_USED:
return new NfcException("Error Service Name already used:" + message);
case ErrorCodes.ERROR_SOCKET_OPTIONS:
return new NfcException("Error Socket options:" + message);
case ErrorCodes.ERROR_INVALID_PARAM:
return new NfcException("Error Set Properties: invalid param" + message);
case ErrorCodes.ERROR_NFC_ON:
return new NfcException("Error Set Properties : NFC is ON" + message);
case ErrorCodes.ERROR_NOT_INITIALIZED:
return new NfcException("NFC is not running " + message);
case ErrorCodes.ERROR_SE_ALREADY_SELECTED:
return new NfcException("Secure Element already connected" + message);
case ErrorCodes.ERROR_NO_SE_CONNECTED:
return new NfcException("No Secure Element connected" + message);
case ErrorCodes.ERROR_SE_CONNECTED:
return new NfcException("A secure Element is already connected" + message);
default:
return new NfcException("Unkown error code " + errorCode + message);
}
}
/**
* @hide
*/
public NfcManager(INfcManager service, Handler handler) {
mService = service;
mHandler = handler;
try {
mNfcTagService = mService.getNfcTagInterface();
mP2pInitiatorService = mService.getP2pInitiatorInterface();
mP2pTargetService = mService.getP2pTargetInterface();
mLlcpServiceSocketService = mService.getLlcpServiceInterface();
mLlcpConnectionlessSocketService = mService.getLlcpConnectionlessInterface();
mLlcpSocketService = mService.getLlcpInterface();
} catch (RemoteException e) {
mLlcpSocketService = null;
mNfcTagService = null;
mP2pInitiatorService = null;
mP2pTargetService = null;
mLlcpConnectionlessSocketService = null;
mLlcpServiceSocketService = null;
}
}
/**
* Return the status of the NFC feature
*
* @return mIsNfcEnabled
* @since AA02.01
*/
public boolean isEnabled() {
try {
return mService.isEnabled();
} catch (RemoteException e) {
Log.e(TAG, "RemoteException in isEnabled(): ", e);
return false;
}
}
/**
* Enable the NFC Feature
* <p class="note">
* <strong>Note:</strong> Requires the NFC_ADMIN permission
*
* @throws NfcException if the enable failed
* @since AA02.01
*/
public void enable() throws NfcException {
try {
boolean isSuccess = mService.enable();
if (isSuccess == false) {
throw new NfcException("NFC Service failed to enable");
}
} catch (RemoteException e) {
Log.e(TAG, "RemoteException in enable(): ", e);
}
}
/**
* Disable the NFC feature
* <p class="note">
* <strong>Note:</strong> Requires the NFC_ADMIN permission
*
* @throws NfcException if the disable failed
* @since AA02.01
*/
public void disable() throws NfcException {
try {
boolean isSuccess = mService.disable();
if (isSuccess == false) {
throw new NfcException("NFC Service failed to disable");
}
} catch (RemoteException e) {
Log.e(TAG, "RemoteException in disable(): ", e);
}
}
/**
* Get the list of the identifiers of the Secure Elements detected
* by the NFC controller.
*
* @return list a list of Secure Element identifiers.
* @see #getSelectedSecureElement
* @see #selectSecureElement(int)
* @see #deselectSecureElement
* @since AA02.01
*/
public int[] getSecureElementList() {
try {
return mService.getSecureElementList();
} catch (RemoteException e) {
Log.e(TAG, "RemoteException in getSecureElementList(): ", e);
return null;
}
}
/**
* Get the identifier of the currently selected secure element.
*
* @return id identifier of the currently selected Secure Element. 0 if none.
* @see #getSecureElementList
* @see #selectSecureElement(int)
* @see #deselectSecureElement
* @since AA02.01
*/
public int getSelectedSecureElement() {
try {
return mService.getSelectedSecureElement();
} catch (RemoteException e) {
Log.e(TAG, "RemoteException in getSelectedSecureElement(): ", e);
return -1;
}
}
/**
* Select a specific Secure Element by its identifier.
* <p class="note">
* <strong>Note:</strong> Requires the NFC_ADMIN permission
*
* @throws NfcException if a or this secure element is already selected
* @see #getSecureElementList
* @see #getSelectedSecureElement
* @see #deselectSecureElement
* @since AA02.01
*/
public void selectSecureElement(int seId) throws NfcException {
try {
int status = mService.selectSecureElement(seId);
if(status != ErrorCodes.SUCCESS){
throw convertErrorToNfcException(status);
}
} catch (RemoteException e) {
Log.e(TAG, "RemoteException in selectSecureElement(): ", e);
}
}
/**
* Deselect the currently selected Secure Element
* <p class="note">
* <strong>Note:</strong> Requires the NFC_ADMIN permission
*
* @throws NfcException if no secure Element is selected
* @see #getSecureElementList
* @see #getSelectedSecureElement
* @see #selectSecureElement(int)
* @since AA02.01
*/
public void deselectSecureElement() throws NfcException {
try {
int status = mService.deselectSecureElement();
if(status != ErrorCodes.SUCCESS){
throw convertErrorToNfcException(status);
}
} catch (RemoteException e) {
Log.e(TAG, "RemoteException in deselectSecureElement(): ", e);
}
}
/**
* Open a connection with a remote NFC peer
*
* This method does not return while no remote NFC peer enters the field.
* <p class="note">
* <strong>Note:</strong> Requires the NFC_RAW permission
*
* @return P2pDevice object to be used to communicate with the detected
* peer.
* @throws IOException if the target has been lost or the connection has
* been closed.
* @throws NfcException if an open is already started
* @see P2pDevice
* @see #getOpenTimeout
* @see #setOpenTimeout(int)
* @see #cancel
* @since AA02.01
*/
public P2pDevice openP2pConnection() throws IOException, NfcException {
try {
int handle = mService.openP2pConnection();
// Handle potential errors
if (ErrorCodes.isError(handle)) {
if (handle == ErrorCodes.ERROR_IO) {
throw new IOException();
} else {
throw convertErrorToNfcException(handle);
}
}
// Build the public NfcTag object, depending on its type
if (mP2pTargetService.getMode(handle) == P2pDevice.MODE_P2P_TARGET) {
return new P2pTarget(mP2pTargetService, handle);
} else {
return new P2pInitiator(mP2pInitiatorService, handle);
}
} catch (RemoteException e) {
Log.e(TAG, "RemoteException in openTagConnection(): ", e);
return null;
}
}
/**
* Open a connection with a tag
*
* This method does not return while no tag enters the field.
* <p class="note">
* <strong>Note:</strong> Requires the NFC_RAW permission
*
* @return tag object to be use to communicate with the detected NfcTag.
* @throws IOException if the target has been lost or the connection has
* been closed.
* @throws NfcException if an open is already started
* @see NfcTag
* @see #getOpenTimeout
* @see #setOpenTimeout(int)
* @see #cancel
* @since AA02.01
*/
public NfcTag openTagConnection() throws IOException, NfcException {
try {
int handle = mService.openTagConnection();
// Handle potential errors
if (ErrorCodes.isError(handle)) {
if (handle == ErrorCodes.ERROR_IO) {
throw new IOException();
} else {
throw convertErrorToNfcException(handle);
}
}
// Build the public NfcTag object
return new NfcTag(mNfcTagService, handle);
} catch (RemoteException e) {
Log.e(TAG, "RemoteException in openTagConnection(): ", e);
return null;
}
}
/**
* Set the timeout for open requests
* <p class="note">
* <strong>Note:</strong> Requires the NFC_RAW permission
*
* @param timeout value of the timeout for open request
* @see #openP2pConnection
* @see #openTagConnection
* @see #getOpenTimeout
* @since AA02.01
*/
public void setOpenTimeout(int timeout) {
try {
mService.setOpenTimeout(timeout);
} catch (RemoteException e) {
Log.e(TAG, "RemoteException in setOpenTimeout(): ", e);
}
}
/**
* Get the timeout value of open requests
*
* @return mTimeout
* @see #setOpenTimeout(int)
* @since AA02.01
*/
public int getOpenTimeout() {
try {
return mService.getOpenTimeout();
} catch (RemoteException e) {
Log.e(TAG, "RemoteException in getOpenTimeout(): ", e);
return 0;
}
}
/**
* Cancel an openTagConnection or an openP2pConnection started
* <p class="note">
* <strong>Note:</strong> Requires the NFC_RAW permission
*
* @see #openP2pConnection
* @see #openTagConnection
* @since AA02.01
*/
public void cancel() {
try {
mService.cancel();
} catch (RemoteException e) {
Log.e(TAG, "RemoteException in cancel(): ", e);
}
}
/**
* Creates a connectionless socket for a LLCP link and set its Service
* Access Point number (SAP)
* <p class="note">
* <strong>Note:</strong> Requires the NFC_LLCP permission
*
* @param sap Service Access Point number related to the created
* Connectionless socket.
* @return LlcpConnectionlessSocket object to be used in a LLCP
* Connectionless communication.
* @throws IOException if the socket creation failed
* @throws NfcException if socket ressources are insufficicent
* @see LlcpConnectionlessSocket
* @since AA02.01
*/
public LlcpConnectionlessSocket createLlcpConnectionlessSocket(int sap) throws IOException,
NfcException {
try {
int handle = mService.createLlcpConnectionlessSocket(sap);
// Handle potential errors
if (ErrorCodes.isError(handle)) {
if (handle == ErrorCodes.ERROR_IO) {
throw new IOException();
} else {
throw convertErrorToNfcException(handle);
}
}
// Build the public LlcpConnectionLess object
return new LlcpConnectionlessSocket(mLlcpConnectionlessSocketService, handle);
} catch (RemoteException e) {
Log.e(TAG, "RemoteException in createLlcpConnectionlessSocket(): ", e);
return null;
}
}
/**
* Creates a LlcpServiceSocket for a LLCP link, set its Service Access Point
* number (SAP).
* <p>
* During a LLCP communication, the LlcpServiceSocket will create LlcpSocket
* to communicate with incoming LLCP clients. For that, a server socket need
* to have some informations as a working buffer length in order to handle
* incoming data and some options to define the LLCP communication.
* <p class="note">
* <strong>Note:</strong> Requires the NFC_LLCP permission
*
* @param sap
* @param sn Service Name of the LlcpServiceSocket
* @param miu Maximum Information Unit (MIU) for a LlcpSocket created by the
* LlcpServiceSocket
* @param rw Receive Window (RW) for a LlcpSocket created by the
* LlcpServiceSocket
* @param linearBufferLength size of the memory space needed to handle
* incoming data for every LlcpSocket created.
* @return LlcpServiceSocket object to be used as a LLCP Service in a
* connection oriented communication.
* @throws IOException if the socket creation failed
* @throws NfcException if socket ressources are insufficicent
* @see LlcpServiceSocket
* @since AA02.01
*/
public LlcpServiceSocket createLlcpServiceSocket(int sap, String sn, int miu, int rw,
int linearBufferLength) throws IOException, NfcException {
try {
int handle = mService.createLlcpServiceSocket(sap, sn, miu, rw, linearBufferLength);
// Handle potential errors
if (ErrorCodes.isError(handle)) {
if (handle == ErrorCodes.ERROR_IO) {
throw new IOException();
} else {
throw convertErrorToNfcException(handle);
}
}
// Build the public LlcpServiceSocket object
return new LlcpServiceSocket(mLlcpServiceSocketService, mLlcpSocketService, handle);
} catch (RemoteException e) {
Log.e(TAG, "RemoteException in createLlcpServiceSocket(): ", e);
return null;
}
}
/**
* Creates a LlcpSocket for a LLCP link with a specific Service Access Point
* number (SAP)
* <p>
* A LlcpSocket need to have a linear buffer in order to handle incoming
* data. This linear buffer will be used to store incoming data as a stream.
* Data will be readable later.
* <p class="note">
* <strong>Note:</strong> Requires the NFC_LLCP permission
*
* @param sap Service Access Point number for the created socket
* @param miu Maximum Information Unit (MIU) of the communication socket
* @param rw Receive Window (RW) of the communication socket
* @param linearBufferLength size of the memory space needed to handle
* incoming data with this socket
* @throws IOException if the socket creation failed
* @throws NfcException if socket ressources are insufficicent
* @see LlcpSocket
* @since AA02.01
*/
public LlcpSocket createLlcpSocket(int sap, int miu, int rw, int linearBufferLength)
throws IOException, NfcException {
try {
int handle = mService.createLlcpSocket(sap, miu, rw, linearBufferLength);
// Handle potential errors
if (ErrorCodes.isError(handle)) {
if (handle == ErrorCodes.ERROR_IO) {
throw new IOException();
} else {
throw convertErrorToNfcException(handle);
}
}
// Build the public LlcpSocket object
return new LlcpSocket(mLlcpSocketService, handle);
} catch (RemoteException e) {
Log.e(TAG, "RemoteException in createLlcpSocket(): ", e);
return null;
}
}
/**
* Set different parameters like the NCIP General bytes, the LLCP link
* parameters and all tag discovery parameters.
* <p class="note">
* <strong>Note:</strong> Requires the NFC_ADMIN permission
*
* @param param parameter to be updated with a new value
* @param value new value of the parameter
* @throws NfcException if incorrect parameters of NFC is ON
* @since AA02.01
*/
public void setProperties(String param, String value) throws NfcException {
try {
int result = mService.setProperties(param, value);
// Handle potential errors
if (ErrorCodes.isError(result)) {
throw convertErrorToNfcException(result);
}
} catch (RemoteException e) {
Log.e(TAG, "RemoteException in setProperties(): ", e);
}
}
/**
* Get the value of different parameters like the NCFIP General bytes, the
* LLCP link parameters and all tag discovery parameters.
*
* @param param parameter to be updated
* @return String value of the requested parameter
* @throws RemoteException
* @since AA02.01
*/
public String getProperties(String param) {
String value;
try {
value = mService.getProperties(param);
} catch (RemoteException e) {
Log.e(TAG, "RemoteException in getProperties(): ", e);
return null;
}
return value;
}
}

View File

@ -1,250 +0,0 @@
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* File : NFCTag.java
* Original-Author : Trusted Logic S.A. (Daniel Tomas)
* Created : 26-02-2010
*/
package com.trustedlogic.trustednfc.android;
import java.io.IOException;
import android.os.RemoteException;
import android.util.Log;
import com.trustedlogic.trustednfc.android.internal.ErrorCodes;
/**
* This class represents tags with no known formatting. One can use the method
* {@link #isNdef()} to determine if the tag can store NDEF-formatted messages.
* <p>
*
* <pre class="prettyprint">
* if (tag.isNdef()) {
* NdefTag ndefTag = (NdefTag) tag;
* NdefMessage msg = ndefTag.read();
* }
* </pre>
*
* @since AA01.04
* @see NdefMessage
* @hide
*/
public class NfcTag {
private static final String TAG = "NfcTag";
/**
* The handle returned by the NFC service and used to identify the tag in
* every call of this class.
*
* @hide
*/
protected int mHandle;
/**
* The entry point for tag operations.
*
* @hide
*/
protected INfcTag mService;
/**
* Flag set when the object is closed and thus not usable any more.
*
* @hide
*/
protected boolean isClosed = false;
/**
* Flag set when the tag is connected.
*
* @hide
*/
protected boolean isConnected = false;
/**
* Flag set when a check NDEF is performed.
*
* @hide
*/
protected boolean isNdef = false;
/**
* Check if tag is still opened.
*
* @return data sent by the P2pInitiator.
* @throws NfcException if accessing a closed target.
*
* @hide
*/
public void checkState() throws NfcException {
if (isClosed) {
throw new NfcException("Tag has been closed.");
}
if (!isConnected) {
throw new NfcException("Tag is not connected.");
}
}
/**
* Internal constructor for the NfcTag class.
*
* @param service The entry point to the Nfc Service for NfcTag class.
* @param handle The handle returned by the NFC service and used to identify
* the tag in subsequent calls.
* @hide
*/
NfcTag(INfcTag service, int handle) {
this.mService = service;
this.mHandle = handle;
}
/**
* Connects to the tag. This shall be called prior to any other operation on
* the tag.
*
* @throws IOException if the tag has been lost or the connection has been
* closed.
* @throws nfcException if the tag is already in connected state.
*/
public void connect() throws NfcException, IOException {
// Check state
if (isClosed) {
throw new NfcException("Tag has been closed.");
}
if (isConnected) {
throw new NfcException("Already connected");
}
// Perform connect
try {
int result = mService.connect(mHandle);
if (ErrorCodes.isError(result)) {
if (result == ErrorCodes.ERROR_IO) {
throw new IOException("Failed to connect");
}
else {
throw NfcManager.convertErrorToNfcException(result);
}
}
isConnected = true;
} catch (RemoteException e) {
Log.e(TAG, "RemoteException in connect(): ", e);
}
}
/**
* Disconnects from the tag. This must be called so that other targets can
* be discovered. It restarts the NFC discovery loop.
*
* @throws NfcException if the tag is already in disconnected state or not connected
*/
public void close() throws NfcException {
// Check state
checkState();
try {
mService.close(mHandle);
isClosed = true;
isConnected = false;
} catch (RemoteException e) {
Log.e(TAG, "RemoteException in close(): ", e);
}
}
/**
* Exchanges raw data with the tag, whatever the tag type.
*
* To exchange APDUs with a ISO14443-4-compliant tag, the data parameter
* must be filled with the C-APDU (CLA, INS, P1, P2 [, ...]). The returned
* data consists of the R-APDU ([...,] SW1, SW2).
*
* @param data data to be sent to the tag
* @return data sent in response by the tag
* @throws IOException if the tag has been lost or the connection has been
* closed.
* @throws NfcException in case of failure within the stack
*/
public byte[] transceive(byte[] data) throws IOException, NfcException {
// Check state
checkState();
// Perform transceive
try {
byte[] response = mService.transceive(mHandle, data);
if (response == null) {
throw new IOException("Transceive failed");
}
return response;
} catch (RemoteException e) {
Log.e(TAG, "RemoteException in transceive(): ", e);
return null;
}
}
/**
* Checks whether tag is NDEF-compliant or not.
*
* @return true if the tag is NDEF-compliant, false otherwise
* @throws NfcException in case an error occurred when trying to determine
* whether the tag is NDEF-compliant
*/
public boolean isNdef() throws NfcException {
// Check state
checkState();
// Perform Check Ndef
try {
isNdef = mService.isNdef(mHandle);
return isNdef;
} catch (RemoteException e) {
Log.e(TAG, "RemoteException in isNdef(): ", e);
return false;
}
}
/**
* Returns target type. constants.
*
* @return tag type.
*/
public String getType() {
try {
return mService.getType(mHandle);
} catch (RemoteException e) {
Log.e(TAG, "RemoteException in getType(): ", e);
return null;
}
}
/**
* Returns target UID.
*
* @return tag UID.
*/
public byte[] getUid() {
try {
return mService.getUid(mHandle);
} catch (RemoteException e) {
Log.e(TAG, "RemoteException in getType(): ", e);
return null;
}
}
}

View File

@ -23,15 +23,14 @@ package com.trustedlogic.trustednfc.android;
import java.io.IOException;
import com.trustedlogic.trustednfc.android.internal.ErrorCodes;
import android.nfc.IP2pInitiator;
import android.os.RemoteException;
import android.util.Log;
/**
* P2pInitiator represents the initiator in an NFC-IP1 peer-to-peer
* communication.
*
*
* @see P2pTarget
* @since AA02.01
* @hide
@ -44,24 +43,24 @@ public class P2pInitiator extends P2pDevice {
* The entry point for P2P tag operations.
* @hide
*/
private IP2pInitiator mService;
private final IP2pInitiator mService;
/**
* Internal constructor for the P2pInitiator class.
*
*
* @param handle The handle returned by the NFC service and used to identify
* the tag in subsequent calls.
*
*
* @hide
*/
P2pInitiator(IP2pInitiator service, int handle) {
this.mService = service;
this.mHandle = handle;
}
}
/**
* Receives data from a P2pInitiator.
*
*
* @return data sent by the P2pInitiator.
* @throws IOException if the target has been lost or if the connection has
* been closed.
@ -81,7 +80,7 @@ public class P2pInitiator extends P2pDevice {
/**
* Sends data to a P2pInitiator.
*
*
* @param data data to be sent to the P2pInitiator.
* @throws IOException if the target has been lost or if the connection has
* been closed.

View File

@ -23,14 +23,14 @@ package com.trustedlogic.trustednfc.android;
import java.io.IOException;
import com.trustedlogic.trustednfc.android.internal.ErrorCodes;
import android.nfc.ErrorCodes;
import android.nfc.IP2pTarget;
import android.os.RemoteException;
import android.util.Log;
/**
* P2pTarget represents the target in an NFC-IP1 peer-to-peer communication.
*
*
* @see P2pInitiator
* @since AA02.01
* @hide
@ -43,27 +43,27 @@ public class P2pTarget extends P2pDevice {
* The entry point for P2P tag operations.
* @hide
*/
private IP2pTarget mService;
private final IP2pTarget mService;
/**
* Flag set when the object is closed and thus not usable any more.
* @hide
*/
private boolean isClosed = false;
private final boolean isClosed = false;
/**
* Flag set when the tag is connected.
* @hide
*/
private boolean isConnected = false;
/**
* Check if tag is still opened.
*
*
* @return data sent by the P2pInitiator.
* @throws NfcException if accessing a closed target.
*
* @hide
*
* @hide
*/
public void checkState() throws NfcException {
if(isClosed) {
@ -73,21 +73,21 @@ public class P2pTarget extends P2pDevice {
/**
* Internal constructor for the P2pTarget class.
*
*
* @param handle The handle returned by the NFC service and used to identify
* the tag in subsequent calls.
*
*
* @hide
*/
P2pTarget(IP2pTarget service, int handle) {
this.mService = service;
this.mHandle = handle;
}
}
/**
* Connects to the P2pTarget. This shall be called prior to any other
* operation on the P2pTarget.
*
*
* @throws NfcException
*/
public void connect() throws NfcException {
@ -96,7 +96,7 @@ public class P2pTarget extends P2pDevice {
if (isConnected) {
throw new NfcException("Already connected");
}
// Perform connect
try {
int result = mService.connect(mHandle);
@ -105,7 +105,8 @@ public class P2pTarget extends P2pDevice {
throw new NfcException("Failed to connect");
}
else {
throw NfcManager.convertErrorToNfcException(result);
// TODO(nxp)
// throw NfcAdapter.convertErrorToNfcException(result);
}
}
isConnected = true;
@ -117,7 +118,7 @@ public class P2pTarget extends P2pDevice {
/**
* Disconnects from the P2p Target. This must be called so that other
* targets can be discovered. It restarts the NFC discovery loop.
*
*
* @throws NFCException
*/
public void disconnect() throws NfcException {
@ -132,7 +133,7 @@ public class P2pTarget extends P2pDevice {
/**
* Exchanges raw data with the P2pTarget.
*
*
* @param data data to be sent to the P2pTarget
* @return data sent in response by the P2pTarget
* @throws IOException if the target has been lost or the connection has
@ -158,10 +159,11 @@ public class P2pTarget extends P2pDevice {
/**
* Get the General bytes of the connected P2P Target
*
*
* @return general bytes of the connected P2P Target
* @throws IOException if the target in not in connected state
*/
@Override
public byte[] getGeneralBytes() throws IOException {
try {
if(isConnected){

View File

@ -1,91 +0,0 @@
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* File : ErrorCodes.java
* Original-Author : Trusted Logic S.A. (Sylvain Fonteneau)
* Created : 26-02-2010
*/
package com.trustedlogic.trustednfc.android.internal;
/**
* This class defines all the error codes that can be returned by the service
* and producing an exception on the application level. These are needed since
* binders does not support exceptions.
*
* @hide
*/
public class ErrorCodes {
public static boolean isError(int code) {
if (code < 0) {
return true;
} else {
return false;
}
}
public static final int SUCCESS = 0;
public static final int ERROR_IO = -1;
public static final int ERROR_CANCELLED = -2;
public static final int ERROR_TIMEOUT = -3;
public static final int ERROR_BUSY = -4;
public static final int ERROR_CONNECT = -5;
public static final int ERROR_DISCONNECT = -5;
public static final int ERROR_READ = -6;
public static final int ERROR_WRITE = -7;
public static final int ERROR_INVALID_PARAM = -8;
public static final int ERROR_INSUFFICIENT_RESOURCES = -9;
public static final int ERROR_SOCKET_CREATION = -10;
public static final int ERROR_SOCKET_NOT_CONNECTED = -11;
public static final int ERROR_BUFFER_TO_SMALL = -12;
public static final int ERROR_SAP_USED = -13;
public static final int ERROR_SERVICE_NAME_USED = -14;
public static final int ERROR_SOCKET_OPTIONS = -15;
public static final int ERROR_NFC_ON = -16;
public static final int ERROR_NOT_INITIALIZED = -17;
public static final int ERROR_SE_ALREADY_SELECTED = -18;
public static final int ERROR_SE_CONNECTED = -19;
public static final int ERROR_NO_SE_CONNECTED = -20;
}

View File

@ -22,47 +22,47 @@
package com.trustedlogic.trustednfc.android.internal;
import com.trustedlogic.trustednfc.android.LlcpPacket;
import android.nfc.LlcpPacket;
/**
* LlcpConnectionlessSocket represents a LLCP Connectionless object to be used
* in a connectionless communication
*
*
* @since AA02.01
* {@hide}
* @hide
*/
public class NativeLlcpConnectionlessSocket {
private int mHandle;
private int mSap;
private int mLinkMiu;
public NativeLlcpConnectionlessSocket(){;
}
public NativeLlcpConnectionlessSocket(int sap){
mSap = sap;
}
private int mHandle;
private int mSap;
private int mLinkMiu;
public NativeLlcpConnectionlessSocket(){;
}
public NativeLlcpConnectionlessSocket(int sap){
mSap = sap;
}
public native boolean doSendTo(int sap, byte[] data);
public native LlcpPacket doReceiveFrom(int linkMiu);
public native boolean doClose();
public int getLinkMiu(){
return mLinkMiu;
return mLinkMiu;
}
public int getSap(){
return mSap;
return mSap;
}
public int getHandle(){
return mHandle;
return mHandle;
}
}

View File

@ -30,53 +30,53 @@ package com.trustedlogic.trustednfc.android.internal;
public class NativeLlcpServiceSocket {
private int mHandle;
private int mLocalMiu;
private int mLocalRw;
private int mLocalLinearBufferLength;
private int mSap;
private int mTimeout;
private String mServiceName;
public NativeLlcpServiceSocket(){
}
public NativeLlcpServiceSocket(String serviceName){
mServiceName = serviceName;
}
private int mHandle;
private int mLocalMiu;
private int mLocalRw;
private int mLocalLinearBufferLength;
private int mSap;
private int mTimeout;
private String mServiceName;
public NativeLlcpServiceSocket(){
}
public NativeLlcpServiceSocket(String serviceName){
mServiceName = serviceName;
}
public native NativeLlcpSocket doAccept(int timeout, int miu, int rw, int linearBufferLength);
public native boolean doClose();
public int getHandle(){
return mHandle;
return mHandle;
}
public void setAcceptTimeout(int timeout){
mTimeout = timeout;
mTimeout = timeout;
}
public int getAcceptTimeout(){
return mTimeout;
return mTimeout;
}
public int getRw(){
return mLocalRw;
return mLocalRw;
}
public int getMiu(){
return mLocalMiu;
return mLocalMiu;
}
public int getLinearBufferLength(){
return mLocalLinearBufferLength;
return mLocalLinearBufferLength;
}
}

View File

@ -25,29 +25,29 @@ package com.trustedlogic.trustednfc.android.internal;
/**
* LlcpClientSocket represents a LLCP Connection-Oriented client to be used in a
* connection-oriented communication
* {@hide}
* @hide
*/
public class NativeLlcpSocket {
private int mHandle;
private int mSap;
private int mLocalMiu;
private int mLocalRw;
private int mHandle;
private int mSap;
private int mLocalMiu;
private int mLocalRw;
private int mTimeout;
public NativeLlcpSocket(){
}
public NativeLlcpSocket(int sap, int miu, int rw){
mSap = sap;
mLocalMiu = miu;
mLocalRw = rw;
mSap = sap;
mLocalMiu = miu;
mLocalRw = rw;
}
public native boolean doConnect(int nSap, int timeout);
@ -59,35 +59,35 @@ public class NativeLlcpSocket {
public native boolean doSend(byte[] data);
public native int doReceive(byte[] recvBuff);
public native int doGetRemoteSocketMiu();
public native int doGetRemoteSocketRw();
public void setConnectTimeout(int timeout){
mTimeout = timeout;
mTimeout = timeout;
}
public int getConnectTimeout(){
return mTimeout;
return mTimeout;
}
public int getSap(){
return mSap;
return mSap;
}
public int getMiu(){
return mLocalMiu;
return mLocalMiu;
}
public int getRw(){
return mLocalRw;
return mLocalRw;
}
public int getHandle(){
return mHandle;
return mHandle;
}
}

View File

@ -25,7 +25,7 @@ package com.trustedlogic.trustednfc.android.internal;
/**
* Native interface to the NDEF tag functions
*
* {@hide}
* @hide
*/
public class NativeNdefTag {
private int mHandle;

View File

@ -24,36 +24,40 @@ package com.trustedlogic.trustednfc.android.internal;
import android.annotation.SdkConstant;
import android.annotation.SdkConstant.SdkConstantType;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import com.trustedlogic.trustednfc.android.NfcManager;
import com.trustedlogic.trustednfc.android.NdefMessage;
import com.trustedlogic.trustednfc.android.NfcTag;
import android.nfc.FormatException;
import android.nfc.NdefTag;
import android.nfc.NfcAdapter;
import android.nfc.NdefMessage;
import android.nfc.Tag;
/**
* Native interface to the NFC Manager functions {@hide}
* Native interface to the NFC Manager functions
* @hide
*/
public class NativeNfcManager {
@SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
public static final String INTERNAL_LLCP_LINK_STATE_CHANGED_EXTRA = "com.trustedlogic.trustednfc.android.extra.INTERNAL_LLCP_LINK_STATE";
@SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
public static final String INTERNAL_LLCP_LINK_STATE_CHANGED_ACTION = "com.trustedlogic.trustednfc.android.action.INTERNAL_LLCP_LINK_STATE_CHANGED";
@SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
public static final String INTERNAL_TARGET_DESELECTED_ACTION = "com.trustedlogic.trustednfc.android.action.INTERNAL_TARGET_DESELECTED";
/* Native structure */
private int mNative;
private Context mContext;
private final Context mContext;
private Handler mNfcHandler;
private final Handler mNfcHandler;
private static final String TAG = "NativeNfcManager";
@ -95,7 +99,7 @@ public class NativeNfcManager {
/**
* Disables an NFCManager mode of operation. Allows to disable tag reader,
* peer to peer initiator or target modes.
*
*
* @param mode discovery mode to enable. Must be one of the provided
* NFCManager.DISCOVERY_MODE_* constants.
*/
@ -130,45 +134,81 @@ public class NativeNfcManager {
public native boolean doActivateLlcp();
private class NfcHandler extends Handler {
private int convertType(String typeName) {
if (typeName.equals("Iso14443")) {
return Tag.NFC_TAG_ISO14443_4B;
} else if (typeName.equals("MifareUL")) {
return Tag.NFC_TAG_MIFARE;
} else if (typeName.equals("Mifare1K")) {
return Tag.NFC_TAG_MIFARE;
} else if (typeName.equals("Mifare4K")) {
return Tag.NFC_TAG_MIFARE;
} else if (typeName.equals("MifareDESFIRE")) {
return Tag.NFC_TAG_MIFARE;
} else if (typeName.equals("Unknown Mifare")) {
return Tag.NFC_TAG_MIFARE;
} else if (typeName.equals("Felica")) {
return Tag.NFC_TAG_FELICA;
} else if (typeName.equals("Jewel")) {
return Tag.NFC_TAG_JEWEL;
} else {
return Tag.NFC_TAG_OTHER;
}
}
@Override
public void handleMessage(Message msg) {
try {
switch (msg.what) {
case MSG_NDEF_TAG:
Log.d(TAG, "Checking for NDEF tag message");
NativeNfcTag tag = (NativeNfcTag) msg.obj;
if (tag.doConnect()) {
if (tag.checkNDEF()) {
byte[] buff = tag.doRead();
Log.d(TAG, "Tag detected, notifying applications");
NativeNfcTag nativeTag = (NativeNfcTag) msg.obj;
if (nativeTag.doConnect()) {
if (nativeTag.checkNDEF()) {
byte[] buff = nativeTag.doRead();
if (buff != null) {
NdefMessage msgNdef = new NdefMessage(buff);
if (msgNdef != null) {
/* Send broadcast ordered */
Intent NdefMessageIntent = new Intent();
NdefMessageIntent
.setAction(NfcManager.NDEF_TAG_DISCOVERED_ACTION);
NdefMessageIntent.putExtra(NfcManager.NDEF_MESSAGE_EXTRA,
msgNdef);
Log.d(TAG, "NDEF message found, broadcasting to applications");
mContext.sendOrderedBroadcast(NdefMessageIntent,
android.Manifest.permission.NFC_NOTIFY);
/* Disconnect tag */
tag.doAsyncDisconnect();
NdefMessage[] msgNdef = new NdefMessage[1];
try {
msgNdef[0] = new NdefMessage(buff);
NdefTag tag = new NdefTag(convertType(nativeTag.getType()), nativeTag.getUid(), nativeTag.getHandle(), msgNdef);
Intent intent = new Intent();
intent.setAction(NfcAdapter.ACTION_NDEF_TAG_DISCOVERED);
intent.putExtra(NfcAdapter.EXTRA_TAG, tag);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Log.d(TAG, "NDEF tag found, starting corresponding activity");
try {
mContext.startActivity(intent);
} catch (ActivityNotFoundException e) {
Log.w(TAG, "No activity found, disconnecting");
nativeTag.doAsyncDisconnect();
}
} catch (FormatException e) {
Log.w(TAG, "Unable to create NDEF message object (tag empty or not well formated)");
nativeTag.doAsyncDisconnect();
}
} else {
Log.w(TAG, "Unable to read NDEF message (tag empty or not well formated)");
/* Disconnect tag */
tag.doAsyncDisconnect();
Log.w(TAG, "Unable to read NDEF message (tag empty or not well formated)");
nativeTag.doAsyncDisconnect();
}
} else {
Log.d(TAG, "Tag is *not* NDEF compliant");
/* Disconnect tag */
tag.doAsyncDisconnect();
Intent intent = new Intent();
Tag tag = new Tag(convertType(nativeTag.getType()), false, nativeTag.getUid(), nativeTag.getHandle());
intent.setAction(NfcAdapter.ACTION_TAG_DISCOVERED);
intent.putExtra(NfcAdapter.EXTRA_TAG, tag);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Log.d(TAG, "Non-NDEF tag found, starting corresponding activity");
try {
mContext.startActivity(intent);
} catch (ActivityNotFoundException e) {
Log.w(TAG, "No activity found, disconnecting");
nativeTag.doAsyncDisconnect();
}
}
} else {
/* Disconnect tag */
tag.doAsyncDisconnect();
Log.w(TAG, "Failed to connect to tag");
nativeTag.doAsyncDisconnect();
}
break;
case MSG_CARD_EMULATION:
@ -176,8 +216,8 @@ public class NativeNfcManager {
byte[] aid = (byte[]) msg.obj;
/* Send broadcast ordered */
Intent TransactionIntent = new Intent();
TransactionIntent.setAction(NfcManager.TRANSACTION_DETECTED_ACTION);
TransactionIntent.putExtra(NfcManager.AID_EXTRA, aid);
TransactionIntent.setAction(NfcAdapter.ACTION_TRANSACTION_DETECTED);
TransactionIntent.putExtra(NfcAdapter.EXTRA_AID, aid);
Log.d(TAG, "Broadcasting Card Emulation event");
mContext.sendOrderedBroadcast(TransactionIntent,
android.Manifest.permission.NFC_NOTIFY);
@ -201,7 +241,7 @@ public class NativeNfcManager {
.setAction(INTERNAL_LLCP_LINK_STATE_CHANGED_ACTION);
LlcpLinkIntent.putExtra(
INTERNAL_LLCP_LINK_STATE_CHANGED_EXTRA,
NfcManager.LLCP_LINK_STATE_ACTIVATED);
NfcAdapter.LLCP_LINK_STATE_ACTIVATED);
Log.d(TAG, "Broadcasting internal LLCP activation");
mContext.sendBroadcast(LlcpLinkIntent);
}
@ -223,7 +263,7 @@ public class NativeNfcManager {
LlcpLinkIntent
.setAction(INTERNAL_LLCP_LINK_STATE_CHANGED_ACTION);
LlcpLinkIntent.putExtra(INTERNAL_LLCP_LINK_STATE_CHANGED_EXTRA,
NfcManager.LLCP_LINK_STATE_ACTIVATED);
NfcAdapter.LLCP_LINK_STATE_ACTIVATED);
Log.d(TAG, "Broadcasting internal LLCP activation");
mContext.sendBroadcast(LlcpLinkIntent);
}
@ -235,9 +275,9 @@ public class NativeNfcManager {
/* Broadcast Intent Link LLCP activated */
Log.d(TAG, "LLCP Link Deactivated message");
Intent LlcpLinkIntent = new Intent();
LlcpLinkIntent.setAction(NfcManager.LLCP_LINK_STATE_CHANGED_ACTION);
LlcpLinkIntent.putExtra(NfcManager.LLCP_LINK_STATE_CHANGED_EXTRA,
NfcManager.LLCP_LINK_STATE_DEACTIVATED);
LlcpLinkIntent.setAction(NfcAdapter.ACTION_LLCP_LINK_STATE_CHANGED);
LlcpLinkIntent.putExtra(NfcAdapter.EXTRA_LLCP_LINK_STATE_CHANGED,
NfcAdapter.LLCP_LINK_STATE_DEACTIVATED);
Log.d(TAG, "Broadcasting LLCP deactivation");
mContext.sendOrderedBroadcast(LlcpLinkIntent,
android.Manifest.permission.NFC_LLCP);

View File

@ -24,39 +24,48 @@ package com.trustedlogic.trustednfc.android.internal;
/**
* Native interface to the NFC tag functions
*
* {@hide}
*
* @hide
*/
public class NativeNfcTag {
private int mHandle;
private int mHandle;
private String mType;
private byte[] mUid;
private String mType;
public native boolean doConnect();
private byte[] mUid;
public native boolean doDisconnect();
public native void doAsyncDisconnect();
public native boolean doConnect();
public native byte[] doTransceive(byte[] data);
public native boolean doDisconnect();
public native void doAsyncDisconnect();
public native byte[] doTransceive(byte[] data);
public native boolean checkNDEF();
public native boolean checkNDEF();
public native byte[] doRead();
public native boolean doWrite(byte[] buf);
public int getHandle() {
return mHandle;
}
public String getType() {
return mType;
}
public byte[] getUid() {
return mUid;
}
private NativeNfcTag() {
}
public NativeNfcTag(int handle, String type, byte[] uid) {
mHandle = handle;
mType = type;
mUid = uid.clone();
}
public int getHandle() {
return mHandle;
}
public String getType() {
return mType;
}
public byte[] getUid() {
return mUid;
}
}

View File

@ -24,52 +24,52 @@ package com.trustedlogic.trustednfc.android.internal;
/**
* Native interface to the P2P Initiator functions
*
* {@hide}
*
* @hide
*/
public class NativeP2pDevice {
/**
* Peer-to-Peer Target.
*/
public static final short MODE_P2P_TARGET = 0x00;
/**
* Peer-to-Peer Initiator.
*/
public static final short MODE_P2P_INITIATOR = 0x01;
/**
* Peer-to-Peer Target.
*/
public static final short MODE_P2P_TARGET = 0x00;
/**
* Invalid target type.
*/
public static final short MODE_INVALID = 0xff;
/**
* Peer-to-Peer Initiator.
*/
public static final short MODE_P2P_INITIATOR = 0x01;
private int mHandle;
/**
* Invalid target type.
*/
public static final short MODE_INVALID = 0xff;
private int mMode;
private int mHandle;
private byte[] mGeneralBytes;
private int mMode;
public native byte[] doReceive();
private byte[] mGeneralBytes;
public native boolean doSend(byte[] data);
public native byte[] doReceive();
public native boolean doConnect();
public native boolean doSend(byte[] data);
public native boolean doDisconnect();
public native boolean doConnect();
public native byte[] doTransceive(byte[] data);
public int getHandle() {
return mHandle;
}
public native boolean doDisconnect();
public int getMode() {
return mMode;
}
public native byte[] doTransceive(byte[] data);
public byte[] getGeneralBytes() {
return mGeneralBytes;
}
public int getHandle() {
return mHandle;
}
public int getMode() {
return mMode;
}
public byte[] getGeneralBytes() {
return mGeneralBytes;
}
}