Java Code Examples for android.bluetooth.le.AdvertiseCallback

The following are top voted examples for showing how to use android.bluetooth.le.AdvertiseCallback. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: coinblesk-client-gui   File: BluetoothLEServer.java   Source Code and License 6 votes vote down vote up
@Override
public void onStop() {
    if (bluetoothGattServer != null) {
        bluetoothGattServer.clearServices();
        bluetoothGattServer.close();
        bluetoothGattServer = null;
    }

    if (bluetoothLeAdvertiseCallbacks != null) {
        if (bluetoothAdapter != null) {
            for (AdvertiseCallback callback : bluetoothLeAdvertiseCallbacks) {
                bluetoothAdapter.getBluetoothLeAdvertiser().stopAdvertising(callback);
            }
        }
        bluetoothLeAdvertiseCallbacks.clear();
    }

    connectedDevices.clear();
}
 
Example 2
Project: Mycroft-Android   File: BeaconUtil.java   Source Code and License 6 votes vote down vote up
public void broadcastAsBeacon() {
        Beacon beacon = new Beacon.Builder()
                .setId1("2f234454-cf6d-4a0f-adf2-f4911ba9ffa6")
                .setId2("1")
                .setId3("2")
                .setManufacturer(0x0118) // Radius Networks.  Change this for other beacon layouts
                .setTxPower(-59)
                .setDataFields(Arrays.asList(new Long[]{0l})) // Remove this for beacon layouts without d: fields
                .build();
// Change the layout below for other beacon types
        BeaconParser beaconParser = new BeaconParser()
                .setBeaconLayout("m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25");
        BeaconTransmitter beaconTransmitter = new BeaconTransmitter(context, beaconParser);
        beaconTransmitter.startAdvertising(beacon, new AdvertiseCallback() {

            @Override
            public void onStartFailure(int errorCode) {
                Log.e("Class", "Advertisement start failed with code: " + errorCode);
            }

            @Override
            public void onStartSuccess(AdvertiseSettings settingsInEffect) {
                Log.i("class", "Advertisement start succeeded.");
            }
        });
    }
 
Example 3
Project: mobly-bundled-snippets   File: BluetoothLeAdvertiserSnippet.java   Source Code and License 5 votes vote down vote up
/**
 * Stop a BLE advertising.
 *
 * @param callbackId The callbackId corresponding to the {@link
 *     BluetoothLeAdvertiserSnippet#bleStartAdvertising} call that started the advertising.
 * @throws BluetoothLeScannerSnippet.BluetoothLeScanSnippetException
 */
@RpcMinSdk(Build.VERSION_CODES.LOLLIPOP_MR1)
@Rpc(description = "Stop BLE advertising.")
public void bleStopAdvertising(String callbackId) throws BluetoothLeAdvertiserSnippetException {
    AdvertiseCallback callback = mAdvertiseCallbacks.remove(callbackId);
    if (callback == null) {
        throw new BluetoothLeAdvertiserSnippetException(
                "No advertising session found for ID " + callbackId);
    }
    mAdvertiser.stopAdvertising(callback);
}
 
Example 4
Project: mobly-bundled-snippets   File: BluetoothLeAdvertiserSnippet.java   Source Code and License 5 votes vote down vote up
@Override
public void shutdown() {
    for (AdvertiseCallback callback : mAdvertiseCallbacks.values()) {
        mAdvertiser.stopAdvertising(callback);
    }
    mAdvertiseCallbacks.clear();
}
 
Example 5
Project: react-native-ibeacon-simulator   File: BeaconBroadcast.java   Source Code and License 5 votes vote down vote up
@ReactMethod
public void startSharedAdvertisingBeaconWithString(String uuid, int major, int minor,String identifier) {
int manufacturer = 0x4C;
Beacon beacon = new Beacon.Builder()
		.setId1(uuid)
		.setId2(String.valueOf(major))
		.setId3(String.valueOf(minor))
		.setManufacturer(manufacturer)
		.setBluetoothName(identifier)
		.setTxPower(-59)
		.build();
BeaconParser beaconParser = new BeaconParser()
		.setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24");
this.beaconTransmitter = new BeaconTransmitter(context, beaconParser);
this.beaconTransmitter.startAdvertising(beacon, new AdvertiseCallback() {

	@Override
	public void onStartFailure(int errorCode) {
		Log.d("ReactNative", "Error from start advertising " + errorCode);
	}

	@Override
	public void onStartSuccess(AdvertiseSettings settingsInEffect) {
		Log.d("ReactNative", "Success start advertising");
	}
});
}
 
Example 6
Project: coinblesk-client-gui   File: BluetoothLEServer.java   Source Code and License 5 votes vote down vote up
private void startAdvertisingService(final UUID uuid) {
    if (bluetoothLeAdvertiseCallbacks == null) {
        bluetoothLeAdvertiseCallbacks = new ArrayList<>();
    }

    AdvertiseCallback advertiseCallback = new AdvertiseCallback() {
        @Override
        public void onStartSuccess(AdvertiseSettings settingsInEffect) {
            Log.d(TAG, "BLE advertising - uuid: " + uuid.toString()
                    + " - onStartSuccess - settings: " + settingsInEffect.toString());
        }

        @Override
        public void onStartFailure(int errorCode) {
            Log.w(TAG, "BLE advertising - uuid: " + uuid.toString()
                    + " - onStartFailure - errorCode=" + errorCode);
        }
    };
    bluetoothLeAdvertiseCallbacks.add(advertiseCallback);

    bluetoothAdapter.getBluetoothLeAdvertiser().startAdvertising(
            new AdvertiseSettings.Builder()
                    .setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY)
                    .setConnectable(true)
                    .setTimeout(0)
                    .setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH)
                    .build(),
            new AdvertiseData.Builder()
                    .setIncludeDeviceName(false)
                    .addServiceUuid(new ParcelUuid(uuid))
                    .build(),
            advertiseCallback
    );
}
 
Example 7
Project: AsteroidOSSync   File: P_AndroidBluetoothManager.java   Source Code and License 5 votes vote down vote up
@Override
public final void startAdvertising(AdvertiseSettings settings, AdvertiseData adData, AdvertiseCallback callback)
{
    final BluetoothLeAdvertiser ad = L_Util.getBluetoothLeAdvertiser(m_adaptor);
    if (ad != null)
    {
        ad.startAdvertising(settings, adData, callback);
    }
    else
    {
        m_bleManager.getLogger().e("Tried to start advertising, but the BluetoothLeAdvertiser was null!");
    }
}
 
Example 8
Project: AsteroidOSSync   File: P_AndroidBluetoothManager.java   Source Code and License 5 votes vote down vote up
@Override
public final void stopAdvertising(AdvertiseCallback callback)
{
    final BluetoothLeAdvertiser ad = L_Util.getBluetoothLeAdvertiser(m_adaptor);
    if (ad != null)
    {
        ad.stopAdvertising(callback);
    }
    else
    {
        m_bleManager.getLogger().e("Tried to stop advertising, but the BluetoothLeAdvertiser was null!");
    }
}
 
Example 9
Project: beacons-android   File: BleService.java   Source Code and License 5 votes vote down vote up
/**
 * Start or restart the advertising of an item's BLE beacon.
 * @param beacon    The item to (re)start.
 * @return True if the beacon was started, false otherwise.
 */
public boolean startBeaconAdvertiser(Beacon beacon) {
    if (null == mAdvertisersManager) {
        return false;
    }

    if (!mAdvertisersManager.isBluetoothEnabled()) {
        return false;
    }

    if (!mAdvertisersManager.canAdvertise()) {
        beacon.onAdvertiseFailed(AdvertiseCallback.ADVERTISE_FAILED_FEATURE_UNSUPPORTED);
        broadcastBeaconEvent(EVENT_ADVERTISE_UNSUPPORTED, beacon);
        return false;
    }

    // stop current advertiser for this beacon
    Advertiser existingAdvertiser = beacon.getAdvertiser();
    if (null != existingAdvertiser) {
        mAdvertisersManager.stopAdvertiser(existingAdvertiser);
        mEstimatedPDUCount += existingAdvertiser.clearPDUCount();
    }

    Advertiser advertiser = beacon.recreateAdvertiser(this);
    if (null != advertiser) {
        advertiser.setManager(mAdvertisersManager);
    }

    return null != advertiser && mAdvertisersManager.startAdvertiser(advertiser);
}
 
Example 10
Project: beacons-android   File: Beacon.java   Source Code and License 5 votes vote down vote up
public void onAdvertiseFailed(int errorCode) {
    if (AdvertiseCallback.ADVERTISE_FAILED_TOO_MANY_ADVERTISERS == errorCode){
        // user may attempt to start the beacon again when we get a free slot
        pause();
    }
    else {
        // fatal, no point in keeping the beacon in active state
        stop();
    }

    mAdvertiser = null;
    mErrorCode = errorCode;
    setErrorDetails(Advertiser.getErrorName(errorCode));
}
 
Example 11
Project: mobly-bundled-snippets   File: BluetoothLeAdvertiserSnippet.java   Source Code and License 4 votes vote down vote up
/**
 * Start Bluetooth LE advertising.
 *
 * <p>This can be called multiple times, and each call is associated with a {@link
 * AdvertiseCallback} object, which is used to stop the advertising.
 *
 * @param callbackId
 * @param advertiseSettings A JSONObject representing a {@link AdvertiseSettings object}. E.g.
 *     <pre>
 *          {
 *            "AdvertiseMode": "ADVERTISE_MODE_BALANCED",
 *            "Timeout": (int, milliseconds),
 *            "Connectable": (bool),
 *            "TxPowerLevel": "ADVERTISE_TX_POWER_LOW"
 *          }
 *     </pre>
 *
 * @param advertiseData A JSONObject representing a {@link AdvertiseData} object. E.g.
 *     <pre>
 *          {
 *            "IncludeDeviceName": (bool),
 *            # JSON list, each element representing a set of service data, which is composed of
 *            # a UUID, and an optional string.
 *            "ServiceData": [
 *                      {
 *                        "UUID": (A string representation of {@link ParcelUuid}),
 *                        "Data": (Optional, The string representation of what you want to
 *                                 advertise, base64 encoded)
 *                        # If you want to add a UUID without data, simply omit the "Data"
 *                        # field.
 *                      }
 *                ]
 *          }
 *     </pre>
 *
 * @throws BluetoothLeAdvertiserSnippetException
 * @throws JSONException
 */
@RpcMinSdk(Build.VERSION_CODES.LOLLIPOP_MR1)
@AsyncRpc(description = "Start BLE advertising.")
public void bleStartAdvertising(
        String callbackId, JSONObject advertiseSettings, JSONObject advertiseData)
        throws BluetoothLeAdvertiserSnippetException, JSONException {
    if (!BluetoothAdapter.getDefaultAdapter().isEnabled()) {
        throw new BluetoothLeAdvertiserSnippetException(
                "Bluetooth is disabled, cannot start BLE advertising.");
    }
    AdvertiseSettings settings = JsonDeserializer.jsonToBleAdvertiseSettings(advertiseSettings);
    AdvertiseData data = JsonDeserializer.jsonToBleAdvertiseData(advertiseData);
    AdvertiseCallback advertiseCallback = new DefaultAdvertiseCallback(callbackId);
    mAdvertiser.startAdvertising(settings, data, advertiseCallback);
    mAdvertiseCallbacks.put(callbackId, advertiseCallback);
}
 
Example 12
Project: AsteroidOSSync   File: UnitTestManagerLayer.java   Source Code and License 4 votes vote down vote up
@Override
public void startAdvertising(AdvertiseSettings settings, AdvertiseData adData, AdvertiseCallback callback)
{
}
 
Example 13
Project: AsteroidOSSync   File: UnitTestManagerLayer.java   Source Code and License 4 votes vote down vote up
@Override
public void stopAdvertising(AdvertiseCallback callback)
{
}
 
Example 14
Project: AndroidBluttoothMasterAndSlave   File: BLEAdvertiser.java   Source Code and License 4 votes vote down vote up
private void initAdvertiseData() {
    //初始化Advertise的设定
    Context mContext = contextWeakReference.get();
    if (mContext == null) {
        prepared = false;
        return;
    }
    BluetoothManager bluetoothManager = (BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE);
    BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();
    advertiser = bluetoothAdapter.getBluetoothLeAdvertiser();
    advertiseCallback = new AdvertiseCallback() {
        @Override
        public void onStartSuccess(AdvertiseSettings settingsInEffect) {
            super.onStartSuccess(settingsInEffect);
            Log.d(TAG, "["+
                    Thread.currentThread().getStackTrace()[2].getFileName() + "_" +
                    Thread.currentThread().getStackTrace()[2].getLineNumber() + "_" +
                    Thread.currentThread().getStackTrace()[2].getMethodName() + "]");
            Log.i(TAG, "onStartSuccess success");

            advertiseResultListener.onAdvertiseSuccess();

            if (settingsInEffect != null) {
                Log.d(TAG, "onStartSuccess TxPowerLv=" + settingsInEffect.getTxPowerLevel() + " mode=" + settingsInEffect.getMode()
                        + " timeout=" + settingsInEffect.getTimeout());
            } else {
                Log.e(TAG, "onStartSuccess, settingInEffect is null");
            }
            Log.i(TAG, "onStartSuccess settingsInEffect" + settingsInEffect);
        }

        @Override
        public void onStartFailure(int errorCode) {
            super.onStartFailure(errorCode);
            Log.e(TAG, "Advertise failed.Error code: " + errorCode);
            advertiseResultListener.onAdvertiseFailed(errorCode);
        }
    };
    AdvertiseSettings.Builder settingsBuilder = new AdvertiseSettings.Builder();
    settingsBuilder.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY);//ADVERTISE_MODE_BALANCED origin
    settingsBuilder.setConnectable(true);
    settingsBuilder.setTimeout(0);
    settingsBuilder.setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH);// ADVERTISE_TX_POWER_MEDIUM origin
    advertiseSettings = settingsBuilder.build();
    AdvertiseData.Builder dataBuilder = new AdvertiseData.Builder();
    dataBuilder.setIncludeDeviceName(true);
    dataBuilder.addServiceUuid(ParcelUuid.fromString(BLEProfile.UUID_SERVICE));
    advertiseData = dataBuilder.build();
    prepared = true;
}
 
Example 15
Project: Android-BLE-Library   File: BLEAdvertiser.java   Source Code and License 4 votes vote down vote up
private void initAdvertiseData() {
    //初始化Advertise的设定
    Context mContext = contextWeakReference.get();
    if (mContext == null) {
        prepared = false;
        return;
    }
    BluetoothManager bluetoothManager = (BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE);
    BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();
    advertiser = bluetoothAdapter.getBluetoothLeAdvertiser();
    advertiseCallback = new AdvertiseCallback() {
        @Override
        public void onStartSuccess(AdvertiseSettings settingsInEffect) {
            super.onStartSuccess(settingsInEffect);
            Log.i(TAG, "Advertise success");

            advertiseResultListener.onAdvertiseSuccess();

            if (settingsInEffect != null) {
                Log.d(TAG, "onStartSuccess TxPowerLv=" + settingsInEffect.getTxPowerLevel() + " mode=" + settingsInEffect.getMode()
                        + " timeout=" + settingsInEffect.getTimeout());
            } else {
                Log.e(TAG, "onStartSuccess, settingInEffect is null");
            }
            Log.i(TAG, "onStartSuccess settingsInEffect" + settingsInEffect);
        }

        @Override
        public void onStartFailure(int errorCode) {
            super.onStartFailure(errorCode);
            Log.e(TAG, "Advertise failed.Error code: " + errorCode);
            advertiseResultListener.onAdvertiseFailed(errorCode);
        }
    };
    AdvertiseSettings.Builder settingsBuilder = new AdvertiseSettings.Builder();
    settingsBuilder.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_BALANCED);
    settingsBuilder.setConnectable(true);
    settingsBuilder.setTimeout(0);
    settingsBuilder.setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_MEDIUM);
    advertiseSettings = settingsBuilder.build();
    AdvertiseData.Builder dataBuilder = new AdvertiseData.Builder();
    dataBuilder.setIncludeDeviceName(true);
    dataBuilder.addServiceUuid(ParcelUuid.fromString(BLEProfile.UUID_SERVICE));
    advertiseData = dataBuilder.build();
    prepared = true;
}
 
Example 16
Project: SweetBlue   File: L_Util.java   Source Code and License 4 votes vote down vote up
public static AdvertiseCallback getNativeAdvertisingCallback()
{
    return m_nativeAdvertiseCallback;
}
 
Example 17
Project: android-beacon-library   File: BeaconTransmitter.java   Source Code and License 4 votes vote down vote up
/**
 * Starts advertising with fields from the passed beacon
 * @param beacon
 */
public void startAdvertising(Beacon beacon, AdvertiseCallback callback) {
    mBeacon = beacon;
    mAdvertisingClientCallback = callback;
    startAdvertising();
}
 
Example 18
Project: AsteroidOSSync   File: P_NativeManagerLayer.java   Source Code and License votes vote down vote up
void startAdvertising(AdvertiseSettings settings, AdvertiseData adData, AdvertiseCallback callback); 
Example 19
Project: AsteroidOSSync   File: P_NativeManagerLayer.java   Source Code and License votes vote down vote up
void stopAdvertising(AdvertiseCallback callback);