NdefFormatable.java revision 4e21e1d21a877cce4db5ec8c5786604cc10f2d7e
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 17package android.nfc.tech; 18 19import android.nfc.ErrorCodes; 20import android.nfc.FormatException; 21import android.nfc.INfcTag; 22import android.nfc.NdefMessage; 23import android.nfc.NfcAdapter; 24import android.nfc.Tag; 25import android.os.RemoteException; 26import android.util.Log; 27 28import java.io.IOException; 29 30/** 31 * An interface to a {@link Tag} allowing to format the tag as NDEF. 32 * 33 * <p>You can acquire this kind of connection with {@link #get}. 34 * 35 * <p class="note"><strong>Note:</strong> 36 * Use of this class requires the {@link android.Manifest.permission#NFC} 37 * permission. 38 */ 39public final class NdefFormatable extends BasicTagTechnology { 40 private static final String TAG = "NFC"; 41 42 /** 43 * Returns an instance of this tech for the given tag. If the tag doesn't support 44 * this tech type null is returned. 45 * 46 * @param tag The tag to get the tech from 47 */ 48 public static NdefFormatable get(Tag tag) { 49 if (!tag.hasTech(TagTechnology.NDEF_FORMATABLE)) return null; 50 try { 51 return new NdefFormatable(tag); 52 } catch (RemoteException e) { 53 return null; 54 } 55 } 56 57 /** 58 * Internal constructor, to be used by NfcAdapter 59 * @hide 60 */ 61 public NdefFormatable(Tag tag) throws RemoteException { 62 super(tag, TagTechnology.NDEF_FORMATABLE); 63 } 64 65 /** 66 * Formats a tag as NDEF, if possible. You may supply a first 67 * NdefMessage to be written on the tag. 68 */ 69 public void format(NdefMessage firstMessage) throws IOException, FormatException { 70 checkConnected(); 71 72 try { 73 int serviceHandle = mTag.getServiceHandle(); 74 INfcTag tagService = mTag.getTagService(); 75 int errorCode = tagService.formatNdef(serviceHandle, MifareClassic.KEY_DEFAULT); 76 switch (errorCode) { 77 case ErrorCodes.SUCCESS: 78 break; 79 case ErrorCodes.ERROR_IO: 80 throw new IOException(); 81 case ErrorCodes.ERROR_INVALID_PARAM: 82 throw new FormatException(); 83 default: 84 // Should not happen 85 throw new IOException(); 86 } 87 // Now check and see if the format worked 88 if (tagService.isNdef(serviceHandle)) { 89 errorCode = tagService.ndefWrite(serviceHandle, firstMessage); 90 switch (errorCode) { 91 case ErrorCodes.SUCCESS: 92 break; 93 case ErrorCodes.ERROR_IO: 94 throw new IOException(); 95 case ErrorCodes.ERROR_INVALID_PARAM: 96 throw new FormatException(); 97 default: 98 // Should not happen 99 throw new IOException(); 100 } 101 } else { 102 throw new IOException(); 103 } 104 } catch (RemoteException e) { 105 Log.e(TAG, "NFC service dead", e); 106 } 107 } 108} 109