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:52] – [NFC] jrseti | android_study_2 [2020/03/10 03:06] (current) – [BLE] jrseti | ||
|---|---|---|---|
| Line 43: | Line 43: | ||
| tranceive() to send and get response | 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==== | ||
android_study_2.1583808756.txt.gz · Last modified: 2020/03/10 02:52 by jrseti