======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 } * Then disable NFC for this app: NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this); nfcAdapter.disableForegroundDispatch(PatchDevicesActivity.this); ====BLE==== final BluetoothManager bluetoothManager = (BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE); mBluetoothAdapter = bluetoothManager.getAdapter(); mLEScanner = mBluetoothAdapter.getBluetoothLeScanner(); settings = new ScanSettings.Builder() .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY) .build(); filters = new ArrayList(); ScanFilter scanFilter = new ScanFilter.Builder() .setServiceUuid(ParcelUuid.fromString(FILTER_UUID)) .build(); filters.add(scanFilter); IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED); registerReceiver(mBLEStateChangedReceiver, filter); * Then start scanning and get the scan callbacks: private ScanCallback mScanCallback = new ScanCallback() { @Override public void onScanResult(int callbackType, ScanResult result) { ====Service==== * Start the service from the main Activity. This will bind to the service if it exists. Otherwise it will start the service. mServiceIntent = new Intent(this, PatchService.class); bindService(mServiceIntent, mConnection, BIND_AUTO_CREATE); startService(mServiceIntent); * In the Service class instance call startForeground() with a notification to display to the user. * This will keep the service active in the foreground and never die. * To start the service when the device is powered on do this in the manifest: * Autostart is a BroadcastReceiver: public class Autostart extends BroadcastReceiver { private static final String LOG_TAG = "PATCH_SERVICE_AUTOSTART"; public void onReceive(Context context, Intent arg1) { Intent intent = new Intent(context, PatchService.class); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { context.startForegroundService(intent); } else { context.startService(intent); } Log.v(LOG_TAG, "started"); } } ====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();