gpt4 book ai didi

java - Alljoyn 框架 - Java - 信号。 BUS_NO_SUCH_INTERFACE

转载 作者:行者123 更新时间:2023-11-30 11:08:18 25 4
gpt4 key购买 nike

这是一个非常具体的问题,但是在同一个地方呆了几天并且在 Alljoyn 论坛上没有得到任何回应之后,我决定在这里发布它。也许有人使用过这个框架。

我正在做一个项目,我需要使用 Alljoyn 框架提供的信号机制。但是,我确实需要 session 中的信号,并且 api 核心中提供的示例是无 session 的。

在我的例子中,我需要服务(服务器)发出信号并让客户端接收它,但我收到了这个错误。按照 api 示例,我设法创建了一个简单的应用程序,它通过接口(interface)中定义的方法交换消息,因此我知道通信正在运行。在这种情况下,服务会一直等到客户端连接并发送信号。客户端加入 session 并在注册信号后立即收到 BUS_NO_SUCH_INTERFACE 错误。我也尝试在发送信号之后和之前注册,同样的问题。我认为由于某种原因,客户端没有找到 bussignalhandler,但我不知道为什么。我也把它放在外部类中,但没有用。

我正在遵循核心 api 指南中的示例: https://allseenalliance.org/developers/develop/api-guide/core/android

这是服务注册并发出信号的部分:

SignalEmitter emitter = new SignalEmitter(mySignalInterface, joinerName, 
sessionId,
SignalEmitter.GlobalBroadcast.Off);
myInterface = emitter.getInterface(SampleInterface.class);
// Emitting signals myInterface.buttonClicked(1);
myInterface.playerPosition(12, 1, -24);

但是,在该示例中,我看不到 myInterface 的定义。我知道这不是一个错误,他们的意思是 mySignalInterface 因为方法 getInterface 要求一个接口(interface)对象,而 mySignalInterface 是一个实现它的类界面。

我将我创建的示例放在这里,我会上传文件以防有人想尝试。

@BusInterface(name = "org.alljoyn.bus.samples.simple.SimpleInterface")
public interface SimpleInterface {

@BusMethod
String Ping(String inStr) throws BusException;

@BusSignal
public void playerPosition(int x, int y, int z) throws BusException;
}

服务:

服务中的类来实现信号和方法

class SimpleService implements SimpleInterface, BusObject {

public String Ping(String inStr) {
mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_PING, inStr));
return inStr;
}

public void playerPosition(int x, int y, int z) { /* no implementation needed here*/}

}

这里是在服务中建立连接的部分代码:

case CONNECT: {
org.alljoyn.bus.alljoyn.DaemonInit.PrepareDaemon(getApplicationContext());

mBus = new BusAttachment(getPackageName(), BusAttachment.RemoteMessage.Receive);

mBus.registerBusListener(new BusListener());

Status status = mBus.registerBusObject(mSimpleService, "/SimpleService");
if (status != Status.OK) {...}

status = mBus.connect();
logStatus("BusAttachment.connect()", status);
if (status != Status.OK) {...}

int flag = 0;

status = mBus.requestName(SERVICE_NAME, flag);
logStatus(String.format("BusAttachment.requestName(%s, 0x%08x)", SERVICE_NAME, flag), status);
if (status == Status.OK) {
status = mBus.advertiseName(SERVICE_NAME, SessionOpts.TRANSPORT_ANY);
logStatus(String.format("BusAttachement.advertiseName(%s)", SERVICE_NAME), status);
if (status != Status.OK) {...}
}

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 + SessionOpts.TRANSPORT_WFD;

status = mBus.bindSessionPort(contactPort, sessionOpts, new SessionPortListener() {
@Override
public boolean acceptSessionJoiner(short sessionPort, String joiner, SessionOpts sessionOpts) {
return sessionPort == CONTACT_PORT;
}

@Override
public void sessionJoined(short port, int id, String s) {
sessionId = id; joinerName = s; sessionEstablished = true;
}
});
logStatus(String.format("BusAttachment.bindSessionPort(%d, %s)",
contactPort.value, sessionOpts.toString()), status);
if (status != Status.OK) {...}

try {
while (!sessionEstablished) {
Thread.sleep(10);
}

SignalEmitter emitter = new SignalEmitter(mSimpleService, joinerName, sessionId, SignalEmitter.GlobalBroadcast.Off);
SimpleInterface myInterface = emitter.getInterface(SimpleInterface.class);
myInterface.playerPosition(12,1,1);
}
catch (BusException ex) {... }
}

客户:

/*On create of the activity which has a button and a text view to send text to the server */
mEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_NULL
&& event.getAction() == KeyEvent.ACTION_UP) {
/* Call the remote object's Ping method. */
Message msg = mBusHandler.obtainMessage(BusHandler.PING,
view.getText().toString());
mBusHandler.sendMessage(msg);
}
return true;
}
});

private static final String SERVICE_NAME = "org.alljoyn.bus.samples.simple";
private static final short CONTACT_PORT=42;

private BusAttachment mBus;
private ProxyBusObject mProxyObj;
private SimpleInterface mSimpleInterface;


case CONNECT: {
org.alljoyn.bus.alljoyn.DaemonInit.PrepareDaemon(getApplicationContext());

mBus = new BusAttachment(getPackageName(), BusAttachment.RemoteMessage.Receive);


mBus.registerBusListener(new BusListener() {
@Override
public void foundAdvertisedName(String name, short transport, String namePrefix) {

if(!mIsConnected) {
Message msg = obtainMessage(JOIN_SESSION);
msg.arg1 = transport;
msg.obj = name;
sendMessage(msg);
}
}
});


Status status = mBus.connect();
logStatus("BusAttachment.connect()", status);
if (Status.OK != status) {...}


status = mBus.findAdvertisedName(SERVICE_NAME);
logStatus(String.format("BusAttachement.findAdvertisedName(%s)", SERVICE_NAME), status);
if (Status.OK != status) {...}

break;
}
case (JOIN_SESSION): {

if (mIsStoppingDiscovery) {
break;
}

short contactPort = CONTACT_PORT;
SessionOpts sessionOpts = new SessionOpts();
sessionOpts.transports = (short)msg.arg1;
Mutable.IntegerValue sessionId = new Mutable.IntegerValue();

Status status = mBus.joinSession((String) msg.obj, contactPort, sessionId, sessionOpts, new SessionListener() {
@Override
public void sessionLost(int sessionId, int reason) {
mIsConnected = false;
logInfo(String.format("MyBusListener.sessionLost(sessionId = %d, reason = %d)", sessionId,reason));
mHandler.sendEmptyMessage(MESSAGE_START_PROGRESS_DIALOG);
}
});


if (status == Status.OK) {

mProxyObj = mBus.getProxyBusObject(SERVICE_NAME,
"/SimpleService",
sessionId.value,
new Class<?>[] { SimpleInterface.class });


mSimpleInterface = mProxyObj.getInterface(SimpleInterface.class);

mSessionId = sessionId.value;
mIsConnected = true;
mHandler.sendEmptyMessage(MESSAGE_STOP_PROGRESS_DIALOG);
}
break;
status = mBus.registerSignalHandlers(this);
if (status != Status.OK) {...}
}
case PING: {
try {
if (mSimpleInterface != null) {

sendUiMessage(MESSAGE_PING, msg.obj);
String reply = mSimpleInterface.Ping((String) msg.obj);
sendUiMessage(MESSAGE_PING_REPLY, reply);
} catch {...}
}
...here some more code...
@BusSignalHandler(iface="org.alljoyn.bus.samples.simple.SimpleInterface", signal="playerPosition")
public void playerPosition(int x, int y, int z) {
sendUiMessage(MESSAGE_POST_TOAST, "Signal captured");
}

在这个例子中,我的客户端有一个 TextView ,我可以在其中添加文本并向服务器发送一个 ping。如果我摆脱注册信号部分,这将起作用。我尝试按照 api 核心所说的那样使用 Thread.sleep 进行操作,但也不起作用。

在这里,我根据 alljoyn api 中的示例添加了我的两个应用程序(客户端和服务器)的代码。 https://github.com/JavierT/Alljoyn_signal_sample

如果您有任何疑问,请告诉我,很难将所有信息放在一篇文章中。提前谢谢你。

最佳答案

我认为这应该可以解决您的问题

Alljoyn BusSignalHandler

以下任一项都可用于注释信号处理程序:

 @BusSignalHandler(iface = "org.sample.MyInterface", signal = "MySignal")
public void handleSignal(String str)
{
}

@BusSignalHandler(iface = "org.myapp.IMyInterface", signal = "EmitMySignal")
public void handleSignal(String str)
{
}

BusAttachment 通过先前调用 BusAttachment.registerBusObject(BusObject, String) 知道 IMyInterface 时,可以成功使用第一个示例> 或 BusAttachment.getProxyBusObject(String, String, int, Class[])

IMyInterfaceBusAttachment 未知时,可以成功使用第二个示例。使用第二个示例应该可以解决您的问题。

此外,根据上面提供的链接,在您的客户端程序中,

@BusSignalHandler(iface="org.alljoyn.bus.samples.simple.SimpleInterface", signal="playerPosition")

应替换为

@BusSignalHandler(iface="org.alljoyn.bus.samples.simpleclient.SimpleInterface", signal="playerPosition")

关于java - Alljoyn 框架 - Java - 信号。 BUS_NO_SUCH_INTERFACE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28611960/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com