我想在此处关注此代码示例:http://simena86.github.io/blog/2013/04/30/logging-accelerometer-from-android-to-pc/
我收到套接字错误:虽然允许拒绝异常。
这是我的清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.accelerometerlogger"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
教程的相关部分:
package noe.rart;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Socket_test2Activity extends Activity {
public int PORT = 15000;
private Button connectPhones;
private String serverIpAddress = "10.0.0.5";
private boolean connected = false;
TextView text;
EditText port;
EditText ipAdr;
private float x,y,z;
private SensorManager sensorManager;
private Sensor sensor;
boolean acc_disp = false;
boolean isStreaming = false;
PrintWriter out;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
connectPhones = (Button)findViewById(R.id.send);
connectPhones.setOnClickListener(connectListener);
text=(TextView)findViewById(R.id.textin);
port=(EditText)findViewById(R.id.port);
ipAdr=(EditText)findViewById(R.id.ipadr);
text.setText("Press send to stream acceleration measurement");
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
sensor = sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
port.setText("15000");
ipAdr.setText(serverIpAddress);
acc_disp =false;
}
private Button.OnClickListener connectListener = new Button.OnClickListener() {
@Override
public void onClick(View v) {
if (!connected) {
if (!serverIpAddress.equals("")) {
connectPhones.setText("Stop Streaming");
Thread cThread = new Thread(new ClientThread());
cThread.start();
}
}
else{
connectPhones.setText("Start Streaming");
connected=false;
acc_disp=false;
}
}
};
public class ClientThread implements Runnable {
Socket socket;
public void run() {
try {
acc_disp=true;
PORT = Integer.parseInt(port.getText().toString());
serverIpAddress=ipAdr.getText().toString();
InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
//InetAddress serverAddr = InetAddress.getByName("TURBOBEAVER");
socket = new Socket(serverAddr, PORT);
connected = true;
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
while (connected) {
out.printf("%10.2f\n", x);
out.flush();
Thread.sleep(2);
}
}
catch (Exception e) {
}
finally{
try{
acc_disp=false;
connected=false;
connectPhones.setText("Start Streaming");
//out.close();
socket.close();
}catch(Exception a){
}
}
}
};
private void init_perif(){
// smthing
}
@Override
protected void onResume() {
super.onResume();
sensorManager.registerListener(accelerationListener, sensor,
SensorManager.SENSOR_DELAY_FASTEST);
}
@Override
protected void onStop() {
sensorManager.unregisterListener(accelerationListener);
super.onStop();
}
private SensorEventListener accelerationListener = new SensorEventListener() {
@Override
public void onAccuracyChanged(Sensor sensor, int acc) {
}
@Override
public void onSensorChanged(SensorEvent event) {
x = event.values[0];
y = event.values[1];
z = event.values[2];
refreshDisplay();
}
};
private void refreshDisplay() {
if(acc_disp == true){
String output = String.format("X:%3.2f m/s^2 | Y:%3.2f m/s^2 | Z:%3.2f m/s^2", x, y, z);
text.setText(output);
}
}
}
和logcat:
11-10 01:44:07.125 31884-31884/com.example.accelerometerlogger E/Zygote﹕ MountEmulatedStorage()
11-10 01:44:07.125 31884-31884/com.example.accelerometerlogger E/Zygote﹕ v2
11-10 01:44:07.125 31884-31884/com.example.accelerometerlogger I/libpersona﹕ KNOX_SDCARD checking this for 10353
11-10 01:44:07.125 31884-31884/com.example.accelerometerlogger I/libpersona﹕ KNOX_SDCARD not a persona
11-10 01:44:07.145 31884-31884/com.example.accelerometerlogger I/SELinux﹕ Function: selinux_compare_spd_ram, SPD-policy is existed. and_ver=SEPF_SM-G900V_5.0 ver=27
11-10 01:44:07.145 31884-31884/com.example.accelerometerlogger I/SELinux﹕ Function: selinux_compare_spd_ram , priority [1] , priority version is VE=SEPF_SM-G900V_5.0_0027
11-10 01:44:07.145 31884-31884/com.example.accelerometerlogger E/SELinux﹕ [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL
11-10 01:44:07.145 31884-31884/com.example.accelerometerlogger I/art﹕ Late-enabling -Xcheck:jni
11-10 01:44:07.175 31884-31884/com.example.accelerometerlogger D/TimaKeyStoreProvider﹕ TimaSignature is unavailable
11-10 01:44:07.175 31884-31884/com.example.accelerometerlogger D/ActivityThread﹕ Added TimaKeyStore provider
11-10 01:44:07.215 31884-31884/com.example.accelerometerlogger D/ResourcesManager﹕ creating new AssetManager and set to /data/app/com.example.accelerometerlogger-2/base.apk
11-10 01:44:07.395 31884-31884/com.example.accelerometerlogger D/Activity﹕ performCreate Call secproduct feature valuefalse
11-10 01:44:07.395 31884-31884/com.example.accelerometerlogger D/Activity﹕ performCreate Call debug elastic valuetrue
11-10 01:44:07.415 31884-31884/com.example.accelerometerlogger D/SensorManager﹕ registerListener :: 0, MPU6500 Acceleration Sensor, 0, 0,
11-10 01:44:07.445 31884-31921/com.example.accelerometerlogger D/OpenGLRenderer﹕ Render dirty regions requested: true
11-10 01:44:07.565 31884-31921/com.example.accelerometerlogger I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build: ()
OpenGL ES Shader Compiler Version: E031.25.01.03
Build Date: 03/03/15 Tue
Local Branch: LA.BF.1.1_RB1_20150108_025_1077123_1158499
Remote Branch:
Local Patches:
Reconstruct Branch:
11-10 01:44:07.565 31884-31921/com.example.accelerometerlogger I/OpenGLRenderer﹕ Initialized EGL, version 1.4
11-10 01:44:07.605 31884-31921/com.example.accelerometerlogger I/OpenGLRenderer﹕ HWUI protection enabled for context , &this =0xb3a22088 ,&mEglDisplay = 1 , &mEglConfig = 8
11-10 01:44:07.615 31884-31921/com.example.accelerometerlogger D/OpenGLRenderer﹕ Enabling debug mode 0
11-10 01:44:07.755 31884-31884/com.example.accelerometerlogger I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@d099f83 time:608961596
11-10 01:44:18.875 31884-31884/com.example.accelerometerlogger D/ViewRootImpl﹕ ViewPostImeInputStage ACTION_DOWN
11-10 01:44:19.055 31884-31884/com.example.accelerometerlogger I/System.out﹕ Starting Stream
11-10 01:44:19.065 31884-32125/com.example.accelerometerlogger I/System.out﹕ Starting client thread
11-10 01:44:19.075 31884-32125/com.example.accelerometerlogger W/System.err﹕ java.net.ConnectException: failed to connect to /98.157.211.152 (port 6881): connect failed: ECONNREFUSED (Connection refused)
11-10 01:44:19.085 31884-32125/com.example.accelerometerlogger W/System.err﹕ at libcore.io.IoBridge.connect(IoBridge.java:124)
11-10 01:44:19.085 31884-32125/com.example.accelerometerlogger W/System.err﹕ at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
11-10 01:44:19.085 31884-32125/com.example.accelerometerlogger W/System.err﹕ at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:163)
11-10 01:44:19.085 31884-32125/com.example.accelerometerlogger W/System.err﹕ at java.net.Socket.startupSocket(Socket.java:590)
11-10 01:44:19.085 31884-32125/com.example.accelerometerlogger W/System.err﹕ at java.net.Socket.<init>(Socket.java:226)
11-10 01:44:19.085 31884-32125/com.example.accelerometerlogger W/System.err﹕ at com.example.accelerometerlogger.MainActivity$ClientThread.run(MainActivity.java:92)
11-10 01:44:19.085 31884-32125/com.example.accelerometerlogger W/System.err﹕ at java.lang.Thread.run(Thread.java:818)
11-10 01:44:19.085 31884-32125/com.example.accelerometerlogger W/System.err﹕ Caused by: android.system.ErrnoException: connect failed: ECONNREFUSED (Connection refused)
11-10 01:44:19.085 31884-32125/com.example.accelerometerlogger W/System.err﹕ at libcore.io.Posix.connect(Native Method)
11-10 01:44:19.085 31884-32125/com.example.accelerometerlogger W/System.err﹕ at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:111)
11-10 01:44:19.085 31884-32125/com.example.accelerometerlogger W/System.err﹕ at libcore.io.IoBridge.connectErrno(IoBridge.java:137)
11-10 01:44:19.085 31884-32125/com.example.accelerometerlogger W/System.err﹕ at libcore.io.IoBridge.connect(IoBridge.java:122)
11-10 01:44:19.085 31884-32125/com.example.accelerometerlogger W/System.err﹕ ... 6 more
答案 0 :(得分:2)
添加权限后,我显然还需要关闭/打开Android工作室并重新打开项目,或者只是重新打开项目。