@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(); }
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."); } }); }
/** * 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); }
@Override public void shutdown() { for (AdvertiseCallback callback : mAdvertiseCallbacks.values()) { mAdvertiser.stopAdvertising(callback); } mAdvertiseCallbacks.clear(); }
@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"); } }); }
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 ); }
@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!"); } }
@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!"); } }
/** * 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); }
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)); }
/** * 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); }
@Override public void startAdvertising(AdvertiseSettings settings, AdvertiseData adData, AdvertiseCallback callback) { }
@Override public void stopAdvertising(AdvertiseCallback callback) { }
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; }
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; }
public static AdvertiseCallback getNativeAdvertisingCallback() { return m_nativeAdvertiseCallback; }
/** * Starts advertising with fields from the passed beacon * @param beacon */ public void startAdvertising(Beacon beacon, AdvertiseCallback callback) { mBeacon = beacon; mAdvertisingClientCallback = callback; startAdvertising(); }
void startAdvertising(AdvertiseSettings settings, AdvertiseData adData, AdvertiseCallback callback);
void stopAdvertising(AdvertiseCallback callback);