- 0
- 0
- 约5.35万字
- 约 38页
- 2018-11-04 发布于湖北
- 举报
pc客户端跟android服务端的socket同步通信
PC客户端与Android服务端的Socket同步通信(USB) 收藏
需求:
1.一个android端的service后台运行的程序,作为socket的服务器端;用于接收Pc client端发来的命令,来处理数据后,把结果发给PC client
2.PC端程序,作为socket的客户端,用于给android手机端发操作命令
难点分析:
1.手机一定要有adb模式,即插上USB线时马上提示的对话框选adb。好多对手机的操作都可以用adb直接作。
不过,我发现LG GW880就没有,要去下载个
2.android默认手机端的IP为“”
3.要想联通PC与android手机的sokcet,一定要用adb forward 来作下端口转发才能连上socket.
view plaincopy to clipboardprint?
Runtime.getRuntime().exec(adb forward tcp:12580 tcp:10086);
Thread.sleep(3000);
Runtime.getRuntime().exec(adb forward tcp:12580 tcp:10086);
Thread.sleep(3000);
4.android端的service程序Install到手机上容易,但是还要有方法来从PC的client端来启动手机上的service ,这个办法可以通过PC端adb命令来发一个Broastcast ,手机端再写个接收BroastcastReceive来接收这个Broastcast,在这个BroastcastReceive来启动service
pc端命令:
view plaincopy to clipboardprint?
Runtime.getRuntime().exec(
adb shell am broadcast -a NotifyServiceStart);
Runtime.getRuntime().exec(
adb shell am broadcast -a NotifyServiceStart);
android端的代码:ServiceBroadcastReceiver.java
view plaincopy to clipboardprint?
package com.otheri.service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class ServiceBroadcastReceiver extends BroadcastReceiver {
private static String START_ACTION = NotifyServiceStart;
private static String STOP_ACTION = NotifyServiceStop;
@Override
public void onReceive(Context context, Intent intent) {
Log.d(androidService.TAG, Thread.currentThread().getName() + ----
+ ServiceBroadcastReceiver onReceive);
String action = intent.getAction();
if (START_ACTION.equalsIgnoreCase(action)) {
context.startService(new Intent(context, androidService.class));
Log.d(androidService.TAG, Thread.currentThread().getName() + ----
+ ServiceBroadcastReceiver onReceive start end);
} else if (ST
您可能关注的文档
最近下载
- 汇川《HD90S系列高压变频器用户手册》-D项目.pdf
- 中国铁路客票发售和预订系统5.0版本(TRSv5.0)售票与经由维护操作说明.pdf VIP
- 人教版2025年中考化学全册考点知识点总结(超强).doc VIP
- 2023北京各区初三一模语文试题汇编《记叙文阅读》.pdf VIP
- 辽宁省事业单位考试综合应用能力(医疗卫生类E类)2026年备考难点精析.docx VIP
- 贴片稳压二极管代号与普通型号元件封装对照表.pdf VIP
- 石化工程项目界面管理.pdf VIP
- 幼儿班级管理课件.pptx VIP
- 宠物咖啡店计划书.docx VIP
- 重庆市(康德卷)2025届高三第一次联合诊断检测数学(原卷版).docx VIP
原创力文档

文档评论(0)