diff --git a/build.gradle b/build.gradle index faa598b..016e4ef 100644 --- a/build.gradle +++ b/build.gradle @@ -2,5 +2,6 @@ dependencies { // Add the module's library dependencies here. // See: https://developer.android.com/studio/build/dependencies - implementation 'org.altbeacon:android-beacon-library:2+' +// implementation 'org.altbeacon:android-beacon-library:2.19.6' + implementation 'org.altbeacon:android-beacon-library:2.19.5' } diff --git a/dist/com.liferay.beacons-android-2.0.0.zip b/dist/com.liferay.beacons-android-2.0.0.zip deleted file mode 100644 index 8d37306..0000000 Binary files a/dist/com.liferay.beacons-android-2.0.0.zip and /dev/null differ diff --git a/dist/com.liferay.beacons-android-4.0.1.zip b/dist/com.liferay.beacons-android-4.0.1.zip deleted file mode 100644 index 9eda289..0000000 Binary files a/dist/com.liferay.beacons-android-4.0.1.zip and /dev/null differ diff --git a/dist/com.liferay.beacons-android-4.1.0.zip b/dist/com.liferay.beacons-android-4.1.0.zip new file mode 100644 index 0000000..66e325c Binary files /dev/null and b/dist/com.liferay.beacons-android-4.1.0.zip differ diff --git a/documentation/index.md b/documentation/index.md index 91430e6..d60cd2e 100644 --- a/documentation/index.md +++ b/documentation/index.md @@ -8,13 +8,15 @@ A Titanium module to interact with iBeacons in Titanium projects that support An Place the ZIP file into your project's root directory, and declare the module and required android permissions in your `tiapp.xml` file (or in your custom `platform/android/AndroidManifest.xml` file if you are using that): -``` +```xml ... + - ` to your tiapp.xml `` block and into your permission request if you use Android 12+ + +There are two events that will be fired: `success` or `error` (with errorCode). + ### Foreground vs. Background It is is a good idea for apps to reduce their power consumption when placed in the background by @@ -283,7 +308,7 @@ however **this module DOES NOT DETECT when your app is sent to the background or You must manually detect foreground/background events and call the appropriate APIs on this module to tell it that it is now in the background and should use the background scan periods. Check out [Ben Bahrenburg's excellent Android Tools](https://github.com/benbahrenburg/benCoding.Android.Tools) for a super-easy way to auto-detect this. Here's an example: -``` +```js var androidPlatformTools = require('bencoding.android.tools').createPlatform(); var isForeground = androidPlatformTools.isInForeground(); console.log("Am I currently in the foreground? " + isForeground); @@ -291,7 +316,7 @@ console.log("Am I currently in the foreground? " + isForeground); You can call this repeatedly (e.g. every 5 seconds) using `setInterval()` and when foreground vs. background is detected, call `TiBeacons.setBackgroundMode()`. At least that's what I do. To configure the scan periods for foreground and background: -``` +```js var TiBeacons = require('com.liferay.beacons'); TiBeacons.setScanPeriods({ foregroundScanPeriod: 1000, @@ -312,7 +337,7 @@ and the defaults. Here is a simple `app.js` application that you can use to see if things are working. You may need to modify it a bit to align with your specific beacon UUID. -``` +```js // sample Titanium app.js app to test that things are working, // this assumes your hardware supports BLE and it's switched on. // you can use checkAvailability() to see if it's supported, but diff --git a/manifest b/manifest index 57ca3ff..a130ffb 100644 --- a/manifest +++ b/manifest @@ -15,7 +15,7 @@ # limitations under the License. # -version: 4.0.1 +version: 4.1.0 apiversion: 4 architectures: arm64-v8a armeabi-v7a x86 x86_64 description: Enables Titanium apps supporting android to interact with iBeacons diff --git a/src/com/liferay/beacons/LiferayBeaconsModule.java b/src/com/liferay/beacons/LiferayBeaconsModule.java index c1d4c57..1554e9d 100644 --- a/src/com/liferay/beacons/LiferayBeaconsModule.java +++ b/src/com/liferay/beacons/LiferayBeaconsModule.java @@ -19,12 +19,19 @@ package com.liferay.beacons; +import android.annotation.SuppressLint; import android.app.Activity; +import android.bluetooth.le.AdvertiseCallback; +import android.bluetooth.le.AdvertiseSettings; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.RemoteException; import com.radiusnetworks.ibeacon.*; + +import org.altbeacon.beacon.Beacon; +import org.altbeacon.beacon.BeaconParser; +import org.altbeacon.beacon.BeaconTransmitter; import org.appcelerator.kroll.KrollDict; import org.appcelerator.kroll.KrollModule; import org.appcelerator.kroll.annotations.Kroll; @@ -33,12 +40,13 @@ import org.appcelerator.titanium.util.TiConvert; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.List; /** - * Events: enteredRegion, exitedRegion, determinedRegionState, beaconProximity + * Events: enteredRegion, exitedRegion, determinedRegionState, beaconProximity, success, error */ @Kroll.module(name="LiferayBeacons", id="com.liferay.beacons") public class LiferayBeaconsModule extends KrollModule implements IBeaconConsumer @@ -51,6 +59,7 @@ public class LiferayBeaconsModule extends KrollModule implements IBeaconConsumer private boolean autoRange = true; private boolean ready = false; + private BeaconTransmitter beaconTransmitter; public LiferayBeaconsModule() { super(); @@ -504,5 +513,51 @@ public boolean bindService(Intent intent, ServiceConnection serviceConnection, i Log.d(TAG, "bindService"); return super.getActivity().bindService(intent, serviceConnection, i); } + + @SuppressLint("NewApi") + @Kroll.method + public void startAdvertise(KrollDict kd) { + String id1 = TiConvert.toString(kd.get("id1"), ""); + String id2 = TiConvert.toString(kd.get("id2"), ""); + String id3 = TiConvert.toString(kd.get("id3"), ""); + String beaconLayout = TiConvert.toString(kd.get("beaconLayout"), ""); + int manu = TiConvert.toInt(kd.get("manufacturer"), 0); + int txPower = TiConvert.toInt(kd.get("txPower"), 0); + + Beacon beacon = new Beacon.Builder() + .setId1(id1) + .setId2(id2) + .setId3(id3) + .setManufacturer(manu) + .setTxPower(txPower) + .setDataFields(Arrays.asList(new Long[] {0l})) + .build(); + BeaconParser beaconParser = new BeaconParser().setBeaconLayout(beaconLayout); + beaconTransmitter = new BeaconTransmitter(getApplicationContext(), beaconParser); + beaconTransmitter.startAdvertising(beacon, new AdvertiseCallback() { + @Override + public void onStartSuccess(AdvertiseSettings settingsInEffect) { + super.onStartSuccess(settingsInEffect); + KrollDict kdSuccess = new KrollDict(); + fireEvent("success", kdSuccess); + } + + @Override + public void onStartFailure(int errorCode) { + super.onStartFailure(errorCode); + KrollDict kdError = new KrollDict(); + kdError.put("errorCode", errorCode); + fireEvent("error", kdError); + } + }); + } + + @Kroll.method + public void stopAdvertise() { + if (beaconTransmitter != null) { + beaconTransmitter.stopAdvertising(); + beaconTransmitter = null; + } + } }