android_study_2
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| android_study_2 [2020/03/10 02:27] – [Driver for USB to serial Device] jrseti | android_study_2 [2020/03/10 03:06] (current) – [BLE] jrseti | ||
|---|---|---|---|
| Line 4: | Line 4: | ||
| ====NFC==== | ====NFC==== | ||
| + | |||
| + | * Configure the NFC: | ||
| + | |||
| + | < | ||
| + | private void configureNfc(){ | ||
| + | mNfcAdapter = NfcAdapter.getDefaultAdapter(this); | ||
| + | mPendingIntent = PendingIntent.getActivity(this, | ||
| + | getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), | ||
| + | |||
| + | Log.v(LOG_TAG, | ||
| + | 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, | ||
| + | </ | ||
| + | |||
| + | * 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, | ||
| + | |||
| + | 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==== | ====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, | ||
| + | </ | ||
| + | |||
| + | * Then start scanning and get the scan callbacks: | ||
| + | |||
| + | < | ||
| + | private ScanCallback mScanCallback = new ScanCallback() { | ||
| + | @Override | ||
| + | public void onScanResult(int callbackType, | ||
| + | </ | ||
| ====Service==== | ====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, | ||
| + | |||
| + | bindService(mServiceIntent, | ||
| + | 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: | ||
| + | |||
| + | < | ||
| + | < | ||
| + | < | ||
| + | <action android: | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | * Autostart is a BroadcastReceiver: | ||
| + | |||
| + | < | ||
| + | public class Autostart extends BroadcastReceiver | ||
| + | { | ||
| + | private static final String LOG_TAG = " | ||
| + | |||
| + | public void onReceive(Context context, Intent arg1) | ||
| + | { | ||
| + | Intent intent = new Intent(context, | ||
| + | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
| + | context.startForegroundService(intent); | ||
| + | } else { | ||
| + | context.startService(intent); | ||
| + | } | ||
| + | Log.v(LOG_TAG, | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| ====Driver for USB to serial Device==== | ====Driver for USB to serial Device==== | ||
| Line 61: | Line 178: | ||
| </ | </ | ||
| + | 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.1583807221.txt.gz · Last modified: 2020/03/10 02:27 by jrseti