android.bluetooth.le.BluetoothLeAdvertiser Java Examples

The following examples show how to use android.bluetooth.le.BluetoothLeAdvertiser. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DalvikBleService.java    From attach with GNU General Public License v3.0 5 votes vote down vote up
private void stopBroadcast() {
    Log.v(TAG, "Stop broadcasting");
    if (callback != null) {
        BluetoothLeAdvertiser advertiser = BluetoothAdapter.getDefaultAdapter().getBluetoothLeAdvertiser();
        advertiser.stopAdvertising(callback);
        callback = null;
    }
}
 
Example #2
Source File: L_Util.java    From AsteroidOSSync with GNU General Public License v3.0 5 votes vote down vote up
public static boolean startAdvertising(BluetoothAdapter adapter, AdvertiseSettings settings, AdvertiseData adData, AdvertisingCallback callback)
{
    final BluetoothLeAdvertiser adv = adapter.getBluetoothLeAdvertiser();
    if (adv == null)
        return false;

    m_userAdvCallback = callback;
    adv.startAdvertising(settings, adData, m_nativeAdvertiseCallback);
    return true;
}
 
Example #3
Source File: L_Util.java    From AsteroidOSSync with GNU General Public License v3.0 5 votes vote down vote up
public static void stopAdvertising(BluetoothAdapter adapter)
{
    if (adapter != null)
    {
        final BluetoothLeAdvertiser adv = adapter.getBluetoothLeAdvertiser();
        if (adv != null)
        {
            adv.stopAdvertising(m_nativeAdvertiseCallback);
        }
    }
}
 
Example #4
Source File: Advertiser.java    From beacons-android with Apache License 2.0 5 votes vote down vote up
/**
 * Attempt to start BLE advertising.
 * @param bleAdvertiser   BLE advertiser
 * @return  True if no exception occurred while trying to start advertising.
 */
boolean start(BluetoothLeAdvertiser bleAdvertiser) {
    try {
        bleAdvertiser.startAdvertising(getAdvertiseSettings(), getAdvertiseData(),
                getAdvertiseScanResponse(), this);
    } catch (IllegalStateException e) {
        // tried to start advertising after Bluetooth was turned off
        // let upper level notice that BT is off instead of reporting an error
        if (BuildConfig.DEBUG) {
            Log.e(TAG, "start", e);
        }
        return false;
    }

    return true;
}
 
Example #5
Source File: Advertiser.java    From beacons-android with Apache License 2.0 5 votes vote down vote up
/**
 * Mark the advertiser as stopped and attempt to actually stop BLE advertisements.
 * @param bleAdvertiser    BLE advertiser, or null
 * @return  True if there was no error while trying to stop the Bluetooth advertiser.
 */
boolean stop(BluetoothLeAdvertiser bleAdvertiser) {
    updateEstimatedPDUCount();
    mStatus = STATUS_STOPPED;

    if (null != bleAdvertiser) {
        bleAdvertiser.stopAdvertising(this);
    }

    return true;
}
 
Example #6
Source File: L_Util.java    From SweetBlue with GNU General Public License v3.0 5 votes vote down vote up
public static boolean startAdvertising(BluetoothAdapter adapter, AdvertiseSettings settings, AdvertiseData adData, AdvertisingCallback callback)
{
    final BluetoothLeAdvertiser adv = adapter.getBluetoothLeAdvertiser();
    if (adv == null)
        return false;

    m_userAdvCallback = callback;
    adv.startAdvertising(settings, adData, m_nativeAdvertiseCallback);
    return true;
}
 
Example #7
Source File: L_Util.java    From SweetBlue with GNU General Public License v3.0 5 votes vote down vote up
public static void stopAdvertising(BluetoothAdapter adapter)
{
    if (adapter != null)
    {
        final BluetoothLeAdvertiser adv = adapter.getBluetoothLeAdvertiser();
        if (adv != null)
        {
            adv.stopAdvertising(m_nativeAdvertiseCallback);
        }
    }
}
 
Example #8
Source File: BluetoothMedic.java    From android-beacon-library with Apache License 2.0 5 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private BluetoothLeAdvertiser getAdvertiserSafely(BluetoothAdapter adapter) {
    try {
        // This can sometimes throw a NullPointerException as reported here:
        // https://github.com/AltBeacon/android-beacon-library/issues/672
        return adapter.getBluetoothLeAdvertiser();
    }
    catch (Exception e) {
        LogManager.w(TAG, "Cannot get bluetoothLeAdvertiser", e);
    }
    return null;
}
 
Example #9
Source File: MainActivity.java    From bluetooth with Apache License 2.0 5 votes vote down vote up
void advertise() {
    BluetoothLeAdvertiser advertiser = BluetoothAdapter.getDefaultAdapter().getBluetoothLeAdvertiser();
    //define the power settings  could use ADVERTISE_MODE_LOW_POWER, ADVERTISE_MODE_BALANCED too.
    AdvertiseSettings settings = new AdvertiseSettings.Builder()
        .setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY)
        .setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH)
        .setConnectable(false)
        .build();

    ParcelUuid pUuid = new ParcelUuid(UUID.fromString(getString(R.string.ble_uuid)));

    AdvertiseData data = new AdvertiseData.Builder()
        .setIncludeDeviceName(false)
        .addServiceUuid(pUuid)
        .addServiceData(pUuid, "LE Demo".getBytes(Charset.forName("UTF-8")))
        .build();
    AdvertiseCallback advertisingCallback = new AdvertiseCallback() {
        @Override
        public void onStartSuccess(AdvertiseSettings settingsInEffect) {
            super.onStartSuccess(settingsInEffect);
        }

        @Override
        public void onStartFailure(int errorCode) {
            Log.e("BLE", "Advertising onStartFailure: " + errorCode);
            super.onStartFailure(errorCode);
        }
    };

    advertiser.startAdvertising(settings, data, advertisingCallback);
    //advertiser.stopAdvertising(advertisingCallback);
}
 
Example #10
Source File: MainActivity.java    From AndroidDemoProjects with Apache License 2.0 5 votes vote down vote up
private void advertise() {
        BluetoothLeAdvertiser advertiser = BluetoothAdapter.getDefaultAdapter().getBluetoothLeAdvertiser();

        AdvertiseSettings settings = new AdvertiseSettings.Builder()
                .setAdvertiseMode( AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY )
                .setTxPowerLevel( AdvertiseSettings.ADVERTISE_TX_POWER_HIGH )
                .setConnectable(false)
                .build();

        ParcelUuid pUuid = new ParcelUuid( UUID.fromString( getString( R.string.ble_uuid ) ) );

        AdvertiseData data = new AdvertiseData.Builder()
                .setIncludeDeviceName( true )
                .addServiceUuid( pUuid )
                .addServiceData( pUuid, "Data".getBytes(Charset.forName("UTF-8") ) )
                .build();

        AdvertiseCallback advertisingCallback = new AdvertiseCallback() {
            @Override
            public void onStartSuccess(AdvertiseSettings settingsInEffect) {
                super.onStartSuccess(settingsInEffect);
            }

            @Override
            public void onStartFailure(int errorCode) {
                Log.e( "BLE", "Advertising onStartFailure: " + errorCode );
                super.onStartFailure(errorCode);
            }
        };

        advertiser.startAdvertising( settings, data, advertisingCallback );
}
 
Example #11
Source File: DalvikBleService.java    From attach with GNU General Public License v3.0 4 votes vote down vote up
private void startBroadcast(String uuid, int major, int minor, String id) {
    Log.v(TAG, "Start broadcasting for uuid = "+uuid+", major = "+major+", minor = "+minor+", id = "+id);
    BluetoothLeAdvertiser advertiser = BluetoothAdapter.getDefaultAdapter().getBluetoothLeAdvertiser();
    AdvertiseSettings parameters = new AdvertiseSettings.Builder()
            .setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY)
            .setConnectable(false)
            .setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH)
            .build();

    byte[] payload = getPayload(uuid, major, minor);
    AdvertiseData data = new AdvertiseData.Builder()
            .addManufacturerData(0x004C, payload)
            .build();
    callback = new AdvertiseCallback() {
        @Override
        public void onStartSuccess(AdvertiseSettings settingsInEffect) {
            super.onStartSuccess(settingsInEffect);
            Log.v(TAG, "onStartSuccess");
        }

        @Override
        public void onStartFailure(int errorCode) {
            Log.e(TAG, "Advertising onStartFailure: " + errorCode);
            super.onStartFailure(errorCode);
        }
    };

    Log.v(TAG, "Advertise with payload = " + Arrays.toString(payload));
    advertiser.startAdvertising(parameters, data, callback);
}
 
Example #12
Source File: L_Util.java    From AsteroidOSSync with GNU General Public License v3.0 4 votes vote down vote up
public static BluetoothLeAdvertiser getBluetoothLeAdvertiser(BluetoothAdapter adapter)
{
    return adapter.getBluetoothLeAdvertiser();
}
 
Example #13
Source File: L_Util.java    From SweetBlue with GNU General Public License v3.0 4 votes vote down vote up
public static BluetoothLeAdvertiser getBluetoothLeAdvertiser(BluetoothAdapter adapter)
{
    return adapter.getBluetoothLeAdvertiser();
}
 
Example #14
Source File: BluetoothMedic.java    From android-beacon-library with Apache License 2.0 4 votes vote down vote up
/**
 * 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);
                    Intent intent = new Intent("onStartFailed");
                    intent.putExtra("errorCode", errorCode);
                    LogManager.d(BluetoothMedic.TAG, "Sending onStartFailure broadcast with "
                            + BluetoothMedic.this.mLocalBroadcastManager);
                    if (BluetoothMedic.this.mLocalBroadcastManager != null) {
                        BluetoothMedic.this.mLocalBroadcastManager.sendBroadcast(intent);
                    }
                    if(errorCode == 4) {
                        BluetoothMedic.this.mTransmitterTestResult = false;
                        LogManager.w(BluetoothMedic.TAG,
                                "Transmitter test failed in a way we consider a test failure");
                        BluetoothMedic.this.sendNotification(context, "transmitter failed",
                                "bluetooth not ok");
                    } 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;
}