AltBeaconLibrary in service and seperate process. Part 2 - android

Help is needed. Thanks to David for the last answer to the question -AltBeacon service in separate android process.
I'm trying to implement on the Xamarin Android - AltBeacon Library in the service and in a separate process. (A separate process is needed to ensure that scanning works constantly, round the clock.
What would the android not cut the scanning after a certain time, after the phone goes into sleep mode).
1. What did I do after David's instructions -
-added attributes in the service definition:
 
[Service (Name = SERVICE_NAME, Process = ": myProcess", Enabled = true, Exported = false, IsolatedProcess = false)]
installed a new version of the library - AltBeacon 2.11. To date, Xamarin does not have version 2.11. The latest version is 2.7
 I downloaded the AltBeacon v2.11 native library and wrapped it with Managed Callable Wrappers (MCW). [(https://developer.xamarin.com/guides/android/advanced_topics/binding-a-java-library/)][1].
 As a result, I got a full library on Xamarin.
 - Connected this library to the project. For testing, I removed the Process: myProcess attribute and ran the application.
  In one process, everything works well. The service works and finds the bicons. Perfectly!
  But - 
 As soon as I started the service in another process I stopped getting the bicons.
 After the other method that triggers this is OnBeaconServiceConnect ().
public void OnBeaconServiceConnect()
{
for (int i = 0; i < guids.Length; i++)
{
var uuid = Identifier.Parse(guids[i]);
var region = new Region("R" + i, uuid, null, null);
beaconManager.StartRangingBeaconsInRegion(region);
}
}
It turns out that the method does not work in the Range Notifier object =(
A rough example of the implementation of the service:
public class myService:Service
{
private BeaconWorker beaconWorker;
public myService()
{
beaconWorker=new beaconWorker(DroidApplication.CurrentInstanceApp);
}
public void MainMethod()
{
var notification = ...Build();
StartForeground(s_id, notification);
while(true)
{
StartMainWork(guids)
}
}
public void StartMainWork(string guid)
{
beaconWorker.GetResult();
}
}
/////////////////////////////////////
public class BeaconWorker:IBeaconConsumer
{
List<Beacon> Beacons;
private Context context;
private RangeNotifier rangeNotifier;
private BeaconManager beaconManager;
//This is main configuring scanning
public BeaconWorker(Context context)
{
Context = context;
this.guids = ...;
rangeNotifier = new RangeNotifier();
BeaconManager.SetDebug(true);
beaconManager =
BeaconManager.GetInstanceForApplication(context);
beaconManager.SetForegroundBetweenScanPeriod(1000);
beaconManager.SetForegroundScanPeriod(1000);
beaconManager.SetBackgroundMode(false);
var beaconParser = new BeaconParser();
beaconParser.SetBeaconLayout("...");
beaconManager.BeaconParsers.Add(beaconParser);
beaconManager.SetRangeNotifier(rangeNotifier);
beaconManager.ApplySettings();
}
//Method that do bind
public void GetResult()
{
beaconManager.Bind(this);
Task.Wait(3000);
beaconManager.UnBind(this);
}
//Implement IBeaconConsumer BindService
public bool BindService(Intent p0, IServiceConnection p1, int p2)
{
return context.BindService(p0, p1, Bind.AutoCreate);
}
public void OnBeaconServiceConnect()
{
for (int i = 0; i < guids.Length; i++)
{
var uuid = Identifier.Parse(guids[i]);
var region = new Region("R" + i, uuid, null, null);
beaconManager.StartRangingBeaconsInRegion(region);
}
}
private class RangeNotifier
{
//THIS METHOD DOES NOT INVOKE =(
public void DidRangeBeaconsInRegion(ICollection<Beacon> beacons, Region region)
{
this.Beacons = beacons;
}
}
}
In the logs of the phone there is such information:
Time Device Name Type PID Tag Message
01-18 18:52:25.370 AGM A8 Warning 17056 BeaconManager Ranging/Monitoring may not be controlled from a separate BeaconScanner process. To remove this warning, please wrap this call in: if (beaconManager.isMainProcess())
Time Device Name Type PID Tag Message
01-18 18:52:25.370 AGM A8 Debug 17056 BeaconManager we have a connection to the service now
Time Device Name Type PID Tag Message
01-18 18:52:25.361 AGM A8 Debug 17056 BeaconManager consumer count is now: 1
Time Device Name Type PID Tag Message
01-18 18:52:25.363 AGM A8 Info 17024 BeaconService beaconService version 2.11 is starting up on the main process
I know about the verification David pointed out in the answer to IsMainProcess() method. But how can I call this check if all this happens in the service itself and not in the main application process?
  Tell me where I'm wrong. What else can I add?
  In the article https://github.com/AltBeacon/android-beacon-library/pull/479
  on how much I realized that it makes no sense to specify a RangeNotifier in a separate process. Is it so?
  I will be grateful for any help! Thank you!

Related

Android OnePlus6 can't detect iBeacon using AltBeacon library

I'm using the AltBeacon library for detecting iBeacons in my Android app. The code I have works on the following devices:
Xiaomi MI9 (Android 10.0)
Motorola Moto G4 (Android 6.0.1)
Huawei P Smart (Android 8.0)
Samsung Galaxy S8 (Android 9.0)
However, the same code doesn't work for a OnePlus 6 (Android 10, OxygenOS 10.3.2). It doesn't detect any beacons in my app. I tried to detect the beacons using an other app (Locate), that works. The creator of the AltBeacon library told me that Locate uses the AltBeacon library, so the beacons are detectable. This means my code setup is wrong. Can you help me by finding out what is wrong with my setup?
I checked (e.g.) this answer, although it didn't fix my problem. I turned debugging on for the BeaconManager but nothing interesting came out of that (an example at the bottom of this question).
In the ViewModel I call the MyStateManager. It contains a List regionsInRange, which contains beacons that are in range. I left out some code because I think it is irrelevant. If you feel like I left out too much, I will add it.
public class MyStateManager implements BootstrapNotifier {
private static final MyStateManager instance = new MyStateManager();
private final MyBeaconHelper myBeaconHelper;
// ViewModel accessess this List to retrieve the beacons that are found.
public final List<Region> regionsInRange = new ArrayList<>();
private PresenceRegistrationStateManager() {
presenceRegistrationBeaconHelper = new PresenceRegistrationBeaconHelper(this);
updateScanningRegions();
}
#Override
public Context getApplicationContext() {
return MyApplication.getAppContext();
}
#Override
public void didEnterRegion(Region region) {
//Empty method
}
#Override
public void didExitRegion(Region region) {
//Empty method
}
#Override
public void didDetermineStateForRegion(int status, Region region) {
if (status == OUTSIDE) {
regionsInRange.remove(region);
} else {
if (!regionsInRange.contains(region)) {
regionsInRange.add(region);
}
}
updateState();
}
public static MyStateManager getInstance() {
return instance;
}
public void updateState() {
// Own implementation here
}
private void updateScanningRegions() {
// add all the regions here
}
}
In addition, this is the MyBeaconHelper:
public class MyBeaconHelper implements BeaconConsumer, Serializable {
private transient final RegionBootstrap regionBootstrap;
private List<Region> scanRegions = new ArrayList<>();
public MyBeaconHelper(BootstrapNotifier bootstrapNotifier) {
BeaconManager beaconManager = BeaconManager.getInstanceForApplication(getApplicationContext());
beaconManager.getBeaconParsers().clear();
beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
LogManager.setVerboseLoggingEnabled(true);
beaconManager.bind(this);
regionBootstrap = new RegionBootstrap(bootstrapNotifier, new ArrayList<>());
}
#Override
public void onBeaconServiceConnect() {
//Empty method
}
#Override
public Context getApplicationContext() {
return MyApplication.getAppContext();
}
#Override
public void unbindService(ServiceConnection serviceConnection) {
getApplicationContext().unbindService(serviceConnection);
}
#Override
public boolean bindService(Intent intent, ServiceConnection serviceConnection, int i) {
return getApplicationContext().bindService(intent, serviceConnection, i);
}
public void updateScanRegions(List<Region> newRegions) {
for (Region oldRegion : this.scanRegions) {
if (!newRegions.contains(oldRegion)) {
regionBootstrap.removeRegion(oldRegion);
}
}
for (Region newRegion : newRegions) {
if (!this.scanRegions.contains(newRegion)) {
regionBootstrap.addRegion(newRegion);
}
}
this.scanRegions = newRegions;
}
}
When I turned debugging on for the BeaconManager, it showed me this a lot of times:
2020-03-31 11:57:30.181 25259-25259/com.my.app D/CycledLeScanner: starting a new scan cycle
2020-03-31 11:57:30.181 25259-25259/com.my.app D/CycledLeScanner: We are already scanning and have been for 1134 millis
2020-03-31 11:57:30.181 25259-25259/com.my.app D/CycledLeScanner: Waiting to stop scan cycle for another 1100 milliseconds
2020-03-31 11:57:30.181 25259-25259/com.my.app D/CycledLeScanner: Scan started
2020-03-31 11:57:31.213 25259-25259/com.my.app D/CycledLeScanner: Waiting to stop scan cycle for another 69 milliseconds
2020-03-31 11:57:31.323 25259-25259/com.my.app D/CycledLeScanner: Done with scan cycle
It keeps printing these lines over and over again...
The log messages shown (these are for OnePlus, yes?) indicate that BLE scanning is started. Do you see any log lines showing hex bytes of the packets detected? If BLE scanning is actually functioning you should. You may want to compare the logs output by the other devices.
Are you certain proper location permission has been granted to your app on the OnePlus? You can check in Settings -> Apps - > You App -> Permissions. Also confirm Bluetooth is on and location is on for the global phone settings (but if Locate works on the same device, this should not be a problem .)
It is not clear if this is related, but the use of beaconManager.bind() at the same time as RegionBootstrap is unnecessary and may cause conflicts. The code appears to not use the BeaconConsumer interface that is called back by the bind method. I suggest you remove the bind call, the use of BeaconConsumer and remove all that interface's callback methods just to be sure.

Incomings Call with Android Sip stack in Embarcadero C++ builder

I'm trying to receive calls on my SIP application at Embarcadero with C++ builder
and I'm not able to get it. My situation is as follows:
I've made an Asterisk server, I've created several accounts to be able to do the
tests and I've downloaded the Zoiper application for both Windows and Android.
In my designed application, I'm able to make calls to those accounts registered
in Zoiper, although not through events, it seems that the listener does not
listen, and I've done it through the status changes in the call.
The Java code is like this:
SipAudioCall.Listener listener = new SipAudioCall.Listener() {
#Override
public void onCallEstablished(SipAudioCall call) {
call.startAudio();
call.setSpeakerMode(true);
call.toggleMute();
Log.d("on call established", "on call established");
}
#Override
public void onCallEnded(SipAudioCall call) {
finish();
}
};
In Embarcadero C++ builder I think it would be like this (it doesn't work)
Compiles and executes but the event never occurs:
//The Manifest counts as the necessary permissions for Android, Internet and Sip.
_di_JSipAudioCall_Listener audioCall_Listener;
_di_JSipSession_Listener sessionListener;
_di_JSipSession session;
_di_JSipManager;
_di_JSipAudioCall audioCall;
_di_JSipProfile profile;
_di_JString uri;
_di_JString uri_llamada;
void onCallEstablished2(SipAudioCall call);
//The process of profile creation and instantiation of SipManager are programmed
//and compiled and do not give any problem.
audioCall_listener = TJSipAudioCall_Listener::JavaClass->init();
audioCall_listener->onCallEstablished = onCallEstablished2;
sessionListener = TJSipSession_Listener::JavaClass->init();
session = manager->createSipSession(profile,sessionListener);
audioCall = manager->makeAudioCall(uri,uri_llamada,audioCall_listener,15);
void onCallEstablished2(SipAudioCall call)
{
audioCall->startAudio();
audioCall->setSpeakerMode(true);
}
The code made in Embarcadero C++ builder that works:
//The Manifest counts as the necessary permissions for Android, Internet and Sip.
_di_JSipAudioCall_Listener audioCall_Listener;
_di_JSipSession_Listener sessionListener;
_di_JSipSession session;
_di_JSipManager;
_di_JSipAudioCall audioCall;
_di_JSipProfile profile;
_di_JString uri;
_di_JString uri_llamada;
//The process of profile creation and instantiation of SipManager are programmed
//and compiled and do not give any problem.
audioCall_listener = TJSipAudioCall_Listener::JavaClass->init();
audioCall_listener->onCallEstablished = onCallEstablished2;
sessionListener = TJSipSession_Listener::JavaClass->init();
session = manager->createSipSession(profile,sessionListener);
audioCall = manager->makeAudioCall(uri,uri_llamada,audioCall_listener,15);
Timer1->Enabled = true;
void __fastcall TMainForm::Timer1Timer(TObject *Sender)
{
if (audioCall->getState() == 8)
{
audioCall->startAudio();
audioCall->setSpeakerMode(true);
}
if(audioCall->getState() == 0)
{
audioCall->endCall();
}
}
As for the Java code for receiving calls, I have found examples here
No ringing event on incoming calls
and here
Android Sip incoming Call using Service with Broadcast Receiver,
but they are all event based, which doesn't seem to work for me.
I have also tried to do the IncomingReceiver class, which extends from
BroadcastReceiver and at the Embarcadero gives me problems.
Class made in Embarcadero with C++ builder (not compiles):
class IncomingReceiver: public JBroadcastReceiver{
public:
__fastcall IncomingReceiver();
_di_JSipAudioCall incomingCall;
void onReceive(_di_JContext contexto, _di_JIntent intento);
void accept();
void showIncomingCallGui(_di_JIntent intento, _di_JContext contexto);
};
So, my questions are:
Why don't events work for me?
Can I receive calls without events?
If so, what would it be like without events?
What do I do if I can't get the IncomingReceiver class?
I found a page (in spanish): http://lfgonzalez.visiblogs.com/cbuilder-10-2-tokyo-jni-broadcastreceiver-android/ in which it is explained the use of BroadcastReceiver in Embarcadero C++ Builder. Maybe with this information you can get the events working in order to receive calls.

AltBeacon service in separate android process

I need help. There is an application for Xamarin Android. Inside it, a service is started that works with the AltBeacon library.
In this service, a thread is created, in which beacons are constantly being scanned. Service started as StartForeground(...).
The service should work constantly, so it was decided to run it in a separate process, because after a while the android system stops allocating
memory and service terminates.
If you run the application and the service in one process, everything works fine. Service works, beacons are scanned.
But as soon as I run it in a separate process (using the Process =: myProcess attribute), the scanning not works.
The DidRangeBeaconsInRegion method does not work for the IRangeNotifier implementation object.
It simply does not work, there are no exceptions.
Brief code base:
public class BeaconsWorker : Java.Lang.Object, IBeaconConsumer
{
private string[] guids;
private readonly Context context;
private readonly BeaconManager beaconManager;
private readonly RangeNotifier rangeNotifier;
private readonly List<BeaconEntry> beacons;
public Context ApplicationContext
{
get { return context.ApplicationContext; }
}
public BeaconsWorker(Context context, string[] guids, int scanTime)
{
...
this.context = context;
rangeNotifier = new RangeNotifier();
beaconManager = BeaconManager.GetInstanceForApplication(context);
beaconManager.SetForegroundBetweenScanPeriod(1000);
beaconManager.SetForegroundScanPeriod(1000);
beaconManager.SetBackgroundMode(false);
var beaconParser = new BeaconParser();
beaconParser.SetBeaconLayout("...");
beaconManager.BeaconParsers.Add(beaconParser);
rangeNotifier.DidRangeBeaconsInRegionComplete += OnBeaconsRanging;
beaconManager.SetRangeNotifier(rangeNotifier);
}
public bool BindService(Intent intent, IServiceConnection serviceConnection, [GeneratedEnum] Bind flags)
{
return context.BindService(intent, serviceConnection, flags);
}
public void OnBeaconServiceConnect()
{
foreach (var region in beaconManager.RangedRegions.ToList())
beaconManager.StopRangingBeaconsInRegion(region);
for (int i = 0; i < guids.Length; i++)
{
var uuid = Identifier.Parse(guids[i]);
var region = new Region("R" + i, uuid, null, null);
beaconManager.StartRangingBeaconsInRegion(region);
}
}
public void UnbindService(IServiceConnection serviceConnection)
{
context.UnbindService(serviceConnection);
}
public async Task<BeaconEntry> GetLocationResult()
{
beaconManager.Bind(this);
await Task.Delay(scanTime * 1000);
beaconManager.Unbind(this);
...
return result;
}
private void OnBeaconsRanging(object sender, RangeEventArgs e)
{
lock (beacons)
foreach (var item in e.Beacons)
{
var beacon = new BeaconEntry()
{
BeaconGUID = item.Id1.ToString(),
BeaconMajor = Int32.Parse(item.Id2.ToString()),
BeaconMinor = Int32.Parse(item.Id3.ToString())
};
beacons.Add(beacon);
}
}
private class RangeEventArgs : EventArgs
{
public Region Region { get; set; }
public ICollection<Beacon> Beacons { get; set; }
}
private class RangeNotifier : Java.Lang.Object, IRangeNotifier
{
public event EventHandler<RangeEventArgs> DidRangeBeaconsInRegionComplete;
public void DidRangeBeaconsInRegion(ICollection<Beacon> beacons, Region region)
{
OnDidRangeBeaconsInRegion(beacons, region);
}
private void OnDidRangeBeaconsInRegion(ICollection<Beacon> beacons, Region region)
{
DidRangeBeaconsInRegionComplete?.Invoke(this, new RangeEventArgs { Beacons = beacons, Region = region });
}
}
It is possible to set up the Android Beacon Library to run in a separate process. You can read the basic configuration instructions here:
https://github.com/AltBeacon/android-beacon-library/pull/479
This multi-process setup was successfully tested with library version 2.11. Version 2.12, however, included significant rework to support Android 8, and I have not tested multi-process support with versions 2.12+, so use those versions with caution. Your best bet is to use version 2.11.
The instructions linked above are written for Android apps built using standard Java or Kotlin development toolset with Android Studio or Gradle. Clearly modifications are needed to make this work with Xamarin. Since I am not a Xamarin expert, it's hard for me to help more.

Using Android IBeacon Library on Service

I´m using the Android IBeacon Library for a Project. I need to create a Service that starts ranging for beacons in the background and notify the user when it finds one (the nearest one). I have searched a lot and coded based on many examples I have found, but still it doesn´t work. Using logs I found that the IBeaconManager doesn't bind, so onIBeaconServiceConnect never gets called. I have already tried some solutions I found here but none of them has been useful. I would really appreciate if someone could help me solving this problem. I post some of my code here
public class RangingService extends Service implements IBeaconConsumer
{
private IBeaconManager beaconManager;
#Override
public void onCreate()
{
super.onCreate();
beaconManager = IBeaconManager.getInstanceForApplication(this);
Log.d("RangingService","Created beaconManager instance");
beaconManager.setBackgroundBetweenScanPeriod(120000);
beaconManager.setBackgroundScanPeriod(30000);
beaconManager.bind(this);
if(beaconManager.isBound(this))
{
Log.d("RangingService","Beacon manager bound");
}
else
{
Log.d("RangingService","Beacon manager not bound");
}
//Show the service has started
notify("RangingService created", "RangingService has started");
}
#Override
public void onDestroy()
{
super.onDestroy();
beaconManager.unBind(this);
}
#Override
public void onIBeaconServiceConnect()
{
Log.d("RangingService", "Entering onIBeaconServiceConnect");
beaconManager.setRangeNotifier(new RangeNotifier() {
#Override
public void didRangeBeaconsInRegion(Collection<IBeacon> beacons, Region region)
{
if(beacons.size() > 0)
{
IBeacon nearestBeacon = beacons.iterator().next();
for(IBeacon b : beacons)
{
if(nearestBeacon.getProximity() == IBeacon.PROXIMITY_UNKNOWN)
{
nearestBeacon = b;
}
else
{
if(b.getProximity() != IBeacon.PROXIMITY_UNKNOWN)
{
if(b.getAccuracy() < nearestBeacon.getAccuracy())
{
nearestBeacon = b;
}
}
}
}
Log.d("RangingService","Nearest Beacon Found "+nearestBeacon.getMajor()+";"+nearestBeacon.getMinor());
notify("Beacon read","Major: "+nearestBeacon.getMajor()+"; Minor: "+nearestBeacon.getMinor());
}
else
{
Log.d("RangingService","No beacons");
}
}
});
try
{
Log.d("RangingService", "Entering startRangingBeacons");
beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
}
catch(RemoteException e)
{
notificar("Error", e.getMessage());
Log.e("RangingService", "Error while starting scanning: "+e.getMessage());
}
}
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
Log.d("RangingService", "Entering onBind");
return null;
}
The Service is already in my manifest also. Thanks for your help.
A couple of points:
The code indicates you are using a 0.x version of the Android iBeacon Library. If you are starting a new project, I would strongly recommend you use the Android Beacon Library 2.0, as the earlier library is no longer actively maintained, and it is now hard to find documentation for it.
Regardless of the library version you are using, you should be able to get a callback to the onBeaconServiceConnect() method after you bind to the BeaconManager. The fact that you don't get this callback probably indicates that the BeaconService is not starting up properly.
The most likely reason that the BeaconService is not starting up properly is because it is not properly declared in the manifest. If you are using Eclipse, you must edit your project.properties file and add the line: manifestmerger.enabled=true. If you are using AndroidStudio, this is not necessary. If you are using IntelliJ, you may have to declare the service manually.
You can verify if the manifest has the proper entries by looking at the generated manifest file in bin/AndroidManifest.xml, and verifying it has an entry for the BeaconService.
I had faced similar issues. Got resolved by doing following things
In eclipse project.properties added manifestmerger.enabled=true
restarted the eclipse
uninstalled other beacon apps in my android phone and restarted the phone

ActivityWatcher & ProcessObserver

I would like to know the purpose of foloowing two files:
frameworks/base/core/java/android/app/IActivityWatcher.aidl
[description: Callback interface to watch the user's traversal through activities.]
frameworks/base/core/java/android/app/IProcessObserver.aidl
[no description]
I am trying to build an app wherein user can decide which apps can be run during particular period of time (say, from 10am till 4pm).
Is there any way where my app will get notified if one the apps specified by the user starts? This way my app can send kill command (I am assuming that root access is available.)
It seems that IActivityWatcher has been removed beginning with JellyBean, in order to monitor which Activity is running foreground, you can use IProcessObserver as following:
mActivityManagerNative = ActivityManagerNative.getDefault();
if (mActivityManagerNative != null) {
try {
mActivityManagerNative.registerProcessObserver(mProcessObserver);
} catch (RemoteException e) {
Log.e("TAG", "onCreate() RemoteException!");
}
}
private IProcessObserver.Stub mProcessObserver = new IProcessObserver.Stub() {
#Override
public void onForegroundActivitiesChanged(int pid, int uid, boolean foregroundActivities) {
doWhatUWantHere();
}
#Override
public void onImportanceChanged(int pid, int uid, int importance) {
}
#Override
public void onProcessDied(int pid, int uid) {
}
};
P.S.
You can use following code snippets to get the package name of foreground running Activity:
private String getForegroundPackage() {
ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
List<RecentTaskInfo> taskInfo = am.getRecentTasks(1,
ActivityManager.RECENT_IGNORE_UNAVAILABLE);
return taskInfo.isEmpty()
? null : taskInfo.get(0).baseIntent.getComponent().getPackageName();
}

Categories

Resources