Java Code Examples for android.bluetooth.le.AdvertiseSettings#Builder

The following examples show how to use android.bluetooth.le.AdvertiseSettings#Builder . 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: JsonDeserializer.java    From mobly-bundled-snippets with Apache License 2.0 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static AdvertiseSettings jsonToBleAdvertiseSettings(JSONObject jsonObject)
        throws JSONException {
    AdvertiseSettings.Builder builder = new AdvertiseSettings.Builder();
    if (jsonObject.has("AdvertiseMode")) {
        int mode = MbsEnums.BLE_ADVERTISE_MODE.getInt(jsonObject.getString("AdvertiseMode"));
        builder.setAdvertiseMode(mode);
    }
    // Timeout in milliseconds.
    if (jsonObject.has("Timeout")) {
        builder.setTimeout(jsonObject.getInt("Timeout"));
    }
    if (jsonObject.has("Connectable")) {
        builder.setConnectable(jsonObject.getBoolean("Connectable"));
    }
    if (jsonObject.has("TxPowerLevel")) {
        int txPowerLevel =
                MbsEnums.BLE_ADVERTISE_TX_POWER.getInt(jsonObject.getString("TxPowerLevel"));
        builder.setTxPowerLevel(txPowerLevel);
    }
    return builder.build();
}
 
Example 2
Source File: BLEServicePeripheral.java    From unity-bluetooth with MIT License 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public void start(String uuidString) {
    mServiceUUID = UUID.fromString(uuidString);
    if (mBtAdvertiser == null) {
        return;
    }

    BluetoothGattService btGattService = new BluetoothGattService(mServiceUUID, BluetoothGattService.SERVICE_TYPE_PRIMARY);
    btGattService.addCharacteristic(mBtGattCharacteristic);
    BluetoothGattServerCallback btGattServerCallback = createGattServerCallback(mServiceUUID, UUID.fromString(CHARACTERISTIC_UUID));
    mBtGattServer = mBtManager.openGattServer(mActivity.getApplicationContext(), btGattServerCallback);
    mBtGattServer.addService(btGattService);

    mDataBuilder = new AdvertiseData.Builder();
    mDataBuilder.setIncludeTxPowerLevel(false);
    mDataBuilder.addServiceUuid(new ParcelUuid(mServiceUUID));

    mSettingsBuilder=new AdvertiseSettings.Builder();
    mSettingsBuilder.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_BALANCED);
    mSettingsBuilder.setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH);

    mBleAdvertiser = mBtAdapter.getBluetoothLeAdvertiser();
    mBleAdvertiser.startAdvertising(mSettingsBuilder.build(), mDataBuilder.build(), mAdvertiseCallback);
}
 
Example 3
Source File: AdvertiserService.java    From connectivity-samples with Apache License 2.0 5 votes vote down vote up
/**
 * Returns an AdvertiseSettings object set to use low power (to help preserve battery life)
 * and disable the built-in timeout since this code uses its own timeout runnable.
 */
private AdvertiseSettings buildAdvertiseSettings() {
    AdvertiseSettings.Builder settingsBuilder = new AdvertiseSettings.Builder();
    settingsBuilder.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_POWER);
    settingsBuilder.setTimeout(0);
    return settingsBuilder.build();
}
 
Example 4
Source File: BleAdvertisingPacket.java    From AsteroidOSSync with GNU General Public License v3.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
/*package*/ AdvertiseSettings getNativeSettings(BleAdvertisingMode mode, BleTransmissionPower power, Interval timeout) {
    AdvertiseSettings.Builder settings = new AdvertiseSettings.Builder();
    settings.setAdvertiseMode(mode.getNativeMode());
    settings.setTxPowerLevel(power.getNativeMode());
    settings.setConnectable(isConnectable());
    settings.setTimeout((int) timeout.millis());
    return settings.build();
}
 
Example 5
Source File: AdvertiserService.java    From android-BluetoothAdvertisements with Apache License 2.0 5 votes vote down vote up
/**
 * Returns an AdvertiseSettings object set to use low power (to help preserve battery life)
 * and disable the built-in timeout since this code uses its own timeout runnable.
 */
private AdvertiseSettings buildAdvertiseSettings() {
    AdvertiseSettings.Builder settingsBuilder = new AdvertiseSettings.Builder();
    settingsBuilder.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_POWER);
    settingsBuilder.setTimeout(0);
    return settingsBuilder.build();
}
 
Example 6
Source File: BleAdvertisingPacket.java    From SweetBlue with GNU General Public License v3.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
/*package*/ AdvertiseSettings getNativeSettings(BleAdvertisingMode mode, BleTransmissionPower power, Interval timeout) {
    AdvertiseSettings.Builder settings = new AdvertiseSettings.Builder();
    settings.setAdvertiseMode(mode.getNativeMode());
    settings.setTxPowerLevel(power.getNativeMode());
    settings.setConnectable(isConnectable());
    settings.setTimeout((int) timeout.millis());
    return settings.build();
}
 
Example 7
Source File: AdvertiseDataUtils.java    From physical-web with Apache License 2.0 5 votes vote down vote up
@TargetApi(21)
public static AdvertiseSettings getAdvertiseSettings(boolean connectable) {
    AdvertiseSettings.Builder builder = new AdvertiseSettings.Builder();
    builder.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_POWER);
    builder.setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_MEDIUM);
    builder.setConnectable(connectable);

    return builder.build();
}
 
Example 8
Source File: BeaconTransmitter.java    From android-beacon-library with Apache License 2.0 4 votes vote down vote up
/**
 * Starts this beacon advertising
 */
public void startAdvertising() {
    if (mBeacon == null) {
        throw new NullPointerException("Beacon cannot be null.  Set beacon before starting advertising");
    }
    int manufacturerCode = mBeacon.getManufacturer();
    int serviceUuid = -1;
    if (mBeaconParser.getServiceUuid() != null) {
        serviceUuid = mBeaconParser.getServiceUuid().intValue();
    }

    if (mBeaconParser == null) {
        throw new NullPointerException("You must supply a BeaconParser instance to BeaconTransmitter.");
    }

    byte[] advertisingBytes = mBeaconParser.getBeaconAdvertisementData(mBeacon);
    String byteString = "";
    for (int i= 0; i < advertisingBytes.length; i++) {
        byteString += String.format("%02X", advertisingBytes[i]);
        byteString += " ";
    }
    LogManager.d(TAG, "Starting advertising with ID1: %s ID2: %s ID3: %s and data: %s of size "
                    + "%s", mBeacon.getId1(),
                    mBeacon.getIdentifiers().size() > 1 ? mBeacon.getId2() : "",
                    mBeacon.getIdentifiers().size() > 2 ? mBeacon.getId3() : "", byteString,
            advertisingBytes.length);

    try{
        AdvertiseData.Builder dataBuilder = new AdvertiseData.Builder();
        if (serviceUuid > 0) {
            byte[] serviceUuidBytes = new byte[] {
                    (byte) (serviceUuid & 0xff),
                    (byte) ((serviceUuid >> 8) & 0xff)};
            ParcelUuid parcelUuid = parseUuidFrom(serviceUuidBytes);
            dataBuilder.addServiceData(parcelUuid, advertisingBytes);
            dataBuilder.addServiceUuid(parcelUuid);
            dataBuilder.setIncludeTxPowerLevel(false);
            dataBuilder.setIncludeDeviceName(false);

        } else {
            dataBuilder.addManufacturerData(manufacturerCode, advertisingBytes);
        }

        AdvertiseSettings.Builder settingsBuilder = new AdvertiseSettings.Builder();

        settingsBuilder.setAdvertiseMode(mAdvertiseMode);
        settingsBuilder.setTxPowerLevel(mAdvertiseTxPowerLevel);
        settingsBuilder.setConnectable(mConnectable);

        mBluetoothLeAdvertiser.startAdvertising(settingsBuilder.build(), dataBuilder.build(), getAdvertiseCallback());
        LogManager.d(TAG, "Started advertisement with callback: %s", getAdvertiseCallback());

    } catch (Exception e){
        LogManager.e(e, TAG, "Cannot start advertising due to exception");
    }
}