Example usage for android.bluetooth.le AdvertiseSettings ADVERTISE_MODE_BALANCED

List of usage examples for android.bluetooth.le AdvertiseSettings ADVERTISE_MODE_BALANCED

Introduction

In this page you can find the example usage for android.bluetooth.le AdvertiseSettings ADVERTISE_MODE_BALANCED.

Prototype

int ADVERTISE_MODE_BALANCED

To view the source code for android.bluetooth.le AdvertiseSettings ADVERTISE_MODE_BALANCED.

Click Source Link

Document

Perform Bluetooth LE advertising in balanced power mode.

Usage

From source file:Main.java

public static AdvertiseSettings createAdvSettings(boolean connectable, int timeoutMillis) {
    AdvertiseSettings.Builder builder = new AdvertiseSettings.Builder();
    builder.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_BALANCED);
    builder.setConnectable(connectable);
    builder.setTimeout(timeoutMillis);/*w  w w  . j av a2 s . com*/
    builder.setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH);
    return builder.build();
}

From source file:io.v.android.impl.google.discovery.plugins.ble.Driver.java

private synchronized void startAdvertising(BluetoothGattService service) {
    mGattServer.addService(service);/*from w ww  .j  a va 2 s.c om*/
    synchronized (Driver.class) {
        mClassicAdvertiser.addService(service.getUuid(), sClassicDiscoverableDurationInSec);
        sClassicDiscoverableDurationInSec = 0;
    }
    if (mLeAdvertiser != null) {
        final UUID uuid = service.getUuid();
        AdvertiseSettings settings = new AdvertiseSettings.Builder()
                .setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_BALANCED).setConnectable(true).build();
        AdvertiseData data = new AdvertiseData.Builder().addServiceUuid(new ParcelUuid(uuid))
                .setIncludeTxPowerLevel(true).build();
        AdvertiseCallback callback = new AdvertiseCallback() {
            @Override
            public void onStartFailure(int errorCode) {
                Log.e(TAG, "startAdvertising failed: " + uuid + ", errorCode:" + errorCode);
            }
        };
        // TODO(jhahn): The maximum number of simultaneous advertisements is limited by the chipset.
        // Rotate active advertisements periodically if the total number of advertisement exceeds
        // the limit.
        mLeAdvertiser.startAdvertising(settings, data, callback);
        mLeAdvertiseCallbacks.put(uuid, callback);
    }
}

From source file:com.elitise.appv2.Peripheral.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent I = getIntent();//from  w w w .j a va2  s.c  o  m
    name = I.getStringExtra("mBname");
    batteryIdx = I.getIntExtra("idx", -1);
    if (!sNExist()) {
        String mSN = I.getStringExtra("mSN");
        saveSN(mSN);
    }
    isTablet = isTabletDevice();

    //    FirebaseStorage storage = FirebaseStorage.getInstance();
    //    StorageReference storageRef = storage.getReference();
    //    FirebaseOptions opts = FirebaseApp.getInstance().getOptions();
    //    Log.e(TAG, "Bucket = " + opts.getStorageBucket()+"\n URL"+opts.getApplicationId());
    //mDetetor = new GestureDetectorCompat(this, new MyGestureListener());

    setContentView(R.layout.activity_peripherals);
    TextView demoText = (TextView) findViewById(R.id.demoText);
    if (mUser.getBatteriesList().get(batteryIdx).isDemo) {
        demoText.setVisibility(View.VISIBLE);
        demoText.bringToFront();
    } else {
        demoText.setVisibility(View.INVISIBLE);
    }
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    mBluetoothDevices = new HashSet<>();
    mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    mBluetoothAdapter = mBluetoothManager.getAdapter();
    sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
    statusBar = (TextView) findViewById(R.id.statusBar);
    //    boolean enableDiag = sharedPrefs.getBoolean(getString(R.string.pref_mode_key), false);
    //

    //    if (!enableDiag)
    //      mCurrentServiceFragment = new BatteryServiceFragment();
    //    else
    //      mCurrentServiceFragment = new BatteryServiceDiagFragment();
    if (savedInstanceState != null) {
        phoneFragment = (PhoneLayoutFragement) getSupportFragmentManager().findFragmentByTag("PHONE");
        normalFragment = (BatteryServiceFragment) getSupportFragmentManager().findFragmentByTag("NORMAL");
        diagFragment = (BatteryServiceDiagFragment) getSupportFragmentManager().findFragmentByTag("DIAG");
        if (isTablet) {
            mCurrentServiceFragment = normalFragment;
        } else {
            mCurrentServiceFragment = phoneFragment;
        }

    } else {
        diagFragment = new BatteryServiceDiagFragment();
        normalFragment = new BatteryServiceFragment();
        phoneFragment = new PhoneLayoutFragement();
        //mCurrentServiceFragment = normalFragment;

        Bundle bd = new Bundle();
        bd.putString("mBname", name);
        bd.putInt("bIndex", batteryIdx);
        diagFragment.setArguments(bd);
        normalFragment.setArguments(bd);
        phoneFragment.setArguments(bd);
        if (isTablet) {
            mCurrentServiceFragment = normalFragment;
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.normal_fragment_container, normalFragment, "NORMAL")
                    .add(R.id.diag_fragment_container, diagFragment, "DIAG").hide(diagFragment)
                    .show(normalFragment).commit();
        } else {
            mCurrentServiceFragment = phoneFragment;
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.phone_fragment_container, phoneFragment, "PHONE")
                    .add(R.id.normal_fragment_container, normalFragment, "NORMAL")
                    .add(R.id.diag_fragment_container, diagFragment, "DIAG").hide(diagFragment)
                    .hide(normalFragment).show(phoneFragment).commit();
        }
        mCurrentServiceFragment.setArguments(bd);
    }

    if (!mUser.getBatteriesList().get(batteryIdx).isBonded) {
        mBluetoothGattService = mCurrentServiceFragment.getBluetoothGattService();
    } else {
        mBluetoothGattService = mCurrentServiceFragment.getBluetoothGattServiceCustom();
    }

    mAdvSettings = new AdvertiseSettings.Builder().setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_BALANCED)
            .setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH).setConnectable(true).setTimeout(0)
            .build();

    mAdvData = new AdvertiseData.Builder().setIncludeDeviceName(false).setIncludeTxPowerLevel(false)
            .addServiceUuid(
                    mCurrentServiceFragment.getServiceUUID(mUser.getBatteriesList().get(batteryIdx).isBonded))
            .build();

    mScanResponse = new AdvertiseData.Builder().setIncludeDeviceName(true).setIncludeTxPowerLevel(true).build();

    //
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar2);
    setSupportActionBar(toolbar);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(false);

    getSupportActionBar().setDisplayShowTitleEnabled(false);

    logoView = (Logo_pathsView) findViewById(R.id.logoBar);

    Context context = getApplicationContext(); // or activity.getApplicationContext()
    PackageManager packageManager = context.getPackageManager();
    String packageName = context.getPackageName();

    String myVersionName = "1.0.0"; // initialize String

    try {
        myVersionName = packageManager.getPackageInfo(packageName, 0).versionName;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    logoView.setVersion(myVersionName);

    if (!mBluetoothAdapter.isMultipleAdvertisementSupported()) {
        new MaterialDialog.Builder(this).title("BLE ERROR")
                .content("Sorry, this device not support BLE peripheral mode").positiveText("CONFIRM")
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        finish();
                    }
                });
    }

}

From source file:edu.umich.eecs.lab11.camera.CameraFragment.java

private void startAdvertising() {
    if (mBluetoothLeAdvertiser == null)
        return;/*from   w  w w .  jav  a2s  . com*/

    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().addServiceUuid(new ParcelUuid(DeviceProfile.BEACON_UUID))
            .addServiceData(new ParcelUuid(DeviceProfile.BEACON_UUID), DeviceProfile.URI_AD).build();

    mBluetoothLeAdvertiser.startAdvertising(settings, data, mAdvertiseCallback);
}