概述
Coap 简介
Coap是一种基于UDP的应用层协议。Coap支持RESTful规范,也就是支持URL方式访问,就像HTTP请求一样,支持GET,POST,PUT,DELETE,还支持订阅。详细可以看这里http://blog.csdn.net/xukai871105/article/details/17734163
Coap客户端
Coap客户端详细可以看这里的demo:https://github.com/eclipse/californium。
为了方便调试,我们使用火狐浏览器coap插件,地址:https://addons.mozilla.org/en-US/firefox/addon/copper-270430/。安装后如图:
服务端(android服务端)
使用开源的californium的SDK,github地址:https://github.com/eclipse/californium。
开启一个服务用来启动CoapServer:
public class ZHCoapService extends Service {
private static final String TAG="ZHCoapService";
private static final boolean DEBUG=true;
private static final int HTTP_SERVER_PROXY_PORT = 8080;
private static final int PORT = NetworkConfig.getStandard().getInt(NetworkConfig.Keys.COAP_PORT);
private static CoapServer mCoapServer;
private ProxyHttpServer mHttpServer = null;
private ForwardingResource coap2coap = new ProxyCoapClientResource("coap2coap");
private ForwardingResource coap2http = new ProxyHttpClientResource("coap2http");
private ZHDataObserve mDataObserve;
private ZHQueryResource mQueryResource;
private boolean isConnected=false;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
initCoapResource();
IntentFilter intentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(connectReceiver, intentFilter);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Serializable serializable=intent.getSerializableExtra(Const.KEY_CMD);
if(serializable != null && serializable instanceof ICmd){
ICmd cmd=(ICmd) serializable;
if(mDataObserve != null){
//将消息通知客户端
mDataObserve.change(cmd.getCmdString());
}
}
return START_STICKY;
}
/**
* 初始化CoapResource
*/
private void initCoapResource() {
mCoapServer = new CoapServer(PORT);
mCoapServer.add(coap2coap);
mCoapServer.add(coap2http);
mQueryResource=new ZHQueryResource(getApplicationContext(),"query");
mDataObserve=new ZHDataObserve("notify");
mCoapServer.add(mDataObserve);
mCoapServer.add(mQueryResource);
}
/**
* 开启coap服务
*/
private void startCoapServer() {
try {
stopCoapServer();
if (mHttpServer == null)
try {
mHttpServer = new ProxyHttpServer(HTTP_SERVER_PROXY_PORT);
} catch (IOException e) {
e.printStackTrace();
}
mHttpServer.setProxyCoapResolver(new DirectProxyCoapResolver(coap2coap));
mCoapServer.start();
if (DEBUG)
Log.d(TAG, "CoapServer start");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 停止coap服务
*/
private void stopCoapServer() {
if (mCoapServer != null) {
mCoapServer.stop();
if (DEBUG)
Log.d(TAG, "CoapServer stop");
}
}
private BroadcastReceiver connectReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
State state = State.DISCONNECTED;
if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
ConnectivityManager contectivityMananger = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkInfo = contectivityMananger.getActiveNetworkInfo();
if (mNetworkInfo != null && mNetworkInfo.isConnected()) {
state = mNetworkInfo.getState();
}
}
if (state == State.CONNECTED &&!isConnected) {
isConnected=true;
startCoapServer();
} else if (state == State.DISCONNECTED) {
isConnected=false;
stopCoapServer();
}
}
};
public void onDestroy() {
unregisterReceiver(connectReceiver);
super.onDestroy();
};
}
在网络连接后启动coap服务。
在客户端上输入地址coap://ip:5683/,点击Discover即可查询出所有资源。
这里添加了2个资源“notify”和 “query”,其中“notify”有开通订阅功能,如图:
public class ZHDataObserve extends CoapResource {
private String msg="";
public ZHDataObserve(String name) {
super(name);
setObservable(true); // enable observing
setObserveType(Type.NON); // configure the notification type to CONs
getAttributes().setObservable(); // mark observable in the Link-Format
}
public void change(String msg){
this.msg=msg;
if(getObserverCount()>0){
changed();
}
}
@Override
public void handleGET(CoapExchange exchange) {
exchange.respond(ResponseCode.CONTENT,msg,MediaTypeRegistry.TEXT_PLAIN);
}
}
调用ZHDataObserve .change(“…”); 即可将消息通知到订阅者。
附上代码:
CoapDemo:CSDN
CoapDemo:github
最后
以上就是激昂冥王星为你收集整理的使用Coap实现android 局域网通信的全部内容,希望文章能够帮你解决使用Coap实现android 局域网通信所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复