Search in sources :

Example 1 with Builder

use of android.bluetooth.le.AdvertiseSettings.Builder in project android-beacon-library by AltBeacon.

the class BluetoothMedic method runTransmitterTest.

/**
 * Starts up a beacon transmitter with the intent of seeing if it results in an error condition
 * indicating the bluetooth stack may be in a bad state.
 *
 * If the failure error code matches a pattern known to be associated with a bad bluetooth stack
 * state, then the bluetooth stack is turned off and then back on after a short delay in order
 * to try to recover.
 *
 * @return false if the test indicates a failure indicating a bad state of the bluetooth stack
 */
@SuppressWarnings({ "unused", "WeakerAccess" })
@RequiresApi(21)
public boolean runTransmitterTest(final Context context) {
    initializeWithContext(context);
    this.mTransmitterTestResult = null;
    long testStartTime = System.currentTimeMillis();
    if (mAdapter != null) {
        final BluetoothLeAdvertiser advertiser = getAdvertiserSafely(mAdapter);
        if (advertiser != null) {
            AdvertiseSettings settings = (new Builder()).setAdvertiseMode(0).build();
            AdvertiseData data = (new android.bluetooth.le.AdvertiseData.Builder()).addManufacturerData(0, new byte[] { 0 }).build();
            LogManager.i(TAG, "Starting transmitter test");
            advertiser.startAdvertising(settings, data, new AdvertiseCallback() {

                public void onStartSuccess(AdvertiseSettings settingsInEffect) {
                    super.onStartSuccess(settingsInEffect);
                    LogManager.i(BluetoothMedic.TAG, "Transmitter test succeeded");
                    advertiser.stopAdvertising(this);
                    BluetoothMedic.this.mTransmitterTestResult = true;
                }

                public void onStartFailure(int errorCode) {
                    super.onStartFailure(errorCode);
                    LogManager.d(BluetoothMedic.TAG, "Sending onStartFailure event");
                    BluetoothMedic.this.processMedicAction("onStartFailed", errorCode);
                    if (errorCode == 4) {
                        BluetoothMedic.this.mTransmitterTestResult = false;
                        LogManager.w(BluetoothMedic.TAG, "Transmitter test failed in a way we consider a test failure");
                    } else {
                        BluetoothMedic.this.mTransmitterTestResult = true;
                        LogManager.i(BluetoothMedic.TAG, "Transmitter test failed, but not in a way we consider a test failure");
                    }
                }
            });
        } else {
            LogManager.d(TAG, "Cannot get advertiser");
        }
        while (this.mTransmitterTestResult == null) {
            LogManager.d(TAG, "Waiting for transmitter test to complete...");
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException e) {
            /* do nothing */
            }
            if (System.currentTimeMillis() - testStartTime > 5000L) {
                LogManager.d(TAG, "Timeout running transmitter test");
                break;
            }
        }
    }
    LogManager.d(TAG, "transmitter test complete");
    return this.mTransmitterTestResult != null && this.mTransmitterTestResult;
}
Also used : AdvertiseCallback(android.bluetooth.le.AdvertiseCallback) AdvertiseSettings(android.bluetooth.le.AdvertiseSettings) TaskStackBuilder(android.app.TaskStackBuilder) Builder(android.bluetooth.le.AdvertiseSettings.Builder) AdvertiseData(android.bluetooth.le.AdvertiseData) BluetoothLeAdvertiser(android.bluetooth.le.BluetoothLeAdvertiser) RequiresApi(androidx.annotation.RequiresApi)

Example 2 with Builder

use of android.bluetooth.le.AdvertiseSettings.Builder in project android-beacon-library by AltBeacon.

the class BluetoothMedic method scheduleRegularTests.

@RequiresApi(21)
private void scheduleRegularTests(Context context) {
    initializeWithContext(context);
    ComponentName serviceComponent = new ComponentName(context, BluetoothTestJob.class);
    android.app.job.JobInfo.Builder builder = new android.app.job.JobInfo.Builder(BluetoothTestJob.getJobId(context), serviceComponent);
    builder.setRequiresCharging(false);
    builder.setRequiresDeviceIdle(false);
    // 900 secs is 15 minutes -- the minimum time on Android
    builder.setPeriodic(900000L);
    builder.setPersisted(true);
    PersistableBundle bundle = new PersistableBundle();
    bundle.putInt("test_type", this.mTestType);
    builder.setExtras(bundle);
    JobScheduler jobScheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
    if (jobScheduler != null) {
        jobScheduler.schedule(builder.build());
    }
}
Also used : JobScheduler(android.app.job.JobScheduler) PersistableBundle(android.os.PersistableBundle) TaskStackBuilder(android.app.TaskStackBuilder) Builder(android.bluetooth.le.AdvertiseSettings.Builder) ComponentName(android.content.ComponentName) RequiresApi(androidx.annotation.RequiresApi)

Aggregations

TaskStackBuilder (android.app.TaskStackBuilder)2 Builder (android.bluetooth.le.AdvertiseSettings.Builder)2 RequiresApi (androidx.annotation.RequiresApi)2 JobScheduler (android.app.job.JobScheduler)1 AdvertiseCallback (android.bluetooth.le.AdvertiseCallback)1 AdvertiseData (android.bluetooth.le.AdvertiseData)1 AdvertiseSettings (android.bluetooth.le.AdvertiseSettings)1 BluetoothLeAdvertiser (android.bluetooth.le.BluetoothLeAdvertiser)1 ComponentName (android.content.ComponentName)1 PersistableBundle (android.os.PersistableBundle)1