NFC 启动和enable 过程

/packages/apps/Settings/res/xml/wireless_settings.xml

SwitchPreference
android:key=”toggle_nfc”
NFC开关

WirelessSettings.java (src\com\android\settings):
private static final String KEY_TOGGLE_NFC = “toggle_nfc”;

private NfcEnabler mNfcEnabler;

SwitchPreference nfc = (SwitchPreference) findPreference(KEY_TOGGLE_NFC);
PreferenceScreen androidBeam = (PreferenceScreen) findPreference(KEY_ANDROID_BEAM_SETTINGS);
1、获取NfcEnable的实例
mNfcEnabler = new NfcEnabler(activity, nfc, androidBeam);

2、获取NfcAdapter实例,查看NFC功能是否可能
// Remove NFC if not available
mNfcAdapter = NfcAdapter.getDefaultAdapter(activity);
if (mNfcAdapter == null) {
getPreferenceScreen().removePreference(nfc);
getPreferenceScreen().removePreference(androidBeam);
mNfcEnabler = null;
}

setp1:NFcEnable实例 packages/apps/Settings/src/com/android/settings/nfc/NfcEnabler.java 实现Preference.OnPreferenceChangeListener接口监听NFC Enable,disnable事件

public boolean onPreferenceChange(Preference preference, Object value) {
// Turn NFC on/off

    final boolean desiredState = (Boolean) value;
    mSwitch.setEnabled(false);

    if (desiredState) {
        mNfcAdapter.enable();
    } else {
        mNfcAdapter.disable();
    }

    return false;
}

NfcAdapter实例的获取
源代码的路径:frameworks/base/core/java/android/nfc/NfcAdapter.java
public final class NfcAdapter {
public static NfcAdapter getDefaultAdapter(Context context) {
if (context == null) {
throw new IllegalArgumentException(“context cannot be null”);
}
context = context.getApplicationContext();
if (context == null) {
throw new IllegalArgumentException(
“context not associated with any application (using a mock context?)”);
}
/* use getSystemService() for consistency */
NfcManager manager = (NfcManager) context.getSystemService(Context.NFC_SERVICE);
if (manager == null) {
// NFC not available
return null;
}
return manager.getDefaultAdapter();
}

code路径:frameworks/base/core/java/android/nfc/NfcManager.java
public final class NfcManager{}

    public NfcAdapter getDefaultAdapter() {
    return mAdapter;
}

mAdapter 又由 NfcManager.java 得到
adapter = NfcAdapter.getNfcAdapter(context);
} catch (UnsupportedOperationException e) {
adapter = null;
}
mAdapter = adapter;

找到NfcAdapter.getNfcAdapter(context);
在NfcAdapter.java 里
public static synchronized NfcAdapter getNfcAdapter(Context context) {
if (!sIsInitialized) {
/* is this device meant to have NFC */
if (!hasNfcFeature()) {
Log.v(TAG, “this device does not have NFC support”);
throw new UnsupportedOperationException();
}

        sService = getServiceInterface();//应用INfcAdapter.aidl和NfcService通信
        if (sService == null) {
            Log.e(TAG, "could not retrieve NFC service");
            throw new UnsupportedOperationException();
        }
        try {
            sTagService = sService.getNfcTagInterface();
        } catch (RemoteException e) {
            Log.e(TAG, "could not retrieve NFC Tag service");
            throw new UnsupportedOperationException();
        }

        try {
            sCardEmulationService = sService.getNfcCardEmulationInterface();
        } catch (RemoteException e) {
            Log.e(TAG, "could not retrieve card emulation service");
            throw new UnsupportedOperationException();
        }

        sIsInitialized = true;
    }
    if (context == null) {
        if (sNullContextNfcAdapter == null) {
            sNullContextNfcAdapter = new NfcAdapter(null);
        }
        return sNullContextNfcAdapter;
    }
    NfcAdapter adapter = sNfcAdapters.get(context);

//sNFcAdapter是个HashMap,从中取出之前创建的NfcAdapter实例
if (adapter == null) {
adapter = new NfcAdapter(context);
sNfcAdapters.put(context, adapter);
}
return adapter;

应用INfcAdapter.aidl和NfcService通信 ,INfcAdapter的方法都在NfcService的内部类NfcAdapterService中实现
/* get handle to NFC service interface /
private static INfcAdapter getServiceInterface() {
/* get a handle to NFC service */
IBinder b = ServiceManager.getService(“nfc”);
if (b == null) {
return null;
}
return INfcAdapter.Stub.asInterface(b);
}

/**
* Enable NFC hardware.

public boolean enable() {
try {
return sService.enable();//sService.enable()通过NfcAdapterService来实现的
} catch (RemoteException e) {
attemptDeadServiceRecovery(e);
return false;
}
}

packages/apps/Nfc/src/com/android/nfc/NfcService.java 中的enable方法

final class NfcAdapterService extends INfcAdapter.Stub {
@Override
public boolean enable() throws RemoteException {

把NFC打开状态写到SharedPreferences中保存起来saveNfcOnSetting(true);
…..
//AsyncTash后台处理NFC的打开 开始初始化设备
newEnableDisableTask().execute(TASK_ENABLE);
return true;

EnableDisableTask 在doInBackground中调用enableInternal();来处理NFC turn on。

boolean enableInternal() {
if (mState == NfcAdapter.STATE_ON) {
return true;
}
Log.i(TAG, “Enabling NFC”);
updateState(NfcAdapter.STATE_TURNING_ON);
……

       try {
                if (!mDeviceHost.initialize()) {//调用jni  initialize做init相关的动作
                    Log.w(TAG, "Error enabling NFC");
                    updateState(NfcAdapter.STATE_OFF);
                    return false;

initialize()对应的是在NativeNfcManager.java 中initialize()实现的,NativeNfcManager.initialize()直接调用的是jni方法 doInitialize(),doInitialize()对应的在jni的方 法:

package\apps\Nfc\nxp\jni\com_android_nfc_NativeNfcManager.cpp

com_android_nfc_NfcManager_initialize(JNIEnv *e, jobject o),
static jboolean com_android_nfc_NfcManager_initialize(JNIEnv *e, jobject o)
{
int init_result = JNI_FALSE;
………
/* Perform the initialization */
init_result = nfc_jni_initialize(nat);

/* Initialization function */
static int nfc_jni_initialize(struct nfc_jni_native_data *nat) {

/* Initialize Driver */
if(!driverConfigured)
{
nfc_jni_configure_driver(nat);
}
static int nfc_jni_configure_driver(struct nfc_jni_native_data *nat)

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值