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

The following examples show how to use android.bluetooth.BluetoothAdapter#getBluetoothLeAdvertiser() . 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
/**
 * 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 File: GattServer.java    From blefun-androidthings with 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
Source File: GattServerActivity.java    From sample-bluetooth-le-gattserver with Apache License 2.0 6 votes vote down vote up
/**
 * 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
Source File: AbstractHOGPServer.java    From DeviceConnect-Android with MIT License 6 votes vote down vote up
/**
 * コンストラクタ.
 * @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
Source File: AdvertiserService.java    From android-BluetoothAdvertisements with Apache License 2.0 6 votes vote down vote up
/**
 * 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
Source File: GattServer.java    From Bluefruit_LE_Connect_Android_V2 with MIT License 5 votes vote down vote up
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
Source File: L_Util.java    From AsteroidOSSync with 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 8
Source File: L_Util.java    From AsteroidOSSync with 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 9
Source File: PhysicalWeb.java    From 365browser with 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 File: L_Util.java    From SweetBlue with 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 File: L_Util.java    From SweetBlue with 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
Source File: BluetoothMedic.java    From android-beacon-library with Apache License 2.0 5 votes vote down vote up
@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
Source File: L_Util.java    From AsteroidOSSync with GNU General Public License v3.0 4 votes vote down vote up
public static BluetoothLeAdvertiser getBluetoothLeAdvertiser(BluetoothAdapter adapter)
{
    return adapter.getBluetoothLeAdvertiser();
}
 
Example 14
Source File: L_Util.java    From SweetBlue with GNU General Public License v3.0 4 votes vote down vote up
public static BluetoothLeAdvertiser getBluetoothLeAdvertiser(BluetoothAdapter adapter)
{
    return adapter.getBluetoothLeAdvertiser();
}