Search in sources :

Example 1 with BluetoothLeAdvertiser

use of android.bluetooth.le.BluetoothLeAdvertiser 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)

Aggregations

TaskStackBuilder (android.app.TaskStackBuilder)1 AdvertiseCallback (android.bluetooth.le.AdvertiseCallback)1 AdvertiseData (android.bluetooth.le.AdvertiseData)1 AdvertiseSettings (android.bluetooth.le.AdvertiseSettings)1 Builder (android.bluetooth.le.AdvertiseSettings.Builder)1 BluetoothLeAdvertiser (android.bluetooth.le.BluetoothLeAdvertiser)1 RequiresApi (androidx.annotation.RequiresApi)1