Example usage for android.bluetooth.le AdvertiseCallback AdvertiseCallback

List of usage examples for android.bluetooth.le AdvertiseCallback AdvertiseCallback

Introduction

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

Prototype

AdvertiseCallback

Source Link

Usage

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

private synchronized void startAdvertising(BluetoothGattService service) {
    mGattServer.addService(service);/*from   www  .j av a2 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:io.v.android.libs.discovery.ble.BlePlugin.java

private void readvertise() {
    if (advertiseCallback != null) {
        bluetoothLeAdvertise.stopAdvertising(advertiseCallback);
        advertiseCallback = null;//ww w. j a  v a2s . com
    }
    if (advertisements.size() == 0) {
        return;
    }

    int hash = advertisements.hashCode();

    AdvertiseData.Builder builder = new AdvertiseData.Builder();
    ByteBuffer buf = ByteBuffer.allocate(9);
    buf.put((byte) 8);
    buf.putLong(hash);
    builder.addManufacturerData(1001, buf.array());
    AdvertiseSettings.Builder settingsBuilder = new AdvertiseSettings.Builder();
    settingsBuilder.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY);
    settingsBuilder.setConnectable(true);
    advertiseCallback = new AdvertiseCallback() {
        @Override
        public void onStartSuccess(AdvertiseSettings settingsInEffect) {
            Log.i("vanadium", "Successfully started " + settingsInEffect);
        }

        @Override
        public void onStartFailure(int errorCode) {
            Log.i("vanadium", "Failed to start advertising " + errorCode);
        }
    };
    bluetoothLeAdvertise.startAdvertising(settingsBuilder.build(), builder.build(), advertiseCallback);
}