#Regvision_Alljoyn_Android_Contacts
1.开启channel
requestName();
bindSession();
advertise();
2.关闭channel
cancelAdvertise();
unbindSession();
releaseName();
3.刷新channel列表
ChatBusListener中的foundAdvertisedName
4.加入session
useSetChannelName(name);
useJoinChannel();
5.离开session
application.useLeaveChannel();
6.通信
发消息
private void doSendMessages() {
Log.i(TAG, "doSendMessages()");
String message;
while ((message = mChatApplication.getOutboundItem()) != null) {
Log.i(TAG, "doSendMessages(): sending message \"" + message + "\"");
/*
* If we are joined to a remote session, we send the message over
* the mChatInterface. If we are implicityly joined to a session
* we are hosting, we send the message over the mHostChatInterface.
* The mHostChatInterface may or may not exist since it is created
* when the sessionJoined() callback is fired in the
* SessionPortListener, so we have to check for it.
*/
try {
if (mJoinedToSelf) {
if (mHostChatInterface != null) {
mHostChatInterface.Chat(message);
}
} else {
mChatInterface.Chat(message);
}
} catch (BusException ex) {
mChatApplication.alljoynError(ChatApplication.Module.USE, "Bus exception while sending message: (" + ex + ")");
}
}
}
收消息
@BusSignalHandler(iface = "org.alljoyn.bus.samples.chat", signal = "Chat")
public void Chat(String string) {
/*
* See the long comment in doJoinSession() for more explanation of
* why this is needed.
*
* The only time we allow a signal from the hosted session ID to pass
* through is if we are in mJoinedToSelf state. If the source of the
* signal is us, we also filter out the signal since we are going to
* locally echo the signal.
*/
String uniqueName = mBus.getUniqueName();
MessageContext ctx = mBus.getMessageContext();
Log.i(TAG, "Chat(): use sessionId is " + mUseSessionId);
Log.i(TAG, "Chat(): message sessionId is " + ctx.sessionId);
/*
* Always drop our own signals which may be echoed back from the system.
*/
if (ctx.sender.equals(uniqueName)) {
Log.i(TAG, "Chat(): dropped our own signal received on session " + ctx.sessionId);
return;
}
/*
* Drop signals on the hosted session unless we are joined-to-self.
*/
// if (mJoinedToSelf == false && ctx.sessionId == mHostSessionId) {
// Log.i(TAG, "Chat(): dropped signal received on hosted session " + ctx.sessionId + " when not joined-to-self");
// return;
// }
/*
* To keep the application simple, we didn't force users to choose a
* nickname. We want to identify the message source somehow, so we
* just use the unique name of the sender's bus attachment.
*/
String nickname = ctx.sender;
nickname = nickname.substring(nickname.length()-10, nickname.length());
Log.i(TAG, "Chat(): signal " + string + " received from nickname " + nickname);
mChatApplication.newRemoteUserMessage(nickname, string);
}
本文详细介绍了使用AllJoyn在Android平台上的通讯实现,包括名称请求、绑定、广告发布、会话管理和消息传递。重点讲解了如何加入/离开会话、控制信号传递,并展示了接收和处理来自其他用户的聊天消息的代码片段。

1234

被折叠的 条评论
为什么被折叠?



