150b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly/* 250b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly * Copyright (C) 2010 The Android Open Source Project 350b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly * 450b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly * Licensed under the Apache License, Version 2.0 (the "License"); 550b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly * you may not use this file except in compliance with the License. 650b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly * You may obtain a copy of the License at 750b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly * 850b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly * http://www.apache.org/licenses/LICENSE-2.0 950b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly * 1050b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly * Unless required by applicable law or agreed to in writing, software 1150b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly * distributed under the License is distributed on an "AS IS" BASIS, 1250b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1350b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly * See the License for the specific language governing permissions and 1450b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly * limitations under the License. 1550b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly */ 1650b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly 1750b4d8f643f31b37e9872f562fb869059cf79c8aNick Pellypackage android.nfc; 1850b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly 19d86b8fea43ebb6e5c31691b44d8ceb0d8d3c9072Jeff Sharkeyimport android.annotation.SystemService; 2050b4d8f643f31b37e9872f562fb869059cf79c8aNick Pellyimport android.content.Context; 2150b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly 2250b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly/** 2350b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly * High level manager used to obtain an instance of an {@link NfcAdapter}. 2450b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly * <p> 2550b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly * Use {@link android.content.Context#getSystemService(java.lang.String)} 2650b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly * with {@link Context#NFC_SERVICE} to create an {@link NfcManager}, 2750b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly * then call {@link #getDefaultAdapter} to obtain the {@link NfcAdapter}. 2850b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly * <p> 2950b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly * Alternately, you can just call the static helper 3050b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly * {@link NfcAdapter#getDefaultAdapter(android.content.Context)}. 3150b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly * 323aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <div class="special reference"> 333aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <h3>Developer Guides</h3> 343aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <p>For more information about using NFC, read the 353aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <a href="{@docRoot}guide/topics/nfc/index.html">Near Field Communication</a> developer guide.</p> 363aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * </div> 373aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * 3850b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly * @see NfcAdapter#getDefaultAdapter(android.content.Context) 3950b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly */ 40d86b8fea43ebb6e5c31691b44d8ceb0d8d3c9072Jeff Sharkey@SystemService(Context.NFC_SERVICE) 4150b4d8f643f31b37e9872f562fb869059cf79c8aNick Pellypublic final class NfcManager { 4250b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly private final NfcAdapter mAdapter; 4350b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly 4450b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly /** 4550b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly * @hide 4650b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly */ 4750b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly public NfcManager(Context context) { 4850b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly NfcAdapter adapter; 49bb951c893973691554f49d2e725985125f866b27Jeff Hamilton context = context.getApplicationContext(); 50b04cce0eb5917db436b7db39eb67c064df2e015aNick Pelly if (context == null) { 51b04cce0eb5917db436b7db39eb67c064df2e015aNick Pelly throw new IllegalArgumentException( 52b04cce0eb5917db436b7db39eb67c064df2e015aNick Pelly "context not associated with any application (using a mock context?)"); 53b04cce0eb5917db436b7db39eb67c064df2e015aNick Pelly } 5450b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly try { 55bb951c893973691554f49d2e725985125f866b27Jeff Hamilton adapter = NfcAdapter.getNfcAdapter(context); 5650b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly } catch (UnsupportedOperationException e) { 5750b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly adapter = null; 5850b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly } 5950b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly mAdapter = adapter; 6050b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly } 6150b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly 6250b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly /** 6350b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly * Get the default NFC Adapter for this device. 6450b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly * 6550b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly * @return the default NFC Adapter 6650b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly */ 6750b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly public NfcAdapter getDefaultAdapter() { 6850b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly return mAdapter; 6950b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly } 7050b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly} 71