Java Code Examples for android.bluetooth.BluetoothAdapter#getBluetoothLeAdvertiser()

The following examples show how to use android.bluetooth.BluetoothAdapter#getBluetoothLeAdvertiser() . These examples are extracted from open source projects. 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
/**
 * Get references to system Bluetooth objects if we don't have them already.
 */
private void initialize() {
    if (mBluetoothLeAdvertiser == null) {
        BluetoothManager mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
        if (mBluetoothManager != null) {
            BluetoothAdapter mBluetoothAdapter = mBluetoothManager.getAdapter();
            if (mBluetoothAdapter != null) {
                mBluetoothLeAdvertiser = mBluetoothAdapter.getBluetoothLeAdvertiser();
            } else {
                Toast.makeText(this, getString(R.string.bt_null), Toast.LENGTH_LONG).show();
            }
        } else {
            Toast.makeText(this, getString(R.string.bt_null), Toast.LENGTH_LONG).show();
        }
    }

}
 
Example 2
Source Project: blefun-androidthings   File: GattServer.java    License: Apache License 2.0 6 votes vote down vote up
private void startAdvertising() {
    BluetoothAdapter bluetoothAdapter = mBluetoothManager.getAdapter();
    mBluetoothLeAdvertiser = bluetoothAdapter.getBluetoothLeAdvertiser();
    if (mBluetoothLeAdvertiser == null) {
        Log.w(TAG, "Failed to create advertiser");
        return;
    }

    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()
            .setIncludeDeviceName(true)
            .setIncludeTxPowerLevel(false)
            .addServiceUuid(new ParcelUuid(SERVICE_UUID))
            .build();

    mBluetoothLeAdvertiser
            .startAdvertising(settings, data, mAdvertiseCallback);
}
 
Example 3
/**
 * Begin advertising over Bluetooth that this device is connectable
 * and supports the Current Time Service.
 */
private void startAdvertising() {
    BluetoothAdapter bluetoothAdapter = mBluetoothManager.getAdapter();
    mBluetoothLeAdvertiser = bluetoothAdapter.getBluetoothLeAdvertiser();
    if (mBluetoothLeAdvertiser == null) {
        Log.w(TAG, "Failed to create advertiser");
        return;
    }

    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()
            .setIncludeDeviceName(true)
            .setIncludeTxPowerLevel(false)
            .addServiceUuid(new ParcelUuid(TimeProfile.TIME_SERVICE))
            .build();

    mBluetoothLeAdvertiser
            .startAdvertising(settings, data, mAdvertiseCallback);
}
 
Example 4
/**
 * コンストラクタ.
 * @param context このインスタンスが属するコンテキスト
 * @throws UnsupportedOperationException BluetoothやBLEがサポートされていない場合に発生
 */
AbstractHOGPServer(final Context context) throws UnsupportedOperationException {
    mApplicationContext = context.getApplicationContext();

    final BluetoothManager btMgr = (BluetoothManager) mApplicationContext.getSystemService(Context.BLUETOOTH_SERVICE);
    final BluetoothAdapter btAdapter = btMgr.getAdapter();
    if (btAdapter == null) {
        throw new UnsupportedOperationException("Bluetooth is not available.");
    }

    if (!btAdapter.isEnabled()) {
        throw new UnsupportedOperationException("Bluetooth is disabled.");
    }

    if (!btAdapter.isMultipleAdvertisementSupported()) {
        throw new UnsupportedOperationException("Bluetooth LE Advertising not supported on this device.");
    }

    mBluetoothLeAdvertiser = btAdapter.getBluetoothLeAdvertiser();
    if (mBluetoothLeAdvertiser == null) {
        throw new UnsupportedOperationException("Bluetooth LE Advertising not supported on this device.");
    }
}
 
Example 5
/**
 * Get references to system Bluetooth objects if we don't have them already.
 */
private void initialize() {
    if (mBluetoothLeAdvertiser == null) {
        BluetoothManager mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
        if (mBluetoothManager != null) {
            BluetoothAdapter mBluetoothAdapter = mBluetoothManager.getAdapter();
            if (mBluetoothAdapter != null) {
                mBluetoothLeAdvertiser = mBluetoothAdapter.getBluetoothLeAdvertiser();
            } else {
                Toast.makeText(this, getString(R.string.bt_null), Toast.LENGTH_LONG).show();
            }
        } else {
            Toast.makeText(this, getString(R.string.bt_null), Toast.LENGTH_LONG).show();
        }
    }

}
 
Example 6
private void onBluetoothStateChanged(Context context) {
    // Setup advertiser
    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    if (adapter != null) {

        if (!adapter.isEnabled()) {
            Log.w(TAG, "BluetoothLE is disabled");
        }

        if (!adapter.isMultipleAdvertisementSupported()) {
            Log.w(TAG, "Multiple advertisement not supported");
        }

        mAdvertiser = adapter.getBluetoothLeAdvertiser();

        if (mAdvertiser == null) {
            Log.w(TAG, "Device does not support Peripheral Mode");
        }
    } else {
        Log.w(TAG, "Device does not support Bluetooth");
    }

    if (mAdvertiser == null) {
        mIsAdvertising = false;
    }

    if (mShouldStartAdvertising && mAdvertiser != null) {
        startAdvertising(context);
    }
}
 
Example 7
public static boolean startAdvertising(BluetoothAdapter adapter, AdvertiseSettings settings, AdvertiseData adData, AdvertisingCallback callback)
{
    final BluetoothLeAdvertiser adv = adapter.getBluetoothLeAdvertiser();
    if (adv == null)
        return false;

    m_userAdvCallback = callback;
    adv.startAdvertising(settings, adData, m_nativeAdvertiseCallback);
    return true;
}
 
Example 8
public static void stopAdvertising(BluetoothAdapter adapter)
{
    if (adapter != null)
    {
        final BluetoothLeAdvertiser adv = adapter.getBluetoothLeAdvertiser();
        if (adv != null)
        {
            adv.stopAdvertising(m_nativeAdvertiseCallback);
        }
    }
}
 
Example 9
Source Project: 365browser   File: PhysicalWeb.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Check if the device bluetooth hardware supports BLE advertisements.
 */
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static boolean hasBleAdvertiseCapability() {
    Context context = ContextUtils.getApplicationContext();
    BluetoothManager bluetoothManager =
            (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
    BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();
    return bluetoothAdapter != null && bluetoothAdapter.getBluetoothLeAdvertiser() != null;
}
 
Example 10
Source Project: SweetBlue   File: L_Util.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static boolean startAdvertising(BluetoothAdapter adapter, AdvertiseSettings settings, AdvertiseData adData, AdvertisingCallback callback)
{
    final BluetoothLeAdvertiser adv = adapter.getBluetoothLeAdvertiser();
    if (adv == null)
        return false;

    m_userAdvCallback = callback;
    adv.startAdvertising(settings, adData, m_nativeAdvertiseCallback);
    return true;
}
 
Example 11
Source Project: SweetBlue   File: L_Util.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static void stopAdvertising(BluetoothAdapter adapter)
{
    if (adapter != null)
    {
        final BluetoothLeAdvertiser adv = adapter.getBluetoothLeAdvertiser();
        if (adv != null)
        {
            adv.stopAdvertising(m_nativeAdvertiseCallback);
        }
    }
}
 
Example 12
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private BluetoothLeAdvertiser getAdvertiserSafely(BluetoothAdapter adapter) {
    try {
        // This can sometimes throw a NullPointerException as reported here:
        // https://github.com/AltBeacon/android-beacon-library/issues/672
        return adapter.getBluetoothLeAdvertiser();
    }
    catch (Exception e) {
        LogManager.w(TAG, "Cannot get bluetoothLeAdvertiser", e);
    }
    return null;
}
 
Example 13
public static BluetoothLeAdvertiser getBluetoothLeAdvertiser(BluetoothAdapter adapter)
{
    return adapter.getBluetoothLeAdvertiser();
}
 
Example 14
Source Project: SweetBlue   File: L_Util.java    License: GNU General Public License v3.0 4 votes vote down vote up
public static BluetoothLeAdvertiser getBluetoothLeAdvertiser(BluetoothAdapter adapter)
{
    return adapter.getBluetoothLeAdvertiser();
}