2016-05-11 17 views
1

私は回路のブルートゥースを接続する必要があるプロジェクトがあります。まず、HC-06モジュールを接続し、「赤」、「緑」、「青」のようなデータを送信する必要があります。私はすでに回路をコーディングしており、Windows(C#)アプリケーションで動作しますが、Androidアプリケーションを作りたいと思っています。arduinoブルートゥースに接続

要約すると、ユーザーはアンドロイドデバイスの色を選択します。 Androidデバイスはその色の名前をHC-06モジュールに送信し、回路は適切な色のモーターを実行します。

私はそれを成功させるためにさまざまな方法を試しましたが、アンドロイドアプリケーションをシャットダウンしました。私の最後のコードはここにあります:

BluetoothAdapter bt; 

OutputStream outputStream; 
BluetoothSocket soket; 
StringBuilder sb; 


private static final UUID uuid_kodu = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 
private static String mac_adres = "00:15:FF:F2:19:5F"; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_anasayfa); 



    bt = BluetoothAdapter.getDefaultAdapter(); 


    bt_control(); 

} 

private void bt_control() { 

    if(bt==null) { 
     Toast.makeText(Anasayfa.this, "Bt doesnt supports on this device", Toast.LENGTH_SHORT).show(); 
    } else { 
     if (!bt.isEnabled()) { 

      Toast.makeText(Anasayfa.this, "Bt is opening", Toast.LENGTH_SHORT).show(); 
      //Prompt user to turn on Bluetooth 
      Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
      startActivityForResult(enableBtIntent, 1); 


     } 
    } 
} 

どうすれば問題を解決できますか?

+0

"ただし、アンドロイドアプリケーションがシャットダウンされました"というのは、問題の詳細な説明ではありません。 – JimmyB

答えて

1

これは本当に簡単なライブラリHC-06モジュールにあなたのAndroidを接続することです:あなたのGradleアプリの依存関係に追加し、それをインストールするには

https://github.com/omaflak/Bluetooth-Library

compile 'me.aflak.libraries:bluetooth:1.2.4'

これは短いサンプルです:

Bluetooth bluetooth = new Bluetooth(this); 
bluetooth.enableBluetooth(); 

bluetooth.setCommunicationCallback(new Bluetooth.CommunicationCallback() { 
     @Override 
     public void onConnect(BluetoothDevice device) { 
       // device connected 
       bluetooth.send("message"); 
     } 

     @Override 
     public void onDisconnect(BluetoothDevice device, String message) { 
      // device disconnected 
     } 

     @Override 
     public void onMessage(String message) { 
      // message received (it has to end with a \n to be received) 
     } 

     @Override 
     public void onError(String message) { 
      // error occurred 
     } 

     @Override 
     public void onConnectError(BluetoothDevice device, String message) { 
      // error during connection 
     } 
}); 

// three options 
bluetooth.connectToName("name"); 
bluetooth.connectToAddress("address"); 
bluetooth.connectToDevice(device); 
+0

ちょうど私のanwserを編集しました! –

+0

ありがとうございますが、私は 'Bluetooth bluetooth = new Bluetooth();' on onCreate – ahmstg

+0

依存関係を追加しましたか? –

関連する問題