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

The following examples show how to use android.bluetooth.le.AdvertiseData#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: AdvertiserService.java    From connectivity-samples with Apache License 2.0 6 votes vote down vote up
/**
 * Returns an AdvertiseData object which includes the Service UUID and Device Name.
 */
private AdvertiseData buildAdvertiseData() {

    /**
     * Note: There is a strict limit of 31 Bytes on packets sent over BLE Advertisements.
     *  This includes everything put into AdvertiseData including UUIDs, device info, &
     *  arbitrary service or manufacturer data.
     *  Attempting to send packets over this limit will result in a failure with error code
     *  AdvertiseCallback.ADVERTISE_FAILED_DATA_TOO_LARGE. Catch this error in the
     *  onStartFailure() method of an AdvertiseCallback implementation.
     */

    AdvertiseData.Builder dataBuilder = new AdvertiseData.Builder();
    dataBuilder.addServiceUuid(Constants.Service_UUID);
    dataBuilder.setIncludeDeviceName(true);

    /* For example - this will cause advertising to fail (exceeds size limit) */
    //String failureData = "asdghkajsghalkxcjhfa;sghtalksjcfhalskfjhasldkjfhdskf";
    //dataBuilder.addServiceData(Constants.Service_UUID, failureData.getBytes());

    return dataBuilder.build();
}
 
Example 2
Source File: BleAdvertisingPacket.java    From AsteroidOSSync with GNU General Public License v3.0 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
/*package*/ AdvertiseData getNativeData() {
    AdvertiseData.Builder data = new AdvertiseData.Builder();
    for (UUID id : serviceUuids)
    {
        data.addServiceUuid(new ParcelUuid(id));
    }
    if (m_manufacturerId != 0 && m_manData != null)
    {
        data.addManufacturerData(m_manufacturerId, m_manData);
    }
    if (serviceData != null && serviceData.size() > 0)
    {
        for (UUID dataUuid : serviceData.keySet())
        {
            data.addServiceData(new ParcelUuid(dataUuid), serviceData.get(dataUuid));
        }
    }
    data.setIncludeDeviceName(includeDeviceName());
    data.setIncludeTxPowerLevel(includeTxPowerLevel());
    return data.build();
}
 
Example 3
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 4
Source File: AdvertiserService.java    From android-BluetoothAdvertisements with Apache License 2.0 6 votes vote down vote up
/**
 * Returns an AdvertiseData object which includes the Service UUID and Device Name.
 */
private AdvertiseData buildAdvertiseData() {

    /**
     * Note: There is a strict limit of 31 Bytes on packets sent over BLE Advertisements.
     *  This includes everything put into AdvertiseData including UUIDs, device info, &
     *  arbitrary service or manufacturer data.
     *  Attempting to send packets over this limit will result in a failure with error code
     *  AdvertiseCallback.ADVERTISE_FAILED_DATA_TOO_LARGE. Catch this error in the
     *  onStartFailure() method of an AdvertiseCallback implementation.
     */

    AdvertiseData.Builder dataBuilder = new AdvertiseData.Builder();
    dataBuilder.addServiceUuid(Constants.Service_UUID);
    dataBuilder.setIncludeDeviceName(true);

    /* For example - this will cause advertising to fail (exceeds size limit) */
    //String failureData = "asdghkajsghalkxcjhfa;sghtalksjcfhalskfjhasldkjfhdskf";
    //dataBuilder.addServiceData(Constants.Service_UUID, failureData.getBytes());

    return dataBuilder.build();
}
 
Example 5
Source File: BleAdvertisingPacket.java    From SweetBlue with GNU General Public License v3.0 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
/*package*/ AdvertiseData getNativeData() {
    AdvertiseData.Builder data = new AdvertiseData.Builder();
    for (UUID id : serviceUuids)
    {
        data.addServiceUuid(new ParcelUuid(id));
    }
    if (m_manufacturerId != 0 && m_manData != null)
    {
        data.addManufacturerData(m_manufacturerId, m_manData);
    }
    if (serviceData != null && serviceData.size() > 0)
    {
        for (UUID dataUuid : serviceData.keySet())
        {
            data.addServiceData(new ParcelUuid(dataUuid), serviceData.get(dataUuid));
        }
    }
    data.setIncludeDeviceName(includeDeviceName());
    data.setIncludeTxPowerLevel(includeTxPowerLevel());
    return data.build();
}
 
Example 6
Source File: AdvertiseDataUtils.java    From physical-web with Apache License 2.0 6 votes vote down vote up
@TargetApi(21)
public static AdvertiseData getAdvertisementData(byte[] urlData) {
  AdvertiseData.Builder builder = new AdvertiseData.Builder();
  builder.setIncludeTxPowerLevel(false); // reserve advertising space for URI

  // Manually build the advertising info
  // See https://github.com/google/eddystone/tree/master/eddystone-url
  if (urlData == null || urlData.length == 0) {
    return null;
  }

  byte[] beaconData = new byte[urlData.length + 2];
  System.arraycopy(urlData, 0, beaconData, 2, urlData.length);
  beaconData[0] = URL_FRAME_TYPE; // frame type: url
  beaconData[1] = (byte) 0xBA; // calibrated tx power at 0 m

  builder.addServiceData(EDDYSTONE_BEACON_UUID, beaconData);

  // Adding 0xFEAA to the "Service Complete List UUID 16" (0x3) for iOS compatibility
  builder.addServiceUuid(EDDYSTONE_BEACON_UUID);

  return builder.build();
}
 
Example 7
Source File: JsonDeserializer.java    From mobly-bundled-snippets with Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static AdvertiseData jsonToBleAdvertiseData(JSONObject jsonObject) throws JSONException {
    AdvertiseData.Builder builder = new AdvertiseData.Builder();
    if (jsonObject.has("IncludeDeviceName")) {
        builder.setIncludeDeviceName(jsonObject.getBoolean("IncludeDeviceName"));
    }
    if (jsonObject.has("IncludeTxPowerLevel")) {
        builder.setIncludeTxPowerLevel(jsonObject.getBoolean("IncludeTxPowerLevel"));
    }
    if (jsonObject.has("ServiceData")) {
        JSONArray serviceData = jsonObject.getJSONArray("ServiceData");
        for (int i = 0; i < serviceData.length(); i++) {
            JSONObject dataSet = serviceData.getJSONObject(i);
            ParcelUuid parcelUuid = ParcelUuid.fromString(dataSet.getString("UUID"));
            builder.addServiceUuid(parcelUuid);
            if (dataSet.has("Data")) {
                byte[] data = Base64.decode(dataSet.getString("Data"), Base64.DEFAULT);
                builder.addServiceData(parcelUuid, data);
            }
        }
    }
    if (jsonObject.has("ManufacturerData")) {
        JSONObject manufacturerData = jsonObject.getJSONObject("ManufacturerData");
        int manufacturerId = manufacturerData.getInt("ManufacturerId");
        byte[] manufacturerSpecificData =
                Base64.decode(jsonObject.getString("ManufacturerSpecificData"), Base64.DEFAULT);
        builder.addManufacturerData(manufacturerId, manufacturerSpecificData);
    }
    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");
    }
}