Android Communicate Amongst Arduino + Hc-06 Bluetooth Module, Purpose Ii

Posted on

It’s my erstwhile ship service “Android instance to communicate amongst Bluetooth device, HC-06 Bluetooth Module“, to brand a Android band communicate amongst Arduino + HC-06 Bluetooth Module. In the example, the starting fourth dimension byte e’er lost.

After to a greater extent than test, I constitute that the starting fourth dimension byte non lost. Because the byte flow sent from Arduino non inward sync amongst Android side, as well as the bytes may live separated inward unlike slot received inward Android ThreadConnected, as well as the erstwhile slot covered yesteryear the novel slot.

To exhibit it, I alteration it non to clear the TextView.

/* Android Example to connect to as well as communicate amongst Bluetooth In this exercise, the target is a Arduino Due + HC-06 (Bluetooth Module)  Ref: - Make BlueTooth connector betwixt Android devices   - Bluetooth communication betwixt Android devices    */ parcel com.example.androidbtcontrol;  import; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothSocket; import android.content.Intent; import; import android.os.Bundle; import; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast;  import; import; import; import java.util.ArrayList; import java.util.Set; import java.util.UUID;  world degree MainActivity extends ActionBarActivity {      person static lastly int REQUEST_ENABLE_BT = 1;      BluetoothAdapter bluetoothAdapter;      ArrayList<BluetoothDevice> pairedDeviceArrayList;      TextView textInfo, textStatus, textByteCnt;     ListView listViewPairedDevice;     LinearLayout inputPane;     EditText inputField;     Button btnSend, btnClear;      ArrayAdapter<BluetoothDevice> pairedDeviceAdapter;     person UUID myUUID;     person lastly String UUID_STRING_WELL_KNOWN_SPP =         "00001101-0000-1000-8000-00805F9B34FB";      ThreadConnectBTdevice myThreadConnectBTdevice;     ThreadConnected myThreadConnected;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          textInfo = (TextView)findViewById(;         textStatus = (TextView)findViewById(;         textByteCnt = (TextView)findViewById(;         listViewPairedDevice = (ListView)findViewById(;          inputPane = (LinearLayout)findViewById(;         inputField = (EditText)findViewById(;         btnSend = (Button)findViewById(;         btnSend.setOnClickListener(new View.OnClickListener(){              @Override             world void onClick(View v) {                 if(myThreadConnected!=null){                     byte[] bytesToSend = inputField.getText().toString().getBytes();                     myThreadConnected.write(bytesToSend);                     byte[] NewLine = "\n".getBytes();                     myThreadConnected.write(NewLine);                 }             }});          btnClear = (Button)findViewById(;         btnClear.setOnClickListener(new View.OnClickListener() {             @Override             world void onClick(View v) {                 textStatus.setText("");                 textByteCnt.setText("");             }         });          if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)){             Toast.makeText(this,                     "FEATURE_BLUETOOTH NOT support",                     Toast.LENGTH_LONG).show();             finish();             return;         }          //using the well-known SPP UUID         myUUID = UUID.fromString(UUID_STRING_WELL_KNOWN_SPP);          bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();         if (bluetoothAdapter == null) {             Toast.makeText(this,                     "Bluetooth is non supported on this hardware platform",                     Toast.LENGTH_LONG).show();             finish();             return;         }          String stInfo = bluetoothAdapter.getName() + "\n" +                 bluetoothAdapter.getAddress();         textInfo.setText(stInfo);     }      @Override     protected void onStart() {         super.onStart();          //Turn ON BlueTooth if it is OFF         if (!bluetoothAdapter.isEnabled()) {             Intent enableIntent = novel Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);             startActivityForResult(enableIntent, REQUEST_ENABLE_BT);         }          setup();     }      person void setup() {         Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();         if (pairedDevices.size() > 0) {             pairedDeviceArrayList = novel ArrayList<BluetoothDevice>();              for (BluetoothDevice device : pairedDevices) {                 pairedDeviceArrayList.add(device);             }              pairedDeviceAdapter = novel ArrayAdapter<BluetoothDevice>(this,                     android.R.layout.simple_list_item_1, pairedDeviceArrayList);             listViewPairedDevice.setAdapter(pairedDeviceAdapter);              listViewPairedDevice.setOnItemClickListener(new AdapterView.OnItemClickListener() {                  @Override                 world void onItemClick(AdapterView<?> parent, View view,                                         int position, long id) {                     BluetoothDevice device =                             (BluetoothDevice) parent.getItemAtPosition(position);                     Toast.makeText(MainActivity.this,                             "Name: " + device.getName() + "\n"                                     + "Address: " + device.getAddress() + "\n"                                     + "BondState: " + device.getBondState() + "\n"                                     + "BluetoothClass: " + device.getBluetoothClass() + "\n"                                     + "Class: " + device.getClass(),                             Toast.LENGTH_LONG).show();                      textStatus.setText("start ThreadConnectBTdevice");                     myThreadConnectBTdevice = novel ThreadConnectBTdevice(device);                     myThreadConnectBTdevice.start();                 }             });         }     }      @Override     protected void onDestroy() {         super.onDestroy();          if(myThreadConnectBTdevice!=null){             myThreadConnectBTdevice.cancel();         }     }      @Override     protected void onActivityResult(int requestCode, int resultCode, Intent data) {         if(requestCode==REQUEST_ENABLE_BT){             if(resultCode == Activity.RESULT_OK){                 setup();             }else{                 Toast.makeText(this,                         "BlueTooth NOT enabled",                         Toast.LENGTH_SHORT).show();                 finish();             }         }     }      //Called inward ThreadConnectBTdevice in ane lawsuit connect successed     //to start ThreadConnected     person void startThreadConnected(BluetoothSocket socket){          myThreadConnected = novel ThreadConnected(socket);         myThreadConnected.start();     }      /*     ThreadConnectBTdevice:     Background Thread to grip BlueTooth connecting     */     person degree ThreadConnectBTdevice extends Thread {          person BluetoothSocket bluetoothSocket = null;         person lastly BluetoothDevice bluetoothDevice;           person ThreadConnectBTdevice(BluetoothDevice device) {             bluetoothDevice = device;              endeavor {                 bluetoothSocket = device.createRfcommSocketToServiceRecord(myUUID);                 textStatus.setText("bluetoothSocket: \n" + bluetoothSocket);             } grab (IOException e) {                 // TODO Auto-generated grab block                 e.printStackTrace();             }         }          @Override         world void run() {             boolean success = false;             endeavor {                 bluetoothSocket.connect();                 success = true;             } grab (IOException e) {                 e.printStackTrace();                  lastly String eMessage = e.getMessage();                 runOnUiThread(new Runnable() {                      @Override                     world void run() {                         textStatus.setText("something incorrect bluetoothSocket.connect(): \n" + eMessage);                     }                 });                  endeavor {                     bluetoothSocket.close();                 } grab (IOException e1) {                     // TODO Auto-generated grab block                     e1.printStackTrace();                 }             }              if(success){                 //connect successful                 lastly String msgconnected = "connect successful:\n"                         + "BluetoothSocket: " + bluetoothSocket + "\n"                         + "BluetoothDevice: " + bluetoothDevice;                  runOnUiThread(new Runnable() {                      @Override                     world void run() {                         textStatus.setText("");                         textByteCnt.setText("");                         Toast.makeText(MainActivity.this, msgconnected, Toast.LENGTH_LONG).show();                          listViewPairedDevice.setVisibility(View.GONE);                         inputPane.setVisibility(View.VISIBLE);                     }                 });                  startThreadConnected(bluetoothSocket);              }else{                 //fail             }         }          world void cancel() {              Toast.makeText(getApplicationContext(),                     "close bluetoothSocket",                     Toast.LENGTH_LONG).show();              endeavor {                 bluetoothSocket.close();             } grab (IOException e) {                 // TODO Auto-generated grab block                 e.printStackTrace();             }          }      }      /*     ThreadConnected:     Background Thread to grip Bluetooth information communication     later connected      */     person degree ThreadConnected extends Thread {         person lastly BluetoothSocket connectedBluetoothSocket;         person lastly InputStream connectedInputStream;         person lastly OutputStream connectedOutputStream;          world ThreadConnected(BluetoothSocket socket) {             connectedBluetoothSocket = socket;             InputStream inward = null;             OutputStream out = null;              endeavor {                 inward = socket.getInputStream();                 out = socket.getOutputStream();             } grab (IOException e) {                 // TODO Auto-generated grab block                 e.printStackTrace();             }              connectedInputStream = in;             connectedOutputStream = out;         }          @Override         world void run() {             byte[] buffer = novel byte[1024];             int bytes;              String strRx = "";              spell (true) {                 endeavor {                     bytes =;                     lastly String strReceived = novel String(buffer, 0, bytes);                     lastly String strByteCnt = String.valueOf(bytes) + " bytes received.\n";                      runOnUiThread(new Runnable(){                          @Override                         world void run() {                             textStatus.append(strReceived);                             textByteCnt.append(strByteCnt);                         }});                  } grab (IOException e) {                     // TODO Auto-generated grab block                     e.printStackTrace();                      lastly String msgConnectionLost = "Connection lost:\n"                             + e.getMessage();                     runOnUiThread(new Runnable(){                          @Override                         world void run() {                             textStatus.setText(msgConnectionLost);                         }});                 }             }         }          world void write(byte[] buffer) {             endeavor {                 connectedOutputStream.write(buffer);             } grab (IOException e) {                 // TODO Auto-generated grab block                 e.printStackTrace();             }         }          world void cancel() {             endeavor {                 connectedBluetoothSocket.close();             } grab (IOException e) {                 // TODO Auto-generated grab block                 e.printStackTrace();             }         }     }  }  


<LinearLayout xmlns:android=""     xmlns:tools=""     android:layout_width="match_parent"     android:layout_height="match_parent"     android:paddingLeft="@dimen/activity_horizontal_margin"     android:paddingRight="@dimen/activity_horizontal_margin"     android:paddingTop="@dimen/activity_vertical_margin"     android:paddingBottom="@dimen/activity_vertical_margin"     android:orientation="horizontal"     tools:context=".MainActivity">      <LinearLayout         android:layout_width="0dp"         android:layout_height="match_parent"         android:layout_weight="1"         android:orientation="vertical">         <TextView             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:layout_gravity="center_horizontal"             android:autoLink="web"             android:text=""             android:textStyle="bold" />          <TextView             android:id="@+id/textbyteCnt"             android:layout_width="wrap_content"             android:layout_height="0dp"             android:layout_weight="1"/>         <TextView             android:id="@+id/status"             android:layout_width="wrap_content"             android:layout_height="0dp"             android:layout_weight="1"/>     </LinearLayout>      <LinearLayout         android:layout_width="0dp"         android:layout_height="match_parent"         android:layout_weight="1"         android:orientation="vertical">          <TextView             android:id="@+id/info"             android:textStyle="bold|italic"             android:layout_width="wrap_content"             android:layout_height="wrap_content"/>         <ListView             android:id="@+id/pairedlist"             android:layout_width="match_parent"             android:layout_height="match_parent"/>          <LinearLayout             android:id="@+id/inputpane"             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:orientation="vertical"             android:visibility="gone">              <EditText                 android:id="@+id/input"                 android:layout_width="match_parent"                 android:layout_height="wrap_content"/>             <Button                 android:id="@+id/send"                 android:layout_width="match_parent"                 android:layout_height="wrap_content"                 android:text="Sent"/>             <Button                 android:id="@+id/clear"                 android:layout_width="match_parent"                 android:layout_height="wrap_content"                 android:text="Clear"/>          </LinearLayout>      </LinearLayout>  </LinearLayout>  

Have to alteration AndroidManifest.xml to uses-permission of “android.permission.BLUETOOTH”. Refer the exmple “Android instance to communicate amongst Bluetooth device, HC-06 Bluetooth Module“.

Android instance to communicate amongst Bluetooth device Android communicate amongst Arduino + HC-06 Bluetooth Module, purpose IIDownload the files (Android Studio Format) .

Android instance to communicate amongst Bluetooth device Android communicate amongst Arduino + HC-06 Bluetooth Module, purpose IIDownload as well as endeavor APK.

For the Arduino side, cite to : Arduino-er – Connect Arduino Due amongst HC-06 (Bluetooth Module).

This Android App tin live used to beak amongst PC, amongst FTDI USB-to-Serial adapter connected to HC-06. Refer to the adjacent ship service “Android Bluetooth connect PC (running Windows10) via HC-06 BT Module as well as FTDI US-to-Serial adapter“.

Android Bluetooth Terminal to login Raspberry Pi + HC-06