User Tools

Site Tools


android_study_2

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
android_study_2 [2020/03/10 02:27] – [Driver for USB to serial Device] jrsetiandroid_study_2 [2020/03/10 03:06] (current) – [BLE] jrseti
Line 4: Line 4:
  
 ====NFC==== ====NFC====
 +
 +  * Configure the NFC:
 +
 +<code>
 +        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() }};
 +    }
 +</code>
 +
 +  * Enable NFC for this app:
 +
 +<code>
 +NfcAdapter.enableForegroundDispatch(PatchDevicesActivity.this, mPendingIntent, mFilters, mTechLists);
 +</code>
 +
 +  * When device gets near enough you get an intent:
 +<code>
 +        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
 +        }
 +</code>
 +
 +  * Then disable NFC for this app:
 +
 +<code>
 +            NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
 +            nfcAdapter.disableForegroundDispatch(PatchDevicesActivity.this);
 +</code>
  
 ====BLE==== ====BLE====
  
 +<code>
 +        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 scanFilter = new ScanFilter.Builder()
 +                .setServiceUuid(ParcelUuid.fromString(FILTER_UUID))
 +                .build();
 +        filters.add(scanFilter);
 +        
 +        IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
 +        registerReceiver(mBLEStateChangedReceiver, filter);
 +</code>
 +
 +  * Then start scanning and get the scan callbacks:
 +
 +<code>
 +        private ScanCallback mScanCallback = new ScanCallback() {
 +        @Override
 +        public void onScanResult(int callbackType, ScanResult result) {
 +</code>
 ====Service==== ====Service====
 +
 +  * Start the service from the main Activity. This will bind to the service if it exists. Otherwise it will start the service.
 +
 +<code>
 +        mServiceIntent = new Intent(this, PatchService.class);
 +
 +        bindService(mServiceIntent, mConnection, BIND_AUTO_CREATE);
 +        startService(mServiceIntent);
 +</code>
 +
 +  * 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:
 +
 +<code>
 +        <receiver android:name=".Autostart">
 +            <intent-filter>
 +                <action android:name="android.intent.action.BOOT_COMPLETED" />
 +            </intent-filter>
 +        </receiver>
 +</code>
 +
 +  * Autostart is a BroadcastReceiver:
 +
 +<code>
 +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");
 +    }
 +}
 +</code>
 +
  
 ====Driver for USB to serial Device==== ====Driver for USB to serial Device====
Line 61: Line 178:
 </code> </code>
  
 +Then create threads 
  
 +<code>
 +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();
 +</code>
  
  
  
android_study_2.1583807221.txt.gz · Last modified: 2020/03/10 02:27 by jrseti