2017-04-14 74 views
0

我在使用應用程序通過藍牙將我的智能手機連接到我的EV3時遇到問題。LeJOS EV3和Android之間的藍牙連接

我的情況:
我開發一個應用程序,以簡單的字符串發送到與leJOS的0.9.1-aplu11運行EV3。
我的問題是,我不太熟悉藍牙,所以我的應用程序或我的接收器軟件不能正常工作。

我的目標:
我的目標是建立在EV3這是從應用程序,當我按下一個按鈕發送字符串的應用程序字符串您永久監聽監聽器。

我希望你能幫助我。
我來自德國,所以不要懷疑我美麗的寫作技巧!

這裏是我的代碼:
應用:

公共類MainActivity擴展AppCompatActivity {

private final static String TAG = "MainActivity"; 
private BluetoothAdapter mBluetoothAdapter; 
private static BluetoothDevice mDevice; 
private Button mSendBN; 

private final static String MY_UUID = "00001101-0000-1000-8000-00805f9b34fb"; 
private static BluetoothSocket mSocket = null; 
private static String mMessage = "Stop"; 
private static PrintStream sender; 

private void findBrick() { 
    Set<BluetoothDevice> pairedDevices = mBluetoothAdapter 
      .getBondedDevices(); 
    for (BluetoothDevice device : pairedDevices) { 
     if (device.getName().equals("EV3")) 
      this.mDevice = device; 
    } 
} 

private void initBluetooth() { 
    Log.d(TAG, "Checking Bluetooth..."); 
    if (mBluetoothAdapter == null) { 
     Log.d(TAG, "Device does not support Bluetooth"); 
     mSendBN.setClickable(false); 
    } else { 
     Log.d(TAG, "Bluetooth supported"); 
    } 
    if (!mBluetoothAdapter.isEnabled()) { 
     mSendBN.setClickable(false); 
     Log.d(TAG, "Bluetooth not enabled"); 
    } else { 
     Log.d(TAG, "Bluetooth enabled"); 
    } 
} 

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    Toast.makeText(this, "SpeechRecognizer gestartet", Toast.LENGTH_SHORT).show(); 
    setContentView(R.layout.activity_main); 

    mSendBN = (Button) findViewById(R.id.button); 
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 

    initBluetooth(); 
    findBrick(); 

    if (mDevice == null) { 
     mSendBN.setClickable(false); 
     Toast.makeText(this, "No Devices found or BT disabled", Toast.LENGTH_SHORT).show(); 
     Log.d("onC", "Connected to " + mDevice); 
    } 

    try { 
     createSocket(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    startService(); 
} 

private void startService() { 
    if (PermissionHandler.checkPermission(this, PermissionHandler.RECORD_AUDIO)) { 
     Intent i = new Intent(this, BackgroundRecognizerService.class); 
     startService(i); 
    } 
} 

public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) { 
    super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
    if (requestCode == PermissionHandler.RECORD_AUDIO && grantResults.length > 0) { 
     if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
      startService(); 
     } 
    } 
} 

public static void onSend(View view) throws IOException { 
    try { 
     OutputStream os = mSocket.getOutputStream(); 
     sender = new PrintStream(os); 
     Log.d("onSend", "Message = " + mMessage); 
     sender.println(mMessage); 
     sender.flush(); 
     Log.d("onSend", "Message sent"); 
     mSocket.close(); 
     Log.d("onSend", "Socket closed"); 
    } catch (IllegalStateException | NullPointerException e) { 
     e.printStackTrace(); 
    } 

} 

public void createSocket() throws IOException { 
    try { 
     UUID uuid = UUID.fromString(MY_UUID); 
     mSocket = mDevice.createRfcommSocketToServiceRecord(uuid); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    Log.d("createSocket", "Adapter"); 

    BluetoothAdapter.getDefaultAdapter().cancelDiscovery(); 
    mSocket.connect(); 
    OutputStream os = mSocket.getOutputStream(); 
    sender = new PrintStream(os); 

    Log.d("createSocket", "Fertig, " + "Socket: " + mSocket + " Sender: " + sender + " OutputStream: " + os + " mDevice: " + mDevice.getName()); 
} 

protected void onDestroy() { 
    super.onDestroy(); 
    Log.d("onDestroy", "App beendet"); 
    try { 
     mSocket.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    Log.d("onDestroy", "App vollständig beendet"); 
} 

}

EV3:

public class BTJ { 
public static void main(String[] args) { 

    BTConnector connector = new BTConnector(); 

    System.out.println("0. Auf Signal warten"); 

    NXTConnection conn = connector.waitForConnection(0, NXTConnection.RAW); 

    InputStream is = conn.openInputStream(); 
    BufferedReader br = new BufferedReader(new InputStreamReader(is)); 

    String message = ""; 

    while (true){ 

     System.out.println("1. Schleife gestartet"); 
     message = ""; 

     try { 
      message = br.readLine(); 
      System.out.println("2. Message: " + message); 
     } catch (IOException e) { 
      e.printStackTrace(System.out); 
     } 
    } 
} 

}

+1

你到目前爲止嘗試了什麼?請張貼一些代碼。 – Aurasphere

+0

我加了我的代碼 –

+0

Can ** _ anyone _ ** help me pls ?! –

回答

0

我有答案..!

public class BTJ { 

    public static void main(String[] args) { 
     BTConnector connector = new BTConnector(); 

     System.out.println("0. Auf Signal warten"); 

     NXTConnection conn = connector.waitForConnection(0, NXTConnection.RAW); 

     InputStream is = conn.openInputStream(); 
     BufferedReader br = new BufferedReader(new InputStreamReader(is), 1); 

     String message = ""; 

     while (true){ 

      System.out.println("1. Schleife gestartet"); 
      message = ""; 

      try { 
       message = br.readLine(); 
       System.out.println("2. Message: " + message); 
      } catch (IOException e) { 
       e.printStackTrace(System.out); 

     } 
    } 
}