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:53] – [NFC] jrsetiandroid_study_2 [2020/03/10 03:06] (current) – [BLE] jrseti
Line 54: Line 54:
 ====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====
android_study_2.1583808825.txt.gz · Last modified: 2020/03/10 02:53 by jrseti