大家好,又见面了,我是你们的朋友全栈君。
实现流程
大致画一个示意图:
package com.example.siemens.testgetmethod;
import android.app.Service;
import android.content.Intent;
import android.net.wifi.WifiConfiguration;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;
import org.alljoyn.bus.BusAttachment;
import org.alljoyn.bus.BusObject;
import org.alljoyn.bus.Mutable;
import org.alljoyn.bus.SessionOpts;
import org.alljoyn.bus.SessionPortListener;
import org.alljoyn.bus.Status;
import javax.net.ssl.SSLEngineResult;
public class MyService extends Service {
private final String TAG="MyService";
public MyService() {
// super();
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
// throw new UnsupportedOperationException("Not yet implemented");
return null;
}
public int onStartCommand(Intent intent, int flags, int startId) {
// Log.i(TAG, "onStartCommand()");
return START_STICKY;
}
public void onDestroy() {
// backgroundHandler.exit();
}
public void onCreate(){
Log.i(TAG,"onCreate method.");
//mbus初始化
mBus=new BusAttachment(getPackageName(),BusAttachment.RemoteMessage.Receive);
startBusThread();
if(backgroundHandler!=null)
backgroundHandler.connect();
else
return;
}
private void startBusThread(){
HandlerThread handlerThread=new HandlerThread("My HandlerThread");
handlerThread.start();
Looper loop=handlerThread.getLooper();
backgroundHandler=new BackgroundHandler(loop);
Log.i(TAG,"startBusThread method.");
}
//请求都在线程中执行
private BackgroundHandler backgroundHandler=null;
private final int CONNECT=2;
//私有内部不可变类
private final class BackgroundHandler extends Handler{
public BackgroundHandler(Looper looper){
super(looper);
}
public void connect(){
Log.i(TAG,"backgroundHandler.connect()");
Message msg=backgroundHandler.obtainMessage(CONNECT);//相当于message.what=2;
backgroundHandler.sendMessage(msg);
}
public void handleMessage(Message msg){
switch(msg.what){
case CONNECT:
doConnect();
break;
default:
break;
}
}
}
//创建总线附件对象,getPackageName()出现NullPointerException异常,先于构造函数执行,此时对象为null,所以放到oncreate里边去
// private BusAttachment mBus=new BusAttachment(getPackageName(),BusAttachment.RemoteMessage.Receive);
private BusAttachment mBus=null;
//创建总线对象
private MyBusObject myBusObject=new MyBusObject();
//总线对象类,实现总线接口和总线对象接口
class MyBusObject implements MyInterface,BusObject{
// public void show(String args){
// Toast.makeText(getApplicationContext(),args,Toast.LENGTH_SHORT).show(); //会调用的远程方法
//
// }
//
// public int add(int a,int b){ //会调用的远程方法
// return a+b;
// }
public String cat(String a,String b){
a=a.replace('H','T');
b=b.replace('W','B');
return a+b;
}
}
//session的端口号
private static final short CONTACT_PORT=42;
//总线对象,总线附件等连接到总线上的实现函数,创建的是service
private void doConnect(){
org.alljoyn.bus.alljoyn.DaemonInit.PrepareDaemon(getApplicationContext()); //启动守护进程
Status status=mBus.registerBusObject(myBusObject,"/myBusObject"); //注册总线对象
if (Status.OK != status) {
System.out.println("BusAttachment.registerBusObject() failed: " + status);
System.exit(0);
return;
}
//总线附件连接到总线上
status=mBus.connect();
if (status != Status.OK) {
Log.i(TAG,"BusAttachment connect to bus failed.");
System.exit(0);
return;
}
//通信的session port 设置
Mutable.ShortValue contactPort = new Mutable.ShortValue(CONTACT_PORT);
SessionOpts sessionOpts = new SessionOpts();
sessionOpts.traffic = SessionOpts.TRAFFIC_MESSAGES;
sessionOpts.isMultipoint = false;
sessionOpts.proximity = SessionOpts.PROXIMITY_ANY;
sessionOpts.transports = SessionOpts.TRANSPORT_ANY;
//绑定通信的session port
status=mBus.bindSessionPort(contactPort,sessionOpts,new SessionPortListener(){
@Override
public boolean acceptSessionJoiner(short sessionPort, String joiner, SessionOpts sessionOpts){
if (sessionPort == CONTACT_PORT) {
return true;
} else {
return false;
}
}
});
if (status != Status.OK) {
Log.i(TAG,"bind certain session port failed.");
System.exit(0);
return;
}
//向总线请求一个名称
int flags = 0; //do not use any request name flags
status = mBus.requestName("com.test.well.known.name", flags);
if (status != Status.OK) {
Log.i(TAG,"request name from bus failed.");
System.exit(0);
return;
}
//在总线上宣告name的存在
status = mBus.advertiseName("com.test.well.known.name", SessionOpts.TRANSPORT_ANY);
if (status != Status.OK) {
Log.i(TAG,"advertise name on bus failed.");
mBus.releaseName("com.test.well.know.name");
System.exit(0);
return;
}else
Toast.makeText(getApplicationContext(),"advertise name=com.test.well.known.name",Toast.LENGTH_SHORT).show();
}
static{
System.loadLibrary("alljoyn_java");
}
}
注意:静态初始化块中的loadlibrary不能少掉。上面的例子参照的是chat源码。
package com.example.siemens.testgetmethod;
import org.alljoyn.bus.BusException;
import org.alljoyn.bus.annotation.BusInterface;
import org.alljoyn.bus.annotation.BusMethod;
/**
* Created by Siemens on 2016/4/8.
*/
@BusInterface (name = "org.my.interface.name")
public interface MyInterface {
// @BusMethod
// void show(String test) throws BusException;
// @BusMethod
// int add(int a,int b) throws BusException;
@BusMethod
String cat(String a,String b) throws BusException;
}
注意:格式必须按照这种格式来,上面接口的name属性后面的值很重要client要对应。
package com.example.siemens.testgetmethodclient;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
import org.alljoyn.bus.BusAttachment;
import org.alljoyn.bus.BusListener;
import org.alljoyn.bus.Mutable;
import org.alljoyn.bus.ProxyBusObject;
import org.alljoyn.bus.SessionListener;
import org.alljoyn.bus.SessionOpts;
import org.alljoyn.bus.Status;
public class MyService extends Service {
private final String TAG="MyClientService";
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
// throw new UnsupportedOperationException("Not yet implemented");
return null;
}
private BusAttachment mBus=null;
private static final short CONTACT_PORT=42;
//代理总线对象;接口对象
private ProxyBusObject mProxyObj;
public MyInterface mInterface;
// public MyInterface getmInterface(){
// return mInterface;
// }
//application对象
private ClientApplication mApplication;
@Override
public void onCreate(){
mApplication=(ClientApplication)getApplication();
//守护进程不能忘
org.alljoyn.bus.alljoyn.DaemonInit.PrepareDaemon(getApplicationContext()); //启动守护进程
mBus=new BusAttachment(getApplication().getPackageName(),BusAttachment.RemoteMessage.Receive);
//连接两次?
// Status status=mBus.connect();
// if (Status.OK != status) {
// Log.i(TAG,"client busattachment first connect to bus failed.");
// System.exit(0);
// return;
// }
mBus.registerBusListener(new BusListener(){
@Override
public void foundAdvertisedName(String name, short transport,String namePrefix){
mBus.enableConcurrentCallbacks();
short contactPort = CONTACT_PORT;
SessionOpts sessionOpts = new SessionOpts();
sessionOpts.traffic = SessionOpts.TRAFFIC_MESSAGES;
sessionOpts.isMultipoint = false;
sessionOpts.proximity = SessionOpts.PROXIMITY_ANY;
sessionOpts.transports = SessionOpts.TRANSPORT_ANY;
Mutable.IntegerValue sessionId = new Mutable.IntegerValue();
Status status = mBus.joinSession(name,contactPort,sessionId,sessionOpts,new SessionListener());
mProxyObj = mBus.getProxyBusObject("com.test.well.known.name", "/myBusObject", sessionId.value, new Class[]{MyInterface.class});
mInterface=mProxyObj.getInterface(MyInterface.class);
//personal add
mApplication.setTestInterface(mInterface);
}
});
//连接两次??官网API问题???
Status status=mBus.connect();
if (Status.OK != status) {
Log.i(TAG,"client busattachment second connect to bus failed.");
System.exit(0);
return;
}
//找到指定名称的busAttachment
status = mBus.findAdvertisedName("com.test.well.known.name");
if (status != Status.OK) {
Log.i(TAG,"client findAdvertisedName failed.");
System.exit(0);
return;
}
else
Toast.makeText(getApplicationContext(),"find name=com.test.well.known.name success.",Toast.LENGTH_SHORT).show();
}
static{
System.loadLibrary("alljoyn_java");
}
}
同时client端app也需要知道总线接口,但是要通过代理对象获得方法的具体实现,也要包含接口类,同server接口类。
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/151688.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...