智能手机和Android Wear(Java)之间没有NFC光束通信

时间:2019-07-04 19:40:40

标签: java android nfc

我正在尝试从智能手机向Android Wear发送一串字符。 我在智能手机上激活了nfc和梁,在手表上激活了nfc。 我将智能手机放在手表上,但手机上没有声音

我尝试在Internet上查找其他来源,但是尽管进行了测试,但仍找不到问题出处。

在手表清单中

    <uses-permission android:name="android.permission.NFC" />
    <uses-feature android:name="android.hardware.nfc" android:required="true"/>


<activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main"
            >
            <intent-filter>
                <action android:name="android.nfc.action.NDEF_DISCOVERED" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="application/com.example.android.beam" />
            </intent-filter>
        </activity>

我认为放置手表激活代码没有用,因为它没有启动,这是问题必须在上游。

在智能手机清单中

    <uses-permission android:name="android.permission.NFC" />
    <uses-feature android:name="android.hardware.nfc" android:required="true"/>


<activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <intent-filter>
                <action android:name="android.nfc.action.NDEF_DISCOVERED" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="application/com.example.android.beam" />
            </intent-filter>
        </activity>

smarphone活动

public class MainActivity extends AppCompatActivity implements NfcAdapter.CreateNdefMessageCallback {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Utils.initializeAll(this, NfcUtils.class); // initialize my NfcUtils library

        NfcUtils.checkAndRequestNfc(this);
        NfcUtils.checkAndRequestBeamNfc(this);

        NfcUtils.setNdefPushMessageCallback(this,this);
    }

    public NdefRecord createMimeRecord(String mimeType, byte[] payload) {
        byte[] mimeBytes = mimeType.getBytes(Charset.forName("US-ASCII"));
        NdefRecord mimeRecord = new NdefRecord(
                NdefRecord.TNF_MIME_MEDIA, mimeBytes, new byte[0], payload);
        return mimeRecord;
    }

    @Override
    protected void onResume() {
        super.onResume();
        NfcUtils.getNfcAdapter().invokeBeam(this);
    }

    @Override
    public void onNewIntent(Intent intent) {
        // onResume gets called after this to handle the intent
        setIntent(intent);
    }

    @Override
    public NdefMessage createNdefMessage(NfcEvent event) {
        String text = "test";
        NdefMessage msg = new NdefMessage(
                new NdefRecord[] { createMimeRecord(
                        "application/com.example.android.beam", text.getBytes())

                });
        return msg;
    }
}

对于那些想要了解我的图书馆NfcUtils内容的人

public class NfcUtils extends Utils {
    private static NfcAdapter mNfcAdapter = null;

    static {
        Utils.register(NfcUtils.class, new String[]{Manifest.permission.NFC});
    }


    static public void initialize(Context context) {
/*
        PackageManager pm = context.getPackageManager();
        // Check whether NFC is available on device
        if (!pm.hasSystemFeature(PackageManager.FEATURE_NFC)) {
            // NFC is not available on the device.
            LogUtils.error("The device does not has NFC hardware.");
            return;
        }
        // Check whether device is running Android 4.1 or higher
        else if (!SystemUtils.hasJellyBean()) {
            LogUtils.error("Android Beam is not supported.");
            return;
        }
*/
        mNfcAdapter = NfcAdapter.getDefaultAdapter(context);
    }

    static public void finalize(Context context) {

    }

    static public List<String> getPermissions(Context context, boolean granted) {
        return Utils.getPermissions(context, granted, NfcUtils.class);
    }

    static public boolean checkPermissionsGranted(Context context) {
        return Utils.checkPermissionsGranted(context, NfcUtils.class);
    }

    static public boolean checkAndRequestPermissions(android.app.Activity activity) {
        return Utils.checkAndRequestPermissions(activity, NfcUtils.class);
    }

    static public boolean checkAndRequestPermissions(AppCompatActivity activity) {
        return Utils.checkAndRequestPermissions(activity, NfcUtils.class);
    }

    static public boolean checkAndRequestPermissions(WearableActivity activity) {
        return Utils.checkAndRequestPermissions(activity, NfcUtils.class);
    }


    static public void setNdefPushMessageCallback(Activity activity, NfcAdapter.CreateNdefMessageCallback callback) {
        mNfcAdapter.setNdefPushMessageCallback(callback,activity);
    }

    static public void enableForegroundDispatch(Context context, Activity activity, String mime)
    {
        Intent nfcIntent = new Intent(context, activity.getClass());
        nfcIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent nfcPendingIntent = PendingIntent.getActivity(context, 0, nfcIntent, 0);

        IntentFilter tagIntentFilter = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
        try {
            tagIntentFilter.addDataType(mime);
            IntentFilter[] intentFiltersArray = new IntentFilter[]{tagIntentFilter};
            mNfcAdapter.enableForegroundDispatch(
                    activity,
                    nfcPendingIntent,
                    intentFiltersArray,
                    null);
        }
        catch (Throwable t) {
            t.printStackTrace();
        }
    }

    static public void disableForegroundDispatch(Activity activity)
    {
        mNfcAdapter.disableForegroundDispatch(activity);
    }

    static public NfcAdapter getNfcAdapter()
    {
        return mNfcAdapter;
    }

    static public void checkAndRequestNfc(Context context)
    {
        if(mNfcAdapter.isEnabled()) return;
        context.startActivity(new Intent(Settings.ACTION_NFC_SETTINGS));
    }


    static public void checkAndRequestBeamNfc(Context context)
    {
       if (mNfcAdapter.isNdefPushEnabled()) return;
        context.startActivity(new Intent(Settings.ACTION_NFCSHARING_SETTINGS));
    }

    static public void setBeamPushUris(Uri[] uris, Activity activity)
    {
        mNfcAdapter.setBeamPushUris(uris, activity);
    }

    static public void setNdefPushMessage(NdefMessage ndefMessage, Activity activity, Activity... activities)
    {
        mNfcAdapter.setNdefPushMessage(ndefMessage, activity,activities);
    }

    static public void setNdefPushMessage(byte[] data, Activity activity, Activity... activities)
    {
        try {
            NdefMessage message = new NdefMessage(data);
            mNfcAdapter.setNdefPushMessage(message, activity,activities);
        } catch (FormatException e) {
            e.printStackTrace();
        }

    }

    static public Parcelable[] getParcelable(@NonNull Intent intent)
    {
        String type = intent.getType();
        String action = intent.getAction();
        if (action.equals(NfcAdapter.ACTION_TAG_DISCOVERED) || action.equals(NfcAdapter.ACTION_NDEF_DISCOVERED)) {
            return  intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
        }
        return new Parcelable[0];
    }

    static public List<NdefMessage> getMessages(@NonNull Parcelable[] parcelables)
    {
        List<NdefMessage> ndefMessageList = new ArrayList<>();

        for (Parcelable p : parcelables) {
            ndefMessageList.add((NdefMessage) p);
        }

        return ndefMessageList;
    }
}

当我接近执行当前应用程序的手表屏幕的智能口(表盘)时,智能手机会发出声音并在智能手机上执行createNdefMessage(),但手表上的活动不会启动..

我不想使用带有常规api的蓝牙在手表和智能手机之间进行通信,因为如果手表因为连接到智能手机而拒绝连接到所需的热点,则必须保持蓝牙关闭

非常感谢您的帮助。

0 个答案:

没有答案
相关问题