Java Code Examples for org.altbeacon.beacon.logging.LogManager#e()

The following examples show how to use org.altbeacon.beacon.logging.LogManager#e() . 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
@MainThread
@Override
public void onDestroy() {
    LogManager.e(TAG, "onDestroy()");
    if (android.os.Build.VERSION.SDK_INT < 18) {
        LogManager.w(TAG, "Not supported prior to API 18.");
        return;
    }
    if (mBeaconNotificationProcessor != null) {
        mBeaconNotificationProcessor.unregister();
    }
    if (bluetoothCrashResolver != null) {
        bluetoothCrashResolver.stop();
    }
    LogManager.i(TAG, "onDestroy called.  stopping scanning");
    handler.removeCallbacksAndMessages(null);

    if (mScanHelper.getCycledScanner() != null) {
        mScanHelper.getCycledScanner().stop();
        mScanHelper.getCycledScanner().destroy();
    }
    mScanHelper.getMonitoringStatus().stopStatusPreservation();
    mScanHelper.terminateThreads();
}
 
Example 2
protected BluetoothAdapter getBluetoothAdapter() {
    try {
        if (mBluetoothAdapter == null) {
            // Initializes Bluetooth adapter.
            final BluetoothManager bluetoothManager =
                    (BluetoothManager) mContext.getApplicationContext().getSystemService(Context.BLUETOOTH_SERVICE);
            mBluetoothAdapter = bluetoothManager.getAdapter();
            if (mBluetoothAdapter == null) {
                LogManager.w(TAG, "Failed to construct a BluetoothAdapter");
            }
        }
    }
    catch (SecurityException e) {
        // Thrown by Samsung Knox devices if bluetooth access denied for an app
        LogManager.e(TAG, "Cannot consruct bluetooth adapter.  Security Exception");
    }
    return mBluetoothAdapter;
}
 
Example 3
/**
 * Configures using a `ScanJob` run with the `JobScheduler` to perform scans rather than using a
 * long-running `BeaconService` to do so.
 *
 * Calling with true on devices older than Android L (5.0) will not apply the change
 * as the JobScheduler is not available.
 *
 * This value defaults to true on Android O+ and false on devices with older OS versions.
 * Accepting the default value of false is recommended on Android N and earlier because
 * otherwise beacon scans may be run only once every 15 minutes in the background, and no low
 * power scans may be performed between scanning cycles.
 *
 * Setting this value to false will disable ScanJobs when the app is run on Android 8+, which
 * can prohibit delivery of callbacks when the app is in the background unless the scanning
 * process is running in a foreground service.
 *
 * This method may only be called if bind() has not yet been called, otherwise an
 * `IllegalStateException` is thown.
 *
 * @param enabled
 */

public void setEnableScheduledScanJobs(boolean enabled) {
    if (isAnyConsumerBound()) {
        LogManager.e(TAG, "ScanJob may not be configured because a consumer is" +
                " already bound.");
        throw new IllegalStateException("Method must be called before calling bind()");
    }
    if (enabled && android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        LogManager.e(TAG, "ScanJob may not be configured because JobScheduler is not" +
                " availble prior to Android 5.0");
        return;
    }
    if (!enabled && android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        LogManager.w(TAG, "Disabling ScanJobs on Android 8+ may disable delivery of "+
                "beacon callbacks in the background unless a foreground service is active.");
    }
    if(!enabled && android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        ScanJobScheduler.getInstance().cancelSchedule(mContext);
    }
    mScheduledScanJobsEnabled = enabled;
}
 
Example 4
/**
 * Creates a new beacon transmitter capable of transmitting beacons with the format
 * specified in the BeaconParser and with the data fields specified in the Beacon object
 * @param context
 * @param parser specifies the format of the beacon transmission
 */
public BeaconTransmitter(Context context, BeaconParser parser) {
    mBeaconParser = parser;
    BluetoothManager bluetoothManager =
            (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
    if (bluetoothManager != null) {
        mBluetoothAdapter = bluetoothManager.getAdapter();
        mBluetoothLeAdvertiser = mBluetoothAdapter.getBluetoothLeAdvertiser();
        LogManager.d(TAG, "new BeaconTransmitter constructed.  mbluetoothLeAdvertiser is %s",
                mBluetoothLeAdvertiser);
    }
    else {
        LogManager.e(TAG, "Failed to get BluetoothManager");
    }
}
 
Example 5
/**
 * Used to disable additional bootstrap callbacks after the first is received.  Unless this is called,
 * your application will be get additional calls as the supplied regions are entered or exited.
 */
public void disable() {
    if (disabled) {
        return;
    }
    disabled = true;
    try {
        for (Region region : regions) {
            beaconManager.stopMonitoringBeaconsInRegion(region);
        }
    } catch (RemoteException e) {
        LogManager.e(e, TAG, "Can't stop bootstrap regions");
    }
    beaconManager.unbind(beaconConsumer);
}
 
Example 6
/**
 * Add a new region
 *
 * @param region
 */
public void addRegion(Region region) {
    if (!regions.contains(region)) {
        if (serviceConnected) {
            try {
                beaconManager.startMonitoringBeaconsInRegion(region);
            } catch (RemoteException e) {
                LogManager.e(e, TAG, "Can't add bootstrap region");
            }
        } else {
            LogManager.w(TAG, "Adding a region: service not yet Connected");
        }
        regions.add(region);
    }
}
 
Example 7
/**
 * Remove a given region
 *
 * @param region
 */
public void removeRegion(Region region) {
    if (regions.contains(region)) {
        if (serviceConnected) {
            try {
                beaconManager.stopMonitoringBeaconsInRegion(region);
            } catch (RemoteException e) {
                LogManager.e(e, TAG, "Can't stop bootstrap region");
            }
        } else {
            LogManager.w(TAG, "Removing a region: service not yet Connected");
        }
        regions.remove(region);
    }
}
 
Example 8
/**
 * Method reserved for system use
 */
@Override
public void onBeaconServiceConnect() {
    LogManager.d(TAG, "Activating background region monitoring");
    beaconManager.addMonitorNotifier(monitorNotifier);
    serviceConnected = true;
    try {
        for (Region region : regions) {
            LogManager.d(TAG, "Background region monitoring activated for region %s", region);
            beaconManager.startMonitoringBeaconsInRegion(region);
        }
    } catch (RemoteException e) {
        LogManager.e(e, TAG, "Can't set up bootstrap regions");
    }
}
 
Example 9
private RssiFilter getFilter() {
    if (mFilter == null) {
        //set RSSI filter
        try {
        Constructor cons = BeaconManager.getRssiFilterImplClass().getConstructors()[0];
            mFilter = (RssiFilter)cons.newInstance();
        } catch (Exception e) {
            LogManager.e(TAG, "Could not construct RssiFilterImplClass %s", BeaconManager.getRssiFilterImplClass().getName());
        }
    }
    return mFilter;
}
 
Example 10
Source Project: android-beacon-library   File: Beacon.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Estimate the distance to the beacon using the DistanceCalculator set on this class.  If no
 * DistanceCalculator has been set, return -1 as the distance.
 * @see org.altbeacon.beacon.distance.DistanceCalculator
 *
 * @param txPower
 * @param bestRssiAvailable
 * @return
 */
protected static Double calculateDistance(int txPower, double bestRssiAvailable) {
    if (Beacon.getDistanceCalculator() != null) {
        return Beacon.getDistanceCalculator().calculateDistance(txPower, bestRssiAvailable);
    }
    else {
        LogManager.e(TAG, "Distance calculator not set.  Distance will bet set to -1");
        return -1.0;
    }
}
 
Example 11
private void loadDefaultModelMap() {
    try {
        buildModelMap(stringFromFilePath(CONFIG_FILE));
    }
    catch (Exception e) {
        mModelMap = new HashMap<AndroidModel, DistanceCalculator>();
        LogManager.e(e, TAG, "Cannot build model distance calculations");
    }
}
 
Example 12
protected void syncSettingsToService() {
    if (mScheduledScanJobsEnabled) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            ScanJobScheduler.getInstance().applySettingsToScheduledJob(mContext, this);
        }
        return;
    }
    try {
        applyChangesToServices(BeaconService.MSG_SYNC_SETTINGS, null);
    } catch (RemoteException e) {
        LogManager.e(TAG, "Failed to sync settings to service", e);
    }
}
 
Example 13
/**
 * Starts this beacon advertising
 */
public void startAdvertising() {
    if (mBeacon == null) {
        throw new NullPointerException("Beacon cannot be null.  Set beacon before starting advertising");
    }
    int manufacturerCode = mBeacon.getManufacturer();
    int serviceUuid = -1;
    if (mBeaconParser.getServiceUuid() != null) {
        serviceUuid = mBeaconParser.getServiceUuid().intValue();
    }

    if (mBeaconParser == null) {
        throw new NullPointerException("You must supply a BeaconParser instance to BeaconTransmitter.");
    }

    byte[] advertisingBytes = mBeaconParser.getBeaconAdvertisementData(mBeacon);
    String byteString = "";
    for (int i= 0; i < advertisingBytes.length; i++) {
        byteString += String.format("%02X", advertisingBytes[i]);
        byteString += " ";
    }
    LogManager.d(TAG, "Starting advertising with ID1: %s ID2: %s ID3: %s and data: %s of size "
                    + "%s", mBeacon.getId1(),
                    mBeacon.getIdentifiers().size() > 1 ? mBeacon.getId2() : "",
                    mBeacon.getIdentifiers().size() > 2 ? mBeacon.getId3() : "", byteString,
            advertisingBytes.length);

    try{
        AdvertiseData.Builder dataBuilder = new AdvertiseData.Builder();
        if (serviceUuid > 0) {
            byte[] serviceUuidBytes = new byte[] {
                    (byte) (serviceUuid & 0xff),
                    (byte) ((serviceUuid >> 8) & 0xff)};
            ParcelUuid parcelUuid = parseUuidFrom(serviceUuidBytes);
            dataBuilder.addServiceData(parcelUuid, advertisingBytes);
            dataBuilder.addServiceUuid(parcelUuid);
            dataBuilder.setIncludeTxPowerLevel(false);
            dataBuilder.setIncludeDeviceName(false);

        } else {
            dataBuilder.addManufacturerData(manufacturerCode, advertisingBytes);
        }

        AdvertiseSettings.Builder settingsBuilder = new AdvertiseSettings.Builder();

        settingsBuilder.setAdvertiseMode(mAdvertiseMode);
        settingsBuilder.setTxPowerLevel(mAdvertiseTxPowerLevel);
        settingsBuilder.setConnectable(mConnectable);

        mBluetoothLeAdvertiser.startAdvertising(settingsBuilder.build(), dataBuilder.build(), getAdvertiseCallback());
        LogManager.d(TAG, "Started advertisement with callback: %s", getAdvertiseCallback());

    } catch (Exception e){
        LogManager.e(e, TAG, "Cannot start advertising due to exception");
    }
}
 
Example 14
/**
 * This method notifies the beacon service that the application is either moving to background
 * mode or foreground mode.  When in background mode, BluetoothLE scans to look for beacons are
 * executed less frequently in order to save battery life. The specific scan rates for
 * background and foreground operation are set by the defaults below, but may be customized.
 * When ranging in the background, the time between updates will be much less frequent than in
 * the foreground.  Updates will come every time interval equal to the sum total of the
 * BackgroundScanPeriod and the BackgroundBetweenScanPeriod.
 *
 * @param backgroundMode true indicates the app is in the background
 * @see #DEFAULT_FOREGROUND_SCAN_PERIOD
 * @see #DEFAULT_FOREGROUND_BETWEEN_SCAN_PERIOD;
 * @see #DEFAULT_BACKGROUND_SCAN_PERIOD;
 * @see #DEFAULT_BACKGROUND_BETWEEN_SCAN_PERIOD;
 * @see #setForegroundScanPeriod(long p)
 * @see #setForegroundBetweenScanPeriod(long p)
 * @see #setBackgroundScanPeriod(long p)
 * @see #setBackgroundBetweenScanPeriod(long p)
 */
public void setBackgroundMode(boolean backgroundMode) {
    if (!isBleAvailableOrSimulated()) {
        LogManager.w(TAG, "Method invocation will be ignored.");
        return;
    }
    mBackgroundModeUninitialized = false;
    if (backgroundMode != mBackgroundMode) {
        mBackgroundMode = backgroundMode;
        try {
            this.updateScanPeriods();
        } catch (RemoteException e) {
            LogManager.e(TAG, "Cannot contact service to set scan periods");
        }
    }
}
 
Example 15
public void onServiceDisconnected(ComponentName className) {
    LogManager.e(TAG, "onServiceDisconnected");
    serviceMessenger = null;
}