2016-09-25 9 views
2

Android StudioでArduino Pro Miniで1を送信する必要があるアプリケーションを作成しました。データは送信されますが、Arduinoには届きません。私はArduinoをBluetooth端末でチェックしたところ、問題はすべてコード化されていたので問題ありません。なにが問題ですか?Bluetoothでデータを送信できません

package com.example.niko.motoco; 

import android.app.Activity; 
import android.bluetooth.BluetoothAdapter; 
import android.bluetooth.BluetoothDevice; 
import android.bluetooth.BluetoothSocket; 
import android.content.Intent; 
import android.os.Bundle; 
import android.support.design.widget.Snackbar; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.util.Log; 
import android.view.View; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.Toast; 

import java.io.IOException; 
import java.io.OutputStream; 
import java.util.UUID; 

public class MainActivity extends AppCompatActivity { 

    BluetoothAdapter btAdapter = null ; 
    BluetoothDevice btDevice = null; 
    BluetoothSocket btSocket = null; 

    final String LOG_TAG = "myLogs"; 
    Button btConnect, btSend; 
    public TextView checkText; 

    private static final int REQUEST_ENABLE_BT = 1; 
    private static final int CONNECTEDdevice = 2; 

    Boolean connection = false; 
    private static String MAC = null; 
    UUID my_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); 
    private ConnectedThread MyThread = null; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     checkText = (TextView) findViewById(R.id.checkText); 
     btConnect = (Button)findViewById(R.id.btConnect); 
     btSend = (Button)findViewById(R.id.btSend); 

     btAdapter = BluetoothAdapter.getDefaultAdapter(); 
     if (btAdapter == null) { 
      Toast.makeText(getApplicationContext(),"Device does not support Bluetooth",Toast.LENGTH_LONG).show(); 
     } else if (!btAdapter.isEnabled()) { 
      Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
      startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); 
     } 


     btConnect.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       if (connection) { 
        // disconnect 
        try { 
         btSocket.close(); 
         connection = false; 
         btConnect.setText("Connect"); 
         Toast.makeText(getApplicationContext(),"Bluetooht is disconnected:",Toast.LENGTH_LONG).show(); 
        } catch (IOException error) { 
         Toast.makeText(getApplicationContext(),"Error;" + error,Toast.LENGTH_LONG).show(); 
        } 
       } else { 
        // connect 
        Intent ListDevices = new Intent(MainActivity.this, ListDevices.class); 
        startActivityForResult(ListDevices,CONNECTEDdevice); 
       } 

      } 
     }); 

     btSend.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       MyThread.write((byte) 1); 
       Toast.makeText(getApplicationContext(),"Sending 1",Toast.LENGTH_LONG).show(); 
      } 
     }); 


    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     //super.onActivityResult(requestCode, resultCode, data); 
     switch (requestCode) { 
      case REQUEST_ENABLE_BT: 
       if(resultCode == Activity.RESULT_OK) { 
        Toast.makeText(getApplicationContext(),"bluetooth is activated",Toast.LENGTH_LONG).show(); 
       } else { 
        Toast.makeText(getApplicationContext(),"bluetooth is not activated",Toast.LENGTH_LONG).show(); 
        finish(); 
       } 
       break; 

      case CONNECTEDdevice : 
       if (resultCode == Activity.RESULT_OK) { 
        MAC = data.getExtras().getString(ListDevices.MACaddress); 

        btDevice = btAdapter.getRemoteDevice(MAC); 

        try { 
         btSocket = btDevice.createRfcommSocketToServiceRecord(my_UUID); 

         btSocket.connect(); 

         connection = true; 

         btConnect.setText("Disconnect"); 

         Toast.makeText(getApplicationContext(),"MAC of connected device:" + MAC,Toast.LENGTH_LONG).show(); 

         MyThread = new ConnectedThread(btSocket); 
         //MyThread.start(); 

        } catch (IOException error) { 
         connection = false; 
         Toast.makeText(getApplicationContext(),"Error:" + error,Toast.LENGTH_LONG).show(); 
        } 

       }else { 
        Toast.makeText(getApplicationContext(),"didn't get MAC",Toast.LENGTH_LONG).show(); 

       } 

     } 
    } 

    private class ConnectedThread extends Thread { 
     private final BluetoothSocket btSocket2; 
     private final OutputStream OutStream; 

     public ConnectedThread(BluetoothSocket socket) { 
      btSocket2 = socket; 
      OutputStream tmpOut = null; 


      try { 
       tmpOut = socket.getOutputStream(); 
      } catch (IOException e) { } 

      OutStream = tmpOut; 
     } 


     public void write(byte bytes) { 
      try { 
       OutStream.write(bytes); 
      } catch (IOException e) { } 
     } 


     public void cancel() { 
      try { 
       btSocket2.close(); 
      } catch (IOException e) { } 
     } 
    } 



} 

答えて

0

何が問題なのですか。コードの少なくともそれらの2枚は間違っている:

try { 
    tmpOut = socket.getOutputStream(); 
} catch (IOException e) { } 

以降:

try { 
    OutStream.write(bytes); 
} catch (IOException e) { } 

何かがあなたが例外黙っと根本的な原因を特定するための緩いすべてのチャンスをキャッチうまくいかない

変更し、このようなものにコード:

try { 
    tmpOut = socket.getOutputStream(); 
} catch (IOException e) { 
    Log.(TAG, "Fail to get socket output stream" ,e); 
} 

以降:

try { 
    OutStream.write(bytes); 
} catch (IOException e) { 
    Log.(TAG, "Fail to write on socket output stream" ,e); 
} 

次に、あなたのコードを再実行して、logcatを見てください。あなたはおそらくあなたの問題の原因を見るでしょう。

+0

助けてくれてありがとうございます – N1KO

関連する問題