Search in sources :

Example 1 with AdvertiseSettings

use of android.bluetooth.le.AdvertiseSettings in project physical-web by google.

the class FatBeaconBroadcastService method broadcastUrl.

// ///////////////////////////////
// utilities
// ///////////////////////////////
// Broadcast via bluetooth the stored URL
private void broadcastUrl() {
    byte[] bytes = null;
    try {
        bytes = mDisplayInfo.getBytes("UTF-8");
    } catch (UnsupportedEncodingException e) {
        Log.e(TAG, "Could not encode URL", e);
        return;
    }
    AdvertiseData advertiseData = AdvertiseDataUtils.getFatBeaconAdvertisementData(bytes);
    AdvertiseSettings advertiseSettings = AdvertiseDataUtils.getAdvertiseSettings(true);
    mBluetoothLeAdvertiser.stopAdvertising(mAdvertiseCallback);
    mBluetoothLeAdvertiser.startAdvertising(advertiseSettings, advertiseData, mAdvertiseCallback);
}
Also used : AdvertiseSettings(android.bluetooth.le.AdvertiseSettings) UnsupportedEncodingException(java.io.UnsupportedEncodingException) AdvertiseData(android.bluetooth.le.AdvertiseData)

Example 2 with AdvertiseSettings

use of android.bluetooth.le.AdvertiseSettings in project sample-bluetooth-le-gattserver by androidthings.

the class GattServerActivity method startAdvertising.

/**
 * Begin advertising over Bluetooth that this device is connectable
 * and supports the Current Time Service.
 */
private void startAdvertising() {
    BluetoothAdapter bluetoothAdapter = mBluetoothManager.getAdapter();
    mBluetoothLeAdvertiser = bluetoothAdapter.getBluetoothLeAdvertiser();
    if (mBluetoothLeAdvertiser == null) {
        Log.w(TAG, "Failed to create advertiser");
        return;
    }
    AdvertiseSettings settings = new AdvertiseSettings.Builder().setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_BALANCED).setConnectable(true).setTimeout(0).setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_MEDIUM).build();
    AdvertiseData data = new AdvertiseData.Builder().setIncludeDeviceName(true).setIncludeTxPowerLevel(false).addServiceUuid(new ParcelUuid(TimeProfile.TIME_SERVICE)).build();
    mBluetoothLeAdvertiser.startAdvertising(settings, data, mAdvertiseCallback);
}
Also used : ParcelUuid(android.os.ParcelUuid) AdvertiseSettings(android.bluetooth.le.AdvertiseSettings) AdvertiseData(android.bluetooth.le.AdvertiseData) BluetoothAdapter(android.bluetooth.BluetoothAdapter)

Example 3 with AdvertiseSettings

use of android.bluetooth.le.AdvertiseSettings 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 4 with AdvertiseSettings

use of android.bluetooth.le.AdvertiseSettings in project physical-web by google.

the class PhysicalWebBroadcastService method broadcastUrl.

// ///////////////////////////////
// utilities
// ///////////////////////////////
// Broadcast via bluetooth the stored URL
private void broadcastUrl(byte[] url) {
    final AdvertiseData advertisementData = AdvertiseDataUtils.getAdvertisementData(url);
    final AdvertiseSettings advertiseSettings = AdvertiseDataUtils.getAdvertiseSettings(false);
    mBluetoothLeAdvertiser.stopAdvertising(mAdvertiseCallback);
    mBluetoothLeAdvertiser.startAdvertising(advertiseSettings, advertisementData, mAdvertiseCallback);
}
Also used : AdvertiseSettings(android.bluetooth.le.AdvertiseSettings) AdvertiseData(android.bluetooth.le.AdvertiseData)

Aggregations

AdvertiseData (android.bluetooth.le.AdvertiseData)4 AdvertiseSettings (android.bluetooth.le.AdvertiseSettings)4 TaskStackBuilder (android.app.TaskStackBuilder)1 BluetoothAdapter (android.bluetooth.BluetoothAdapter)1 AdvertiseCallback (android.bluetooth.le.AdvertiseCallback)1 Builder (android.bluetooth.le.AdvertiseSettings.Builder)1 BluetoothLeAdvertiser (android.bluetooth.le.BluetoothLeAdvertiser)1 ParcelUuid (android.os.ParcelUuid)1 RequiresApi (androidx.annotation.RequiresApi)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1