android.bluetooth.le.AdvertiseSettings Java Examples
The following examples show how to use
android.bluetooth.le.AdvertiseSettings.
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: AdvertiserRequest.java From Android-BLE with Apache License 2.0 | 7 votes |
private void setAdvertiserSettings(){ mAdvertiser = bluetoothAdapter.getBluetoothLeAdvertiser(); if (mAdvertiser == null) { try { throw new AdvertiserUnsupportException("Device does not support Avertise!"); } catch (AdvertiserUnsupportException e) { e.printStackTrace(); } } //设置频率: ADVERTISE_MODE_LOW_LATENCY 100ms ADVERTISE_MODE_LOW_POWER 1s ADVERTISE_MODE_BALANCED 250ms myAdvertiseSettings = new AdvertiseSettings.Builder() .setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY)//设置广播间隔100ms .setConnectable(true) .setTimeout(0) .setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH) .build(); }
Example #2
Source File: AdvertiserService.java From connectivity-samples with Apache License 2.0 | 6 votes |
/** * Starts BLE Advertising. */ private void startAdvertising() { goForeground(); Log.d(TAG, "Service: Starting Advertising"); if (mAdvertiseCallback == null) { AdvertiseSettings settings = buildAdvertiseSettings(); AdvertiseData data = buildAdvertiseData(); mAdvertiseCallback = new SampleAdvertiseCallback(); if (mBluetoothLeAdvertiser != null) { mBluetoothLeAdvertiser.startAdvertising(settings, data, mAdvertiseCallback); } } }
Example #3
Source File: JsonDeserializer.java From mobly-bundled-snippets with Apache License 2.0 | 6 votes |
@TargetApi(Build.VERSION_CODES.LOLLIPOP) public static AdvertiseSettings jsonToBleAdvertiseSettings(JSONObject jsonObject) throws JSONException { AdvertiseSettings.Builder builder = new AdvertiseSettings.Builder(); if (jsonObject.has("AdvertiseMode")) { int mode = MbsEnums.BLE_ADVERTISE_MODE.getInt(jsonObject.getString("AdvertiseMode")); builder.setAdvertiseMode(mode); } // Timeout in milliseconds. if (jsonObject.has("Timeout")) { builder.setTimeout(jsonObject.getInt("Timeout")); } if (jsonObject.has("Connectable")) { builder.setConnectable(jsonObject.getBoolean("Connectable")); } if (jsonObject.has("TxPowerLevel")) { int txPowerLevel = MbsEnums.BLE_ADVERTISE_TX_POWER.getInt(jsonObject.getString("TxPowerLevel")); builder.setTxPowerLevel(txPowerLevel); } return builder.build(); }
Example #4
Source File: GattServer.java From blefun-androidthings with Apache License 2.0 | 6 votes |
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 #5
Source File: GattServerActivity.java From sample-bluetooth-le-gattserver with Apache License 2.0 | 6 votes |
/** * 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 #6
Source File: BLEServicePeripheral.java From unity-bluetooth with MIT License | 6 votes |
@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 #7
Source File: AdvertiserRequest.java From Android-BLE with Apache License 2.0 | 6 votes |
public void startAdvertising(final byte[] payload, final AdvertiseSettings advertiseSettings){ if (bluetoothAdapter.isEnabled()){ mHandler.removeCallbacks(stopAvertiseRunnable); if(mAdvertiser != null){ ThreadUtils.asyn(new Runnable() { @Override public void run() { mAdvertiser.stopAdvertising(mAdvertiseCallback); myAdvertiseData = new AdvertiseData.Builder() .addManufacturerData(65520, payload) .setIncludeDeviceName(true) .build(); mAdvertiser.startAdvertising(advertiseSettings, myAdvertiseData, mAdvertiseCallback); } }); } } }
Example #8
Source File: EddystoneGattConfigurator.java From beacons-android with Apache License 2.0 | 6 votes |
@SuppressLint("InlinedApi") @Override public int setAdvertiseInterval(int advertiseIntervalMs) { Util.log(TAG, "setAdvertiseInterval() called with: advertiseIntervalMs = [" + advertiseIntervalMs + "]"); @Advertiser.Mode int mode; if (advertiseIntervalMs <= 100 + (250 - 100) / 2) { // 100 ms mode = AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY; } else if (advertiseIntervalMs >= 1000 - (1000 - 250) / 2) { // 1000 ms mode = AdvertiseSettings.ADVERTISE_MODE_LOW_POWER; } else { // 250 ms actually mode = AdvertiseSettings.ADVERTISE_MODE_BALANCED; } if (mode != getModifiedOrOriginalBeacon().getAdvertiseMode()) { // restarting a beacon destroys the GATT connection, make sure we use a stopped clone getOrCloneConfiguredBeacon().edit().setAdvertiseMode(mode).apply(); } return getAdvertiseInterval(); }
Example #9
Source File: AdvertiserService.java From android-BluetoothAdvertisements with Apache License 2.0 | 6 votes |
/** * Starts BLE Advertising. */ private void startAdvertising() { goForeground(); Log.d(TAG, "Service: Starting Advertising"); if (mAdvertiseCallback == null) { AdvertiseSettings settings = buildAdvertiseSettings(); AdvertiseData data = buildAdvertiseData(); mAdvertiseCallback = new SampleAdvertiseCallback(); if (mBluetoothLeAdvertiser != null) { mBluetoothLeAdvertiser.startAdvertising(settings, data, mAdvertiseCallback); } } }
Example #10
Source File: NativeUtil.java From SweetBlue with GNU General Public License v3.0 | 6 votes |
@TargetApi(Build.VERSION_CODES.LOLLIPOP) public static void setToAdvertising(BleManager mgr, final AdvertiseSettings settings, L_Util.AdvertisingCallback callback, Interval delay) { if (Utils.isLollipop()) { L_UtilBridge.setAdvListener(callback); mgr.getPostManager().postToUpdateThreadDelayed(new Runnable() { @Override public void run() { L_Util.getNativeAdvertisingCallback().onStartSuccess(settings); } }, delay.millis()); } }
Example #11
Source File: AdvertiserService.java From connectivity-samples with Apache License 2.0 | 5 votes |
/** * Returns an AdvertiseSettings object set to use low power (to help preserve battery life) * and disable the built-in timeout since this code uses its own timeout runnable. */ private AdvertiseSettings buildAdvertiseSettings() { AdvertiseSettings.Builder settingsBuilder = new AdvertiseSettings.Builder(); settingsBuilder.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_POWER); settingsBuilder.setTimeout(0); return settingsBuilder.build(); }
Example #12
Source File: DalvikBleService.java From attach with GNU General Public License v3.0 | 5 votes |
private void startBroadcast(String uuid, int major, int minor, String id) { Log.v(TAG, "Start broadcasting for uuid = "+uuid+", major = "+major+", minor = "+minor+", id = "+id); BluetoothLeAdvertiser advertiser = BluetoothAdapter.getDefaultAdapter().getBluetoothLeAdvertiser(); AdvertiseSettings parameters = new AdvertiseSettings.Builder() .setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY) .setConnectable(false) .setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH) .build(); byte[] payload = getPayload(uuid, major, minor); AdvertiseData data = new AdvertiseData.Builder() .addManufacturerData(0x004C, payload) .build(); callback = new AdvertiseCallback() { @Override public void onStartSuccess(AdvertiseSettings settingsInEffect) { super.onStartSuccess(settingsInEffect); Log.v(TAG, "onStartSuccess"); } @Override public void onStartFailure(int errorCode) { Log.e(TAG, "Advertising onStartFailure: " + errorCode); super.onStartFailure(errorCode); } }; Log.v(TAG, "Advertise with payload = " + Arrays.toString(payload)); advertiser.startAdvertising(parameters, data, callback); }
Example #13
Source File: GattServer.java From Bluefruit_LE_Connect_Android_V2 with MIT License | 5 votes |
@Override public void onStartSuccess(AdvertiseSettings settingsInEffect) { super.onStartSuccess(settingsInEffect); Log.d(TAG, "Advertising onStartSuccess"); mIsAdvertising = true; if (mListener != null) { mListener.onDidStartAdvertising(); } }
Example #14
Source File: JsonSerializer.java From mobly-bundled-snippets with Apache License 2.0 | 5 votes |
@TargetApi(Build.VERSION_CODES.LOLLIPOP) public static Bundle serializeBleAdvertisingSettings(AdvertiseSettings advertiseSettings) { Bundle result = new Bundle(); result.putString( "TxPowerLevel", MbsEnums.BLE_ADVERTISE_TX_POWER.getString(advertiseSettings.getTxPowerLevel())); result.putString( "Mode", MbsEnums.BLE_ADVERTISE_MODE.getString(advertiseSettings.getMode())); result.putInt("Timeout", advertiseSettings.getTimeout()); result.putBoolean("IsConnectable", advertiseSettings.isConnectable()); return result; }
Example #15
Source File: MbsEnums.java From mobly-bundled-snippets with Apache License 2.0 | 5 votes |
private static RpcEnum buildBleAdvertiseTxPowerEnum() { RpcEnum.Builder builder = new RpcEnum.Builder(); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { return builder.build(); } return builder.add( "ADVERTISE_TX_POWER_ULTRA_LOW", AdvertiseSettings.ADVERTISE_TX_POWER_ULTRA_LOW) .add("ADVERTISE_TX_POWER_LOW", AdvertiseSettings.ADVERTISE_TX_POWER_LOW) .add("ADVERTISE_TX_POWER_MEDIUM", AdvertiseSettings.ADVERTISE_TX_POWER_MEDIUM) .add("ADVERTISE_TX_POWER_HIGH", AdvertiseSettings.ADVERTISE_TX_POWER_HIGH) .build(); }
Example #16
Source File: MbsEnums.java From mobly-bundled-snippets with Apache License 2.0 | 5 votes |
private static RpcEnum buildBleAdvertiseModeEnum() { RpcEnum.Builder builder = new RpcEnum.Builder(); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { return builder.build(); } return builder.add("ADVERTISE_MODE_BALANCED", AdvertiseSettings.ADVERTISE_MODE_BALANCED) .add("ADVERTISE_MODE_LOW_LATENCY", AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY) .add("ADVERTISE_MODE_LOW_POWER", AdvertiseSettings.ADVERTISE_MODE_LOW_POWER) .build(); }
Example #17
Source File: BluetoothLeAdvertiserSnippet.java From mobly-bundled-snippets with Apache License 2.0 | 5 votes |
public void onStartSuccess(AdvertiseSettings settingsInEffect) { Log.e("Bluetooth LE advertising started with settings: " + settingsInEffect.toString()); SnippetEvent event = new SnippetEvent(mCallbackId, "onStartSuccess"); Bundle advertiseSettings = JsonSerializer.serializeBleAdvertisingSettings(settingsInEffect); event.getData().putBundle("SettingsInEffect", advertiseSettings); sEventCache.postEvent(event); }
Example #18
Source File: BeaconBroadcast.java From react-native-ibeacon-simulator with MIT License | 5 votes |
@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 #19
Source File: BeaconSimulatorFragment.java From iBeacon-Android with Apache License 2.0 | 5 votes |
private void transmitIBeacon() { boolean isSupported = false; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) { isSupported = btAdapter.isMultipleAdvertisementSupported(); if (isSupported) { Log.v(TAG, "is support advertistment"); if (beaconTransmitter.isStarted()) { beaconTransmitter.stopAdvertising(); beaconIv.setAnimation(null); } else { beaconTransmitter.startAdvertising(beacon, new AdvertiseCallback() { @Override public void onStartFailure(int errorCode) { Log.e(TAG, "Advertisement start failed with code: " + errorCode); } @TargetApi(Build.VERSION_CODES.LOLLIPOP) @Override public void onStartSuccess(AdvertiseSettings settingsInEffect) { Log.i(TAG, "Advertisement start succeeded." + settingsInEffect.toString()); } }); beaconIv.startAnimation(anim); } } else { UiHelper.showErrorMessage(getActivity(), "Your device is not support leBluetooth."); } } else { UiHelper.showErrorMessage(getActivity(), "Your device is not support leBluetooth."); } }
Example #20
Source File: L_Util.java From AsteroidOSSync with GNU General Public License v3.0 | 5 votes |
@Override public void onStartSuccess(AdvertiseSettings settingsInEffect) { if (m_userAdvCallback != null) { m_userAdvCallback.onStartSuccess(fromNativeSettings(settingsInEffect)); } }
Example #21
Source File: L_Util.java From AsteroidOSSync with GNU General Public License v3.0 | 5 votes |
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 #22
Source File: BleAdvertisingPacket.java From AsteroidOSSync with GNU General Public License v3.0 | 5 votes |
@TargetApi(Build.VERSION_CODES.LOLLIPOP) /*package*/ AdvertiseSettings getNativeSettings(BleAdvertisingMode mode, BleTransmissionPower power, Interval timeout) { AdvertiseSettings.Builder settings = new AdvertiseSettings.Builder(); settings.setAdvertiseMode(mode.getNativeMode()); settings.setTxPowerLevel(power.getNativeMode()); settings.setConnectable(isConnectable()); settings.setTimeout((int) timeout.millis()); return settings.build(); }
Example #23
Source File: P_AndroidBluetoothManager.java From AsteroidOSSync with GNU General Public License v3.0 | 5 votes |
@Override public final void startAdvertising(AdvertiseSettings settings, AdvertiseData adData, L_Util.AdvertisingCallback callback) { if (!L_Util.startAdvertising(m_adaptor, settings, adData, callback)) { m_bleManager.ASSERT(false, "Unable to start advertising!"); m_bleManager.getLogger().e("Failed to start advertising!"); } }
Example #24
Source File: Advertiser.java From beacons-android with Apache License 2.0 | 5 votes |
public Advertiser(SettingsProvider provider) { mAdvertiseSettings = new AdvertiseSettings.Builder() .setAdvertiseMode(provider.getAdvertiseMode()) .setTxPowerLevel(provider.getTxPowerLevel()) .setConnectable(provider.isConnectable()) // oups! https://code.google.com/p/android/issues/detail?id=232219 // .setTimeout(provider.getTimeout()) .build(); }
Example #25
Source File: Advertiser.java From beacons-android with Apache License 2.0 | 5 votes |
@Override public void onStartSuccess(AdvertiseSettings settingsInEffect) { mStatus = STATUS_RUNNING; mSettingsInEffect = settingsInEffect; // on start or restart, rebase the clock time used for PDU count estimation mLastPDUUpdateTime = SystemClock.elapsedRealtime(); if (null != mAdvertisersManager) { mAdvertisersManager.onAdvertiserStarted(this); } }
Example #26
Source File: EddystoneGattConfigurator.java From beacons-android with Apache License 2.0 | 5 votes |
@Override public int getAdvertiseInterval() { switch (getModifiedOrOriginalBeacon().getAdvertiseMode()) { case AdvertiseSettings.ADVERTISE_MODE_LOW_POWER: default: return 1000; case AdvertiseSettings.ADVERTISE_MODE_BALANCED: return 250; case AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY: return 100; } }
Example #27
Source File: AbstractHOGPServer.java From DeviceConnect-Android with MIT License | 5 votes |
/** * アドバタイジングを開始します. */ private void startAdvertising() { if (DEBUG) { Log.d(TAG, "startAdvertising"); } mHandler.post(new Runnable() { @Override public void run() { // set up advertising setting final AdvertiseSettings advertiseSettings = new AdvertiseSettings.Builder() .setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH) .setConnectable(true) .setTimeout(0) .setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY) .build(); // set up advertising data final AdvertiseData advertiseData = new AdvertiseData.Builder() .setIncludeTxPowerLevel(false) .setIncludeDeviceName(true) .addServiceUuid(ParcelUuid.fromString(SERVICE_DEVICE_INFORMATION.toString())) .addServiceUuid(ParcelUuid.fromString(SERVICE_BLE_HID.toString())) .addServiceUuid(ParcelUuid.fromString(SERVICE_BATTERY.toString())) .build(); // set up scan result final AdvertiseData scanResult = new AdvertiseData.Builder() .addServiceUuid(ParcelUuid.fromString(SERVICE_DEVICE_INFORMATION.toString())) .addServiceUuid(ParcelUuid.fromString(SERVICE_BLE_HID.toString())) .addServiceUuid(ParcelUuid.fromString(SERVICE_BATTERY.toString())) .build(); mBluetoothLeAdvertiser.startAdvertising(advertiseSettings, advertiseData, scanResult, mAdvertiseCallback); } }); }
Example #28
Source File: AdvertiserService.java From android-BluetoothAdvertisements with Apache License 2.0 | 5 votes |
/** * Returns an AdvertiseSettings object set to use low power (to help preserve battery life) * and disable the built-in timeout since this code uses its own timeout runnable. */ private AdvertiseSettings buildAdvertiseSettings() { AdvertiseSettings.Builder settingsBuilder = new AdvertiseSettings.Builder(); settingsBuilder.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_POWER); settingsBuilder.setTimeout(0); return settingsBuilder.build(); }
Example #29
Source File: L_Util.java From SweetBlue with GNU General Public License v3.0 | 5 votes |
@Override public void onStartSuccess(AdvertiseSettings settingsInEffect) { if (m_userAdvCallback != null) { m_userAdvCallback.onStartSuccess(fromNativeSettings(settingsInEffect)); } }
Example #30
Source File: L_Util.java From SweetBlue with GNU General Public License v3.0 | 5 votes |
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; }