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)

Ubuntu安装 NFC(近场通信)驱动和支持通常涉及到几个步骤: 1. **更新系统**: 首先,确保您的Ubuntu系统是最新的。打开终端并输入: ``` sudo apt update && sudo apt upgrade ``` 2. **安装必要的包**: 对于支持nfc的硬件,您需要安装`nfc-tools`包,它包含了基本的nfc工具。运行: ``` sudo apt install nfc-tools ``` 3. **检查硬件兼容性**: 确认您的计算机是否配备了支持Ubuntu的NFC模块。可以通过设备管理器查看,或者使用命令`lsusb`查找带有“NFC”标识的USB设备。 4. **安装Linux内核模块**: 如果您的设备需要特定内核模块,可能需要安装它们。对于一些较新的nfc芯片,如Broadcom BCM2835,可以尝试安装`bcm2835-nfc`: ``` sudo apt install bcm2835-nfc ``` 或者查询其他对应芯片的软件包。 5. **启用nfc服务**: 启动nfc服务,并设置开机自动启动: ``` sudo systemctl enable nfc-manager sudo systemctl start nfc-manager ``` 6. **测试nfc功能**: 使用`nfc-list`命令确认nfc设备已识别,并尝试读取或写入标签,如: ``` nfc-list nfc-data-read nfc-data-write ``` 7. **配置reader/writer应用**: 可能还需要安装其他应用程序来方便地读取和写入nfc标签,例如Google Keep、Nextcloud Notes等。 **注意事项**: - 安装过程中可能遇到依赖缺失或其他硬件问题,这时可能需要查阅具体的硬件文档或搜索解决方案。 - 尽量使用适用于您Ubuntu版本的稳定版软件包,避免使用不稳定或过期的版本。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值