Nikolay ILYIN
unread,Oct 22, 2017, 4:48:19 PM10/22/17Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to android-ndk
Alex, hi: thanks for answering me. It looks like I've created too many unnecessary layers (for the sake of true background my native peerprotocols operation) and the better way will be to get rid of the service and its helper class at all. To clarify: my native code is doing some UPNP and Bluetooth logic such as as device discovery and sending text and (whole) files.
here is how my jni file looks, could you please check it out and confirm that no dedicated Service is actually needed:
package myapp.jnipeerprotocol;
import android.content.Context;
import android.os.Environment;
import android.util.Log;
import net.myapp.one.utils.Consts;
public class PeerProtocol {
/*
I had to do this to send callbacks to the specifically created peerprotocol java service, which in turn processes the data received and then LocalBroadcasts that to PeerProtocolHelper class imported to Activity/Fragments
*/
public interface IPeerProtocolCallbackListener {
void deviceScanCallback(String deviceName, String ipAddress, int scanType);
void bTFileTransferCallback(String strDeviceName, String strAddress, int transferType, String strFile, int param);
}
private static IPeerProtocolCallbackListener mListener;
public void setOnPeerProtocolCallbackListener(IPeerProtocolCallbackListener listener) {
mListener = listener;
}
private static final String TAG = "PeerProtocolClass";
private static PeerProtocol instance = null;
private Context mContext;
private static int transfer_result = 0;
public PeerProtocol(Context c) {
mContext = c;
}
public static PeerProtocol getInstance(Context context) {
if (instance == null) {
instance = new PeerProtocol(context.getApplicationContext());
}
return instance;
}
// below a list of C/C++ native functions
public static native int initializePeerProtocol();
public static native int terminatePeerProtocol();
public static native int scanNetwork();
public static native int transferFile(String deviceAddress, String pathFileName);
public static native int setDeviceDiscoveryCallbackFunc(String func);
public static native int setFileTransferCallbackFunc(String func);
public static native int setDownloadFolder(String path);
public static native int hasRouterServices();
public static native String getExternalIPAddress();
public static native int addPortMapping(String remoteHost, int externalPort, String protocol, int internalPort, String internalClient, String description);
public static native int deletePortMapping(String remoteHost, int externalPort, String protocol);
public static void scan() {
PeerProtocol.scanNetwork();
}
public static int init() {
int ret = PeerProtocol.initializePeerProtocol();
if (ret != -1) { // ret = 0 if ok
PeerProtocol.setDeviceDiscoveryCallbackFunc("PeerProtocolDeviceProc");
PeerProtocol.setFileTransferCallbackFunc("PeerProtocolTransferProc");
PeerProtocol.setDownloadFolder(Environment.getExternalStorageDirectory().getAbsolutePath() + Consts.DEFAULT_FOLDER + "/");
}
return ret;
}
public static int PeerProtocolDeviceProc(String strDeviceName, String strIpAddress, int scanType) {
if (strDeviceName == null && strIpAddress == null)
return -1;
final String devname = strDeviceName;
final String ipa = strIpAddress;
final int scantype = scanType;
mListener.deviceScanCallback(devname, ipa, scantype);
return 0;
}
private static boolean callbackReceived = false;
public static int PeerProtocolTransferProc(String strDeviceName, String strAddress, int transferType, String strFile, int param) {
mListener.bTFileTransferCallback(strDeviceName, strAddress, transferType, strFile, param);
waitForResult ();
return transfer_result;
}
public synchronized void setFileTransferCallbackProcResults (int result) {
transfer_result = result;
callbackReceived = true;
this.notify();
}
private static synchronized void waitForResult () {
callbackReceived = false;
while (!callbackReceived) {
try {
PeerProtocol.class.wait(10000);
} catch (InterruptedException ignore) {
}
}
}
static {
System.loadLibrary("jniPeerProtocol");
}
}