User Tools

Site Tools


android_study_2

This is an old revision of the document!


Android Study 2

Things I have done in my apps.

NFC

  • Configure the NFC:
        private void configureNfc(){
        mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
        mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
                getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

        Log.v(LOG_TAG, "NFC enabled = " + mNfcAdapter.isEnabled());
        IntentFilter intnfcv = new IntentFilter(
                NfcAdapter.ACTION_TECH_DISCOVERED);
        mFilters = new IntentFilter[] {
                intnfcv};
        mTechLists = new String[][] { new String[] {
                NfcV.class.getName() }};
    }
  • Enable NFC for this app:
NfcAdapter.enableForegroundDispatch(PatchDevicesActivity.this, mPendingIntent, mFilters, mTechLists);
  • When device gets near enough you get an intent:
        protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        final String action = intent.getAction();
        Log.v(LOG_TAG, "onNewIntent:" + action);

        setIntent(intent);
        if (mNfcEnabled == true && 
            NfcAdapter.ACTION_TECH_DISCOVERED.equals(getIntent().getAction())) {
              Tag tag = getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG);
              tranceive() to send and get response
        }

BLE

Service

Driver for USB to serial Device

FTSLink by Redpark

In the app the manifest enables USB attach and detach intents to be delivered.

* Asked for permission:
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);			
this.parent.getParentContext().getApplicationContext().registerReceiver(mUsbReceiver, filter);
  • ACTION_USB_ACCESSORY_ATTACHED re-starts the activity
  • ACTION_USB_ACCESSORY_DETACHED is received in a BroadcastReceiver:
private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {

		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			Log.v(debugTag, "ACTION: " + action);
			if (action.equals(UsbManager.ACTION_USB_ACCESSORY_DETACHED)) {
				Log.v(debugTag, "Closing Accessory");
				closeAccessory();
			}

		}

	};
  • Upon attach, connect to the Device
UsbAccessory[] accessories = mUSBManager.getAccessoryList();

            // There is only one USB, this must be the first one
            UsbAccessory accessory = (accessories == null ? null : accessories[0]);

            if (accessory != null) {
                Log.v(FTSLinkServer.debugTag, "Accessory found, try to connect....");
                RPUsbConnection connection = new RPUsbConnection(mUSBManager, accessory);
                ParcelFileDescriptor mFileDescriptor = usbManager.openAccessory(accessory);
                FileDescriptor fd = mFileDescriptor.getFileDescriptor();
                mInputStream = new FileInputStream(fd);
                mOutputStream = new FileOutputStream(fd);
                
                ... Create threads 
            }

Then create threads

private static class ReadThread implements Runnable {
  @Override
        public void run() {
        }
}

parentDevice.readThread = new ReadThread(parentDevice);
Thread rt = new Thread(parentDevice.readThread);
rt.setPriority(Thread.MAX_PRIORITY);
rt.start();
android_study_2.1583808756.txt.gz · Last modified: 2020/03/10 02:52 by jrseti