আগের পর্বে আমারা যতদূর করেছি তার ফলে এ রকম একটা ডিসপ্লে তৈরি হয়
এখন আসল কাজে আসা যাক।
বাম পাসের টুলবার থেকে জাভা এর ভিতর তিনটি জাভা ফাইল তৈরি করি যার একটা করা আছে…
১.MainActivity.java
২.ListDivices.java
৩.HomeAutomationActivity.java
এবার ফাইল তিনটিতে নিচের মত করে কোড গুলো লিখে দি।।
****MainActivity.java package com.homeautomation; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Set; import java.util.UUID; 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.text.Editable; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.Window; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import android.widget.ToggleButton; publicclass MainActivity extends Activity { BluetoothAdapter mBluetoothAdapter; BluetoothSocket mmSocket; BluetoothDevice mmDevice; OutputStream mmOutputStream; InputStream mmInputStream; Editable nameb; String blname; intstat; finalintAPPLY_ACTIVE = 1; finalintAPPLY_CONNECTION = 2; String DEVICE = null; EditText name; @Override protectedvoid onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.activity_main); getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.actionbar); name = (EditText) (findViewById(R.id.bluetoothName)); Button ok = (Button) (findViewById(R.id.button1)); Button connect = (Button) findViewById(R.id.button2); ok.setOnClickListener(new View.OnClickListener() { publicvoid onClick(View v) { // TODO Auto-generated method stub Intent openList = new Intent(MainActivity.this, ListDivices.class); startActivityForResult(openList, APPLY_CONNECTION); } }); connect.setOnClickListener(new View.OnClickListener() { @Override publicvoid onClick(View v) { // TODO Auto-generated method stub nameb = name.getText(); blname = nameb.toString(); if (!blname.equals("")) { try { findBT(); stat = 1; } catch (IOException e) { stat = 0; e.printStackTrace(); } } else { Toast.makeText(getApplicationContext(), "First add your device then press connect", Toast.LENGTH_SHORT).show(); } } }); final ToggleButton fan = (ToggleButton) findViewById(R.id.toggleButton1); fan.setOnClickListener(new View.OnClickListener() { publicvoid onClick(View v) { // TODO Auto-generated method stub if (fan.isChecked()) { if (stat == 1) { try { mmOutputStream.write('1'); } catch (IOException e) { // TODO Auto-generated catch block Toast.makeText( MainActivity.this, "Connection not established with your home", Toast.LENGTH_SHORT).show(); e.printStackTrace(); } } else Toast.makeText(MainActivity.this, "Connection not established with your home", Toast.LENGTH_SHORT).show(); } else { if (stat == 1) { try { mmOutputStream.write('2'); } catch (IOException e) { // TODO Auto-generated catch block Toast.makeText( MainActivity.this, "Connection not established with your home", Toast.LENGTH_SHORT).show(); e.printStackTrace(); } } else Toast.makeText(MainActivity.this, "Connection not established with your home", Toast.LENGTH_SHORT).show(); } } }); final ToggleButton light1 = (ToggleButton) findViewById(R.id.toggleButton2); light1.setOnClickListener(new View.OnClickListener() { publicvoid onClick(View v) { // TODO Auto-generated method stub if (light1.isChecked()) { if (stat == 1) { try { mmOutputStream.write('3'); } catch (IOException e) { // TODO Auto-generated catch block Toast.makeText( MainActivity.this, "Connection not established with your home", Toast.LENGTH_SHORT).show(); e.printStackTrace(); } } else Toast.makeText(MainActivity.this, "Connection not established with your home", Toast.LENGTH_SHORT).show(); } else { if (stat == 1) { try { mmOutputStream.write('4'); } catch (IOException e) { // TODO Auto-generated catch block Toast.makeText( MainActivity.this, "Connection not established with your home", Toast.LENGTH_SHORT).show(); e.printStackTrace(); } } else Toast.makeText(MainActivity.this, "Connection not established with your home", Toast.LENGTH_SHORT).show(); } } }); final ToggleButton light2 = (ToggleButton) findViewById(R.id.toggleButton3); light2.setOnClickListener(new View.OnClickListener() { publicvoid onClick(View v) { // TODO Auto-generated method stub if (light2.isChecked()) { if (stat == 1) { try { mmOutputStream.write('5'); } catch (IOException e) { // TODO Auto-generated catch block Toast.makeText( MainActivity.this, "Connection not established with your home", Toast.LENGTH_SHORT).show(); e.printStackTrace(); } } else Toast.makeText(MainActivity.this, "Connection not established with your home", Toast.LENGTH_SHORT).show(); } else { if (stat == 1) { try { mmOutputStream.write('6'); } catch (IOException e) { // TODO Auto-generated catch block Toast.makeText( MainActivity.this, "Connection not established with your home", Toast.LENGTH_SHORT).show(); e.printStackTrace(); } } else Toast.makeText(MainActivity.this, "Connection not established with your home", Toast.LENGTH_SHORT).show(); } } }); final ToggleButton light3 = (ToggleButton) findViewById(R.id.toggleButton4); light3.setOnClickListener(new View.OnClickListener() { publicvoid onClick(View v) { // TODO Auto-generated method stub if (light3.isChecked()) { if (stat == 1) { try { mmOutputStream.write('7'); } catch (IOException e) { // TODO Auto-generated catch block Toast.makeText( MainActivity.this, "Connection not established with your home", Toast.LENGTH_SHORT).show(); e.printStackTrace(); } } else Toast.makeText(MainActivity.this, "Connection not established with your home", Toast.LENGTH_SHORT).show(); } else { if (stat == 1) { try { mmOutputStream.write('8'); } catch (IOException e) { // TODO Auto-generated catch block Toast.makeText( MainActivity.this, "Connection not established with your home", Toast.LENGTH_SHORT).show(); e.printStackTrace(); } } else Toast.makeText(MainActivity.this, "Connection not established with your home", Toast.LENGTH_SHORT).show(); } } }); final ToggleButton tv = (ToggleButton) findViewById(R.id.toggleButton5); tv.setOnClickListener(new View.OnClickListener() { publicvoid onClick(View v) { // TODO Auto-generated method stub if (tv.isChecked()) { if (stat == 1) { try { mmOutputStream.write('9'); } catch (IOException e) { // TODO Auto-generated catch block Toast.makeText( MainActivity.this, "Connection not established with your home", Toast.LENGTH_SHORT).show(); e.printStackTrace(); } } else Toast.makeText(MainActivity.this, "Connection not established with your home", Toast.LENGTH_SHORT).show(); } else { if (stat == 1) { try { mmOutputStream.write('0'); } catch (IOException e) { // TODO Auto-generated catch block Toast.makeText( MainActivity.this, "Connection not established with your home", Toast.LENGTH_SHORT).show(); e.printStackTrace(); } } else Toast.makeText(MainActivity.this, "Connection not established with your home", Toast.LENGTH_SHORT).show(); } } }); } void findBT() throws IOException { mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); // if(mBluetoothAdapter == null) // { // myLabel.setText("No bluetooth adapter available"); // } if (!mBluetoothAdapter.isEnabled()) { Intent enableBluetooth = new Intent( BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBluetooth, 1); } Set<BluetoothDevice> pairedDevices = mBluetoothAdapter .getBondedDevices(); if (pairedDevices.size() > 0) { for (BluetoothDevice device : pairedDevices) { if (device.getName().equals(blname)) // FireFly-B1A7Change to the name of your bluetooth module (Case // sensitive) { mmDevice = device; break; } } } // myLabel.setText("Bluetooth Device Found"); UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); // Standard // //SerialPortService // ID mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid); mmSocket.connect(); mmOutputStream = mmSocket.getOutputStream(); mmInputStream = mmSocket.getInputStream(); } @Override publicboolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); returntrue; } @Override publicboolean onOptionsItemSelected(MenuItem item) { super.onOptionsItemSelected(item); switch (item.getItemId()) { // Get id from R.java file case R.id.action_about: startActivity(new Intent(this, About.class)); break; } returntrue; } @Override protectedvoid onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { caseAPPLY_ACTIVE: if (resultCode == Activity.RESULT_OK) { Toast.makeText(getApplicationContext(), "Bluetooth is activated", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(getApplicationContext(), "Bluetooth is not activated", Toast.LENGTH_SHORT) .show(); finish(); } break; caseAPPLY_CONNECTION: if (resultCode == Activity.RESULT_OK) { DEVICE = data.getExtras().getString("name"); Toast.makeText(getApplicationContext(), "Device " + DEVICE + " is selected", Toast.LENGTH_SHORT) .show(); name.setText(DEVICE); } else { Toast.makeText(getApplicationContext(), "Please pair your device & select it", Toast.LENGTH_SHORT).show(); } default: break; } } }
****ListDivices.java package com.homeautomation; import java.util.Set; import android.app.ListActivity; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; publicclass ListDivices extends ListActivity { BluetoothAdapter myBluetooth; static String MAC_ADDRESS = null; String DeviceName; String list[]; @Override protectedvoid onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ArrayAdapter<String> ArrayBluetooth = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1); myBluetooth = BluetoothAdapter.getDefaultAdapter(); Set<BluetoothDevice> pairedDevices = myBluetooth.getBondedDevices(); if (pairedDevices.size() > 0) { for (BluetoothDevice paired : pairedDevices) { DeviceName = paired.getName(); ArrayBluetooth.add(DeviceName); } } setListAdapter(ArrayBluetooth); } @Override protectedvoid onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); String infoGeneral = ((TextView) v).getText().toString(); Intent returnMac = new Intent(); returnMac.putExtra("DeviceName", infoGeneral); setResult(RESULT_OK, returnMac); finish(); } }
****HomeAutomationActivity.java package com.homeautomation; import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.content.Intent; import android.os.Bundle; import android.widget.Toast; publicclass HomeAutomationActivity extends Activity { /** Called when the activity is first created. */ BluetoothAdapter mBluetoothAdapter; @Override publicvoid onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); startActivity(new Intent(this, MainActivity.class)); mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (mBluetoothAdapter == null) { Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_SHORT).show(); finish(); return; } } publicvoid onStart() { super.onStart(); if (!mBluetoothAdapter.isEnabled()) { Intent enableIntent = new Intent( BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableIntent, 3); finish(); // Otherwise, setup the chat session } } @Override protectedvoid onDestroy() { // TODO Auto-generated method stub super.onDestroy(); } @Override protectedvoid onPause() { // TODO Auto-generated method stub super.onPause(); } @Override protectedvoid onResume() { // TODO Auto-generated method stub super.onResume(); } }
main activity.java ফাইল এর ভিতর লক্ষ করিঃ
mmoutputstream.write(‘সংখ্যা’);
একটা লাইন প্রতিটা বাটন এর জন্য দুই বার করে দেয়া আছে।
এখানে fan অনের জন্য “1” এবং অফের জন্য “2” দেয়া আছে যা fan button টি তে টাচ করলে arduino তে পাঠাবে।
আপনি প্রতিটা বাটন এর জন্য এভাবে কমান্ড ঠিক করে দিবেন।
এবার রান এ ক্লিক করুণ।
*** \Android Workspace\homeautomation\app\build\outputs\ এরকম এড্রেস এ আপনি apk টা পাবেন ।
.APK ফাইল টি এই খান থেকে ডাউনলড করে নিতে পারেন
পর্ব-১
পর্ব-৩
এটা কি বাস্তবে চেক করা
হুম