Java Question Connecting to PTV navigator

JDS

Active Member
Licensed User
Longtime User
We are trying to connect to an application called PTV navigator.
That application works with an remote interface which is a service to interact with PTV navigator.

There are three seps to the navigation service.
1. Create a service connection
2. Bind to the service
3. Receive/send messages from/to the service

B4X:
public  void BindToPTV() {
     
      /*The package name of the service is always "com.ptvag.navigation.ri.RIService", the componentName may be different*/
      String componentName = "com.ptvag.navigator.app";
      /*String componentName = "com.ptvag.navigatortruckplay.app";
      String componentName = "com.ptvag.navigatorfleetplay.app";*/
      String serviceName = "com.ptvag.navigation.ri.RIService"; 
      Intent i = new Intent();
     i.setClassName(componentName, serviceName);
     boolean rc = getApplicationContext().bindService(i, serviceConnection, Context.BIND_AUTO_CREATE);
   }


At the moment we can't get passed step 2. We've compiled the java-code to an library without any problem. But when we call BindToPTV the B4A-applications stops working. The line "getApplicationContext().bindservice...." generates an error:

java.lang.NullPointerException
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:109)
at jds.b4a.ptv.PTV.BindToNav(PTV.java:152)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:680)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:308)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:238)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:121)
at b4a.example.main.afterFirstLayout(main.java:98)
at b4a.example.main.access$100(main.java:16)
at b4a.example.main$WaitForLayout.run(main.java:76)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:212)
at android.app.ActivityThread.main(ActivityThread.java:5135)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:877)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
at dalvik.system.NativeStart.main(Native Method)


Any thoughts?
 

JDS

Active Member
Licensed User
Longtime User
Erel,

It used to be:

B4X:
public class PTV extends activity {

I changed it to:

B4X:
public class PTV {

At that moment the lines give some issues.

B4X:
getApplicationContext().bindService(i,serviceConnection, Context.BIND_AUTO_CREATE);/*It says:"The method getApplicationContext() is undefined for the type PTV".*/
/* or the line*/
anywheresoftware.b4a.BA.applicationContext(i,serviceConnection,Context.BIND_AUTO_CREATE);/*It says:"The method getApplicationContext() is undefined for the type BA".*/

Any thoughts about the above?
 

JDS

Active Member
Licensed User
Longtime User
Thanks Erel, I overlooked that one.

I changed a few things but I can't get it to work.

I call the public void addstation. The first thing it should do is to bind to the service (which never happens).

B4X:
public void AddStation(String Name, String x, String y)
   {
      BindToPTV();
 }

@return 
    */
   private void BindToPTV() {
     
    //  final String apps[] = getServiceApps();
      String componentName = "com.ptvag.navigation.app";
      Common.Log(componentName);
      String serviceName = "com.ptvag.navigation.ri.RIService"; 
      Intent i = new Intent();
      i.setClassName(componentName, serviceName);
      anywheresoftware.b4a.BA.applicationContext.bindService(i,serviceConnection,Context.BIND_AUTO_CREATE);
      if (isBound()) Common.Log("Connected");
   }

You can see that there is a line Common.Log("Connected"), that one gets skipped. Bound is false.
Below you find the serviceconnection. If I put an logging in there it isn't executed.

Any thoughts? The library is compiling without errors.

B4X:
/** Class for interacting with the main interface of the service. */
   protected ServiceConnection serviceConnection = new ServiceConnection()
   {
      public void onServiceConnected(ComponentName className, IBinder service)
      {
         // This is called when the connection with the service has been
         // established, giving us the object we can use to
         // interact with the service. We are communicating with the
         // service using a Messenger, so here we get a client-side
         // representation of that from the raw IBinder object.
         foreignService = new Messenger(service);
         bound = true;
      }

      public void onServiceDisconnected(ComponentName className)
      {
         // This is called when the connection with the service has been
         // unexpectedly disconnected -- that is, its process crashed.
         foreignService = null;
         bound = false;
         
      }
   };
 

JDS

Active Member
Licensed User
Longtime User
Yes but that is a complete program with buttons.
I only need the application to start and drive to a certain point (x y coordinates)
 
Top