前提・実現したいこと
はじめて投稿させていただきます。
現在、Huawei P20 Lite(Android 8.0.0)のアプリの開発を行っております。
Android4.0.3(API15)で開発していたアプリを、この度Android8.0(API26)で復活させるべく、動作確認をおこなっていたところ、Socket通信が正常に行われていないことが判明しました。
Socket通信では、バイナリデータの送受信を行っています。
発生している問題・エラーメッセージ
原因を突き止めるべく簡素なテストアプリを作成し、挙動を確認していたところ、サーバー側に情報が届いていない状態であることがわかりました。
どのようにすれば通信が正常に行えるか、どなたかご教授いただけませんでしょうか?
該当のソースコード
ソース内容は下記です。
activity_main.xml
java
1<LinearLayout 2 android:layout_width="wrap_content" 3 android:layout_height="wrap_content" 4 android:layout_alignParentLeft="true" 5 android:layout_alignParentRight="true" 6 android:layout_alignParentTop="true" > 7 8 <Button 9 android:id="@+id/button1" 10 android:layout_width="150dp" 11 android:layout_height="wrap_content" 12 android:text="init" /> 13 14 <Button 15 android:id="@+id/button2" 16 android:layout_width="150dp" 17 android:layout_height="wrap_content" 18 android:text="Send" /> 19 20</LinearLayout>
MainActivity.java
java
1public class MainActivity extends Activity { 2 3 Socket socket = null; 4 OutputStream output = null; 5 InputStream input = null; 6 7 @Override 8 protected void onCreate(Bundle savedInstanceState) { 9 super.onCreate(savedInstanceState); 10 setContentView(R.layout.activity_main); 11 12 Button button1 = findViewById(R.id.button1); 13 Button button2 = findViewById(R.id.button2); 14 15 // 初期化ボタン 16 button1.setOnClickListener(new View.OnClickListener() { 17 18 @Override 19 public void onClick(View v) { 20 Runnable sender = new Runnable() { 21 @Override 22 public void run() { 23 String address = "192.168.1.57"; 24 int port = 50000; 25 26 try { 27 socket = new Socket(address, port); 28 output = socket.getOutputStream(); 29 input = socket.getInputStream(); 30 31 } catch (UnknownHostException e) { 32 e.printStackTrace(); 33 } catch (IOException e) { 34 e.printStackTrace(); 35 } 36 } 37 }; 38 Thread th = new Thread(sender); 39 th.start(); 40 } 41 }); 42 43 // 送信ボタン 44 button2.setOnClickListener(new View.OnClickListener() { 45 46 @Override 47 public void onClick(View v) { 48 Runnable sender = new Runnable() { 49 @Override 50 public void run() { 51 byte[] buffer = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06}; 52 try { 53 if (socket != null) { 54 output.write(buffer); //※※ここで落ちます※※ 55 output.flush(); 56 } 57 } catch (IOException e) { 58 e.printStackTrace(); 59 } 60 } 61 }; 62 Thread th = new Thread(sender); 63 th.start(); 64 } 65 }); 66 } 67 68 @Override 69 protected void onDestroy() 70 { 71 if (socket != null) { 72 try { 73 socket.close(); 74 socket = null; 75 } catch (IOException e) { 76 e.printStackTrace(); 77 } 78 } 79 } 80}
AndroidManifest.xml
java
1<uses-sdk 2 android:minSdkVersion="15" 3 android:targetSdkVersion="26" /> 4<uses-permission android:name="android.permission.INTERNET"/> 5 6<application 7 android:allowBackup="true" 8 android:icon="@drawable/ic_launcher" 9 android:label="@string/app_name" 10 android:theme="@style/AppTheme" > 11 <activity 12 android:name=".MainActivity" 13 android:label="@string/app_name" > 14 <intent-filter> 15 <action android:name="android.intent.action.MAIN" /> 16 17 <category android:name="android.intent.category.LAUNCHER" /> 18 </intent-filter> 19 </activity> 20</application>
試したこと
ボタンを一つにし、
java
1String address = "192.168.1.57"; 2int port = 50000; 3byte[] buffer = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06}; 4 5try { 6 socket = new Socket(address, port); 7 output = socket.getOutputStream(); 8 input = socket.getInputStream(); 9 10 output.write(buffer); 11 output.flush(); 12 13} catch (UnknownHostException e) { 14 e.printStackTrace(); 15} catch (IOException e) { 16 e.printStackTrace(); 17}
とすると、サーバー側アプリで受け取ることができます。
将来的には一定周期でアクセスを行いたいため、毎回の接続時にSocketの生成をするのはあまり良くないと思い、OutputStreamを取得し、送信できるようにしたいと思っています。
補足情報(FW/ツールのバージョンなど)
開発環境 Eclipse
下記バージョン(23.0.7.2120684)
Android DDMS
Android 開発ツール
Android 階層ビューアー
Android ネイティブ開発ツール
Android トレースビュー
OpenGL ES 用トレーサー
下記バージョン(23.0.2.1259578)
Eclipse IDE for Android Developers
諸事情によりAndroid StudioでなくEclipseでの開発とさせていただいています。
宜しくお願い申し上げます。
回答1件
あなたの回答
tips
プレビュー