I am trying to setup two android devices to communicate with each other through wifi. Some of the links I have gone through suggest alljoyn sdk in order to accomplish this.
There is an sdk download but there is no documentation for how to setup environment.
Here is how to set up an AllJoyn SDK development environment with android studio:
Download the SDK from this page. Go for Android Core SDK - release (or debug).
Create a new blank android project.
Create directory <project>/app/src/main/jniLibs and <project>/app/src/main/jniLibs/armeabi.
From alljoyn-15.09.00-rel/java/jar copy alljoyn.jar and from alljoyn-15.09.00-rel/java/lib copy liballjoyn_java.so. The directory to copy from might differ depending on the current version and your release/debug choice.
Put alljoyn.jar in /jniLibs and put liballjoyn_java.so in /jniLibs/armeabi. Should look like this
Right click project -> Open Module Settings -> app -> Dependencies.
With the green [+] button, add a file dependency.
Navigate to <project>/app/src/main/jniLibs/alljoyn.jar and select that jar.
This will add a line in your gradle (compile files('src/main/jniLibs/alljoyn.jar')) that will allow for code completion etc.
In the file where you want to use alljoyn code, include this snippet
/* Load the native alljoyn_java library. */
static {
System.loadLibrary("alljoyn_java");
}
for example:
public class MainActivity extends AppCompatActivity {
/* Load the native alljoyn_java library. */
static {
System.loadLibrary("alljoyn_java");
}
#Override
public void onCreate(Bundle savedInstanceState) {
...
}
}
You can now use the alljoyn SDK. Import classes with
import org.alljoyn.bus.BusAttachment;
import org.alljoyn.bus.BusException;
import org.alljoyn.bus.BusListener;
etc.
If you're more of an eclipse guy, check this official documentation page on how to setup an eclipse environment.
Related
I am trying to connect postek EM 210 Bluetooth printer using my react native mobile app. I have SDK/ .jar file but need help for implementing its methods in react native
Postek has provided android SDK for use in android studio. Since my project is in react-native, I have included the .jar file in my project but have no clue how to use this file in react-native and access methods and functions that are available inside .jar file.
package com.postek.coyote.cdfptkbluetooth;
..
..
import com.postek.cdf.CDFPTKAndroid;
import com.postek.cdf.CDFPTKAndroidImpl;
public class BluetoothActivity extends AppCompatActivity implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {
...
public static CDFPTKAndroid cdf = null;
...
}
So how do I convert these lines to react-native -
import com.postek.cdf.CDFPTKAndroid;
import com.postek.cdf.CDFPTKAndroidImpl;
..
public static CDFPTKAndroid cdf = null;
Alternatively, I want develop same app (using same SDK) with Bluetooth as here but in react-native.
Side note - We will even make react-native version of this app available on git for other developer who may be looking for this.
I try to import jna.jar into my project since JNA is a very useful tool to call Native library which is base on JNI.
OS: Windows 10
IDE: Android Studio 1.5.1
JDK: 1.8.0_73
NDK: r10e
What I have done
(AS = Android Studio)
Create a new project by AS with API18.
Download jna.jar from their GitHub.
https://github.com/java-native-access/jna
copy jna.jar into project folder.
JNATest\app\libs\jna.jar
In AS, right-click on the icon of jna.jar, choose Add as Library
Wait for few seconds, check the File->Project Structure->app->Dependencies. We do have the jna.jar. (Same as app\build.gradle)
build gradle
Implement JAVA code about JNA in MainActivity.java
Run app on real device Sony Z3 (arm)
Crash by CLibrary.Instance.printf("Hello, JNA");
Error Message on Android Monitor
E/AndroidRuntime: FATAL EXCEPTION: main
Process: i3d.jnatest, PID: 1068
java.lang.UnsatisfiedLinkError: Native library (com/sun/jna/android-arm/libjnidispatch.so) not found in resource path (.)
at com.sun.jna.Native.loadNativeDispatchLibraryFromClasspath(Native.java:866)
at com.sun.jna.Native.loadNativeDispatchLibrary(Native.java:826)
at com.sun.jna.Native.<clinit>(Native.java:140)
..
... so on
Java code
package i3d.jnatest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.sun.jna.Library;
import com.sun.jna.Native;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CLibrary.Instance.printf("Hello, JNA");
}
public interface CLibrary extends Library
{
CLibrary Instance = (CLibrary) Native.loadLibrary("msvcrt", CLibrary.class);
void printf(String format, Object... args);
}
}
Question
According to error message, I miss /android-arm/libjnidispatch.so in runtime.
Did I put the wrong place for jna.jar?
How should I get and use /android-arm/libjnidispatch.so?
I am a newbie about Android Studio, so maybe misunderstanding something key-point.
For Android, reference the JNA library adding #aar at the end of the string instead of downloading the JNA jar:
https://github.com/java-native-access/jna/blob/master/www/FrequentlyAskedQuestions.md#jna-on-android
Put the .so file in the following directory (when using android studio): yourproject\app\src\main\jniLibs\armeabi-v7a\libjnidispatch.so
Update I: (up to version <= 4.3.0) Since some of you asked where to find the *.so file:
On the official JNA site you will find all the supported architectures (30+) for download:
https://github.com/java-native-access/jna/tree/master/lib/native
Download the jar of the architecture you'd like and open it with some zip tool. In there you'll find the libjnidispatch.so file (of course only for unix architectures. For windows its a dll)
Update II: (starting from version >= 4.4.0)
Use the jna-X.X.0.aar file, supplied by the JNA project
As mentioned in a comment - starting from version 4.4.0 JNA publishes an AAR to maven central with all the libjnidispatch.so's in it. Users have had better luck using gradle than straight maven here, which doesn't always select or properly handle aars.
I found this comment in a file in the library github repo - "If you're using Google's Eclipse plugin then you must manually remove libjnidispatch.so from jna.jar/lib/armeabi and add it into your project's libs/armeabi directory."
Since this file was created in 2012 and Android Studio was still in very early phase and not super popular by that time, I assume it might be a valid note for Eclipse and also for Android Studio. I suggest you try it.
With the new SDK Tools and ADT versions 22.6.2, the default options for creating a new project with a blank activity produces a project which uses Fragments and refers to a library project (a new one for each project) named appcompat_v7_x.
The relevant parts of the stub code for the main activity are:
package com.myname.miniandroid;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
.....
.....
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
....
}
public static class PlaceholderFragment extends Fragment {
......
......
}
}
.
We can see that it uses a Fragment and an ActionBarActivity. The automatically created library project has both the v4 and the v7 jars in its libs folder, the main project has just the v4 jar. As built by default, hovering over either of these classes gives no javadoc information.
To get such support I've had to :
1) insert a file android-support-v7-appcompat.jar.properties into the libs folder of my automatically created appcompat_v7_4 library project. It contains the lines:
doc=c:\\dev\\tools\\android-sdk-windows4.4\\docs\\reference
src=C:\\dev\\tools\\android-sdk-windows4.4\\extras\\android\\support\\v7\\appcompat\\src
(Both lines seem to be necessary)
2) insert a file anndroid-support-v4.jar.properties into the libs folder of the main project. It contains the line:
src=C:\\dev\\tools\\android-sdk-windows4.4\\extras\\android\\support\\v4\\src
3) Close and reopen both projects and clean all projects.
I've got a brand new installation of Eclipse Kepler and an up to date installation of the SDK.
I feel that there must be a quicker way of getting set up to start a new project with the recommended default options. If anyone can tell me what it is, I would be most grateful.
You have use the "New" button in the upper left corner, select Android Application Project, choose the name, icon...
When you have done, you should have a working "Hello world" app.
If you want to add external libraries this is the answer: https://stackoverflow.com/a/3643015/3203988
I'm working on a libGDX project and I have a class called CheerVArachnids that has another inline class which is an event listener. When I run this project on the desktop it works fine. BUT when I run on my Android device, it can't find that inline class and I get the following error:
Could not find class 'com.bbj.cva.CheerVArachnids$PlaceUnitListener', referenced from method com.bbj.cva.CheerVArachnids.<init>
Here are the important parts of my class:
package com.bbj.cva;
public class CheerVArachnids implements ApplicationListener {
class PlaceUnitListener implements EventSubscriber<PlaceUnitEvent> {
#Override
public void onEvent(PlaceUnitEvent event)
{
//
}
}
public CheerVArachnids() {
EventBus.subscribe(PlaceUnitEvent.class, new PlaceUnitListener());
EventBus.subscribe(RemoveScreenObjectEvent.class,
new RemoveScreenObjectListener());
}
}
Any ideas why on Android, at runtime it can't find that inline class?
Since some ADT-Version you have to set which libraries / projects should be exported too.
Project-Propiertes -> Java Build Path -> Order and Export -> Check your Sources and other Libraries you are using.
Do these Export-Settings for your Core- and Android-Project.
Then it should work fine on Android.
In my case, everything worked fine until I installed the new updates for the SDK and Eclipse.
I got an error: "Could not find class..."
I found solution in another stackoverflow site.
I have a similar problem when using external jar (in my case openCSV). The reason I had a problem was due to a change in ADT 17 (or above). What I needed to do to resolve the problem was
In Eclipse go to Properties -> Java build path -> Order and export.
Mark my jar.
Move jar to top of the list.
The solution was found in the following page which reference to a very good article.
First you should: import XXX(Class).java,
If you added external library jar file import them to the LIB folder.
After that:
Right Click to your Project -> Properties -> Java Build Path -> Order and Export(tab) -> select All -> press OK -> Clean your Project.
Hope this solve this issue
I'm currently trying to implement a browser with pipelining feature in it. The problem is in order to code the HTTP pipeline, i need the support of a library. I tried to import it to my workspace by using Project - > properties -> Java build path ->addJar. And I was able to add it successfully. But now my problem is, even though it is added to my workspace it doesnt let me run this code. It gives me errors as if I have not imported the library:
package com.test.download;
import java.util.Collections;
import org.factor45.*;
public class Example {
// Execute the request
HttpRequestFuture<String> future = client.execute("hotpotato.factor45.org", 80, request,
new BodyAsStringProcessor());
future.addListener(new HttpRequestFutureListener<String>() {
#Override
public void operationComplete(HttpRequestFuture<String> future) throws Exception {
System.out.println(future);
if (future.isSuccessfulResponse()) {
System.out.println(future.getProcessedResult());
}
client.terminate();
}
});
}
This is available in the same website which provided me the library for the above code. "Here" is the website for your reference. Also you can find the JAR file inside this website.
Please help me to figure out the problem. If you solve this you are a life saver! Thanks in advance!
Try this:
Properties >> Java Build Path >> Libraries >> Add External JARs... Then select the library