Maintenance: Project Hub will be unavailable on Thursday 5 (9AM to 6PM CET) while we deploy critical improvements
Components and supplies
Arduino Pro Mini 328 - 5V/16MHz
Tactile Switch, Top Actuated
Buzzer Module
HC-05 Bluetooth Module
ATmega 328 P-PU
5 mm LED: Yellow
5 mm LED: Red
Resistor 220 ohm
Apps and platforms
Android Studio
EAGLE
Arduino IDE
Project description
Code
HC05_04_withBuzzer_OneSwitch.ino
arduino
Arduino Code for the project
1#include <SoftwareSerial.h> 2const int RX_PIN = 2; 3const int TX_PIN = 3; 4const int BuzzerPin = 8; 5const int ManualReset = 11; 6const int CallPin = 10; 7SoftwareSerial mySerial(RX_PIN,TX_PIN); 8char CommandChar; 9String message; 10int Lock; 11 12void setup() { 13 // put your setup code here, to run once: 14 15 mySerial.begin(9600); 16 Serial.begin(9600); 17 pinMode(13,OUTPUT); //Red LED incoming Call 18 pinMode(12,OUTPUT); //Yellow LED Normal 19 pinMode(BuzzerPin,OUTPUT); 20 digitalWrite(BuzzerPin,LOW); 21 pinMode(ManualReset,INPUT); 22 pinMode(CallPin,INPUT); 23 // noTone(BuzzerPin); 24 Lock = 0; 25 26} 27 28void loop() { 29 // put your main code here, to run repeatedly: 30 31if(mySerial.available()) 32{ 33 //delay(10); 34 CommandChar = (char)mySerial.read(); 35 if(CommandChar != "\ 36") 37 { 38 Serial.print(CommandChar); 39 if ((String)CommandChar == "P") 40 { 41 Lock = 1; 42 pinMode(BuzzerPin,OUTPUT); 43 digitalWrite(12,LOW); 44 digitalWrite(13,HIGH); 45 digitalWrite(BuzzerPin,HIGH); 46 //Serial.println("Received P"); 47 } 48 if (((String)CommandChar =="R") ) 49 { 50 Lock = 0; 51 digitalWrite(13, LOW); 52 digitalWrite(12,HIGH); 53 digitalWrite(BuzzerPin,LOW); 54 } 55 if ((String)CommandChar == "C") 56 { 57 mySerial.print("Call*"); 58 } 59 } 60 else 61 { 62 Serial.println(CommandChar); 63 } 64}//End of mySerial loop 65 66if ((digitalRead(ManualReset) == 1) && (digitalRead(12)==HIGH)) 67{ 68// Lock = 0; 69// digitalWrite(13, LOW); 70// digitalWrite(12,HIGH); 71// pinMode(BuzzerPin,INPUT); 72 mySerial.print("Call*"); 73 Serial.println("called from box"); 74} 75 76if ((digitalRead(ManualReset) == 1) && (digitalRead(12)==LOW)) 77{ 78 Lock = 0; 79 digitalWrite(13, LOW); 80 digitalWrite(12,HIGH); 81 pinMode(BuzzerPin,INPUT); 82 mySerial.print("Reset*"); 83 Serial.println("Reset from box"); 84} 85 86if (digitalRead(CallPin) ==1) 87{ 88 mySerial.print("Call*"); 89} 90 91if (Lock == 1) 92 { 93 digitalWrite(13,HIGH); 94 digitalWrite(BuzzerPin,HIGH); 95 delay(300); 96 digitalWrite(13,LOW); 97 digitalWrite(BuzzerPin,LOW); 98 // noTone(BuzzerPin); 99 delay(300); 100 } 101 102 103if (Lock == 0) 104{ 105 digitalWrite(13,LOW); 106 digitalWrite(12,HIGH); 107 pinMode(BuzzerPin,INPUT); 108} 109Serial.println(digitalRead(ManualReset)); 110mySerial.print("Hello*"); 111//mySerial.println(); 112 113delay(2000); 114 115} 116 117 118 119 120 121
MainActivity.java
java
This is the main activity for the android app (called myapplication)
1package com.ratnadeep.myapplication; 2 3import android.Manifest; 4import android.bluetooth.BluetoothAdapter; 5import android.bluetooth.BluetoothDevice; 6import android.bluetooth.BluetoothSocket; 7import android.content.BroadcastReceiver; 8import android.content.Context; 9import android.content.Intent; 10import android.content.IntentFilter; 11import android.content.SharedPreferences; 12import android.content.pm.PackageManager; 13import android.graphics.Color; 14import android.net.Uri; 15import android.os.Handler; 16import android.os.Message; 17import android.support.v4.app.ActivityCompat; 18import android.support.v4.content.ContextCompat; 19import android.support.v7.app.AppCompatActivity; 20import android.os.Bundle; 21import android.telephony.PhoneStateListener; 22import android.telephony.TelephonyManager; 23import android.util.Log; 24import android.view.Menu; 25import android.view.MenuInflater; 26import android.view.MenuItem; 27import android.view.View; 28import android.widget.Button; 29import android.widget.CompoundButton; 30import android.widget.EditText; 31import android.widget.TextView; 32import android.widget.Toast; 33import android.widget.ToggleButton; 34 35import java.io.IOException; 36import java.io.InputStream; 37import java.io.OutputStream; 38import java.util.Set; 39import java.util.UUID; 40 41 42public class MainActivity extends AppCompatActivity { 43 Button btnSend, BtnArm, BtnReset; 44 ToggleButton tglBtnConnect, tglBtnTest; 45 TextView tvStatus, ETReceivedTxt; 46 EditText etMsgBox; 47 BluetoothAdapter btAdapter; 48 BluetoothDevice btDevice; 49 public BluetoothSocket socket; 50 Set<BluetoothDevice> mPairedDevices; 51 OutputStream outputStream; 52 InputStream inputStream; 53 SendRecieve sendRecieve; 54 private final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); 55 private final int MESSAGE_SENT = 10; 56 private final int BLUE_TOOTH_OFF = 11; 57 private final int MESSAGE_RECEIVED = 12; 58 private final int SOCKET_NOT_CONNECTED = 13; 59 private final int SOCKET_OPENED = 1; 60 public static boolean cont = false; 61 int MY_APP_PERMISSION_CALL_PHONE; 62 Boolean canCall = false; // cannot call without permissions 63 SharedPreferences sharedPreferences ; 64 @Override 65 protected void onCreate(Bundle savedInstanceState) { 66 super.onCreate(savedInstanceState); 67 setContentView(R.layout.activity_main); 68 findViewbyIDs(); 69 RequestPermission(); 70 71 72 73 if (ContextCompat.checkSelfPermission(MainActivity.this, 74 Manifest.permission.READ_PHONE_STATE)!= PackageManager.PERMISSION_GRANTED){ 75 ActivityCompat.requestPermissions(this, 76 new String[] {Manifest.permission.READ_PHONE_STATE},1); 77 } 78 79 tglBtnConnect.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 80 @Override 81 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 82 if(isChecked){ 83 tvStatus.setText("Connecting..."); 84 btAdapter = BluetoothAdapter.getDefaultAdapter(); 85 if (btAdapter != null) { 86 Connect myconnect = new Connect(); 87 if (cont) { 88 myconnect.start(); 89 } 90 } else { 91 Toast.makeText(MainActivity.this, "Check if Bluetooth is on", 92 Toast.LENGTH_SHORT).show(); 93 //tglBtnConnect.setChecked(false); 94 } 95 96 } 97 else { 98 btAdapter = null; 99 socket = null; 100 cont = false; 101 tvStatus.setText("Remote Device Disconnected"); 102 ETReceivedTxt.setText(""); 103 BtnArm.setBackgroundResource(android.R.drawable.btn_default); 104 } 105 106 } 107 }); 108 tglBtnTest.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 109 @Override 110 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 111 if(isChecked){ 112 if(cont){ 113 sendRecieve.SendMessage("P"); 114 } 115 else{ 116 tvStatus.setText("Caannot Continue, Check status"); 117 tglBtnTest.setChecked(false); 118 119 } 120 } 121 else { 122 if(cont) { 123 sendRecieve.SendMessage("R"); 124 125 } 126 } 127 } 128 }); 129 130 131 132 133 134 BtnArm.setOnClickListener(new View.OnClickListener() { 135 @Override 136 public void onClick(View v) { 137 138 TelephonyManager tmgr = 139 (TelephonyManager) MainActivity.this.getSystemService(Context.TELEPHONY_SERVICE); 140 if (socket != null) { 141 142 //Create new Receiver and register it 143 myReceiver myNewReceiver = new myReceiver(); 144 IntentFilter filter = new IntentFilter(); 145 filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED); 146 registerReceiver(myNewReceiver,filter); 147 148 BtnArm.setBackgroundColor(Color.RED); 149 } else { 150 Toast.makeText(MainActivity.this, "No Socket", 151 Toast.LENGTH_SHORT).show(); 152 } 153 } 154 }); 155 156 BtnReset.setOnClickListener(new View.OnClickListener() { 157 @Override 158 public void onClick(View v) { 159 String strMsg = "R" + "\ 160"; 161 //SendRecieve sendRecieve = new SendRecieve(socket); 162 sendRecieve.SendMessage(strMsg); 163 164 165 } 166 }); 167 168 169 sharedPreferences = this.getSharedPreferences(getString(R.string.file_name),MODE_PRIVATE); 170 } 171 172 173 174 private void findViewbyIDs() { 175 tglBtnConnect = (ToggleButton) findViewById(R.id.btnCnnct); 176 btnSend = (Button) findViewById(R.id.btnSend); 177 tvStatus = (TextView) findViewById(R.id.tvStatus); 178 // etMsgBox = (EditText) findViewById(R.id.MsgBox); 179 BtnArm = (Button) findViewById(R.id.btnArm); 180 tglBtnTest = (ToggleButton)findViewById(R.id.tglTestReset); 181 BtnReset = (Button) findViewById(R.id.btnReset); 182 ETReceivedTxt = (TextView) findViewById(R.id.etReceivedText); 183 184 } 185 186 private class Connect extends Thread { 187 private BluetoothSocket mmSocket; 188 189 public Connect() { 190 btAdapter = BluetoothAdapter.getDefaultAdapter(); 191 if (btAdapter.isEnabled()) { 192 btDevice = (BluetoothDevice) (btAdapter.getBondedDevices().toArray())[0]; 193 btAdapter.getRemoteDevice(String.valueOf(btDevice)); 194 cont = true; 195 } else { 196 Message message = Message.obtain(); 197 message.what = BLUE_TOOTH_OFF; 198 handler.sendMessage(message); 199 200 } 201 202 } 203 204 public void run() { 205 try { 206 mmSocket = btDevice.createInsecureRfcommSocketToServiceRecord(MY_UUID); 207 mmSocket.connect(); 208 } catch (IOException e) { 209 cont = false; 210 Message message = Message.obtain(); 211 message.what = SOCKET_NOT_CONNECTED; 212 handler.sendMessage(message); 213 e.printStackTrace(); 214 } 215 if ((mmSocket != null) && (cont)) { 216 Message message = Message.obtain(); 217 message.what = SOCKET_OPENED; 218 handler.sendMessage(message); 219 socket = mmSocket; 220 } 221 } 222 } 223 224 public class SendRecieve extends Thread { 225 226 private OutputStream outputStream; 227 private InputStream inputStream; 228 int inByte; 229 byte[] buffer = new byte[512]; 230 byte[] newBuffer = new byte[1024]; 231 int bytes, availableBytes; 232 String bufferMessage = ""; 233 char Ascii; 234 235 public SendRecieve(BluetoothSocket mySocket) { 236 try { 237 outputStream = mySocket.getOutputStream(); 238 inputStream = mySocket.getInputStream(); 239 } catch (IOException e) { 240 e.printStackTrace(); 241 } 242 243 } 244 245 public void run() { 246 bytes = 0; 247 248 while (true) { 249 try { 250 251 //bytes = inputStream.read(buffer,0,buffer.length); 252 inByte = inputStream.read(); 253 if (inByte != -1) Ascii = (char) inByte; 254 if ((inByte != 42) && (inByte != -1)) { 255 bufferMessage += (char) inByte; 256 257 Log.e("Char REceived", String.valueOf((char) inByte)); 258 } else { 259 handler.obtainMessage(MESSAGE_RECEIVED, bufferMessage.length(), -1, bufferMessage).sendToTarget(); 260 bufferMessage = ""; 261 262 } 263 264 //Log.e("REceived Buffer Length",String.valueOf(bytes)); 265 266 } catch (IOException e) { 267 e.printStackTrace(); 268 } 269 270 } 271 272 273 } 274 275 public void SendMessage(String msg) { 276 try { 277 outputStream.write(msg.getBytes()); 278 Message message = Message.obtain(); 279 message.what = MESSAGE_SENT; 280 handler.sendMessage(message); 281 } catch (IOException e) { 282 e.printStackTrace(); 283 } 284 } 285 } 286 287 288 289 public class myReceiver extends BroadcastReceiver{ 290 291 292 public myReceiver(){ 293 294 295 } 296 297 @Override 298 public void onReceive(Context context, Intent intent) { 299 300 try { 301 String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE); 302 if (state.equals("RINGING")) { 303 Toast.makeText(context, state, Toast.LENGTH_SHORT).show(); 304 sendRecieve.SendMessage("P" + "\ 305"); 306 } 307 } catch (Exception e) { 308 Log.e("Error", e.toString()); 309 } 310 } 311 312 313 314 } 315 316 317 318 319 Handler handler = new Handler(new Handler.Callback() { 320 @Override 321 public boolean handleMessage(Message msg) { 322 switch (msg.what) { 323 case (SOCKET_OPENED): 324 tvStatus.setText("Socket is open"); 325 sendRecieve = new SendRecieve(socket); 326 sendRecieve.start(); 327 break; 328 case (MESSAGE_SENT): 329 Toast.makeText(MainActivity.this, "Message Sent", 330 Toast.LENGTH_SHORT).show(); 331// etMsgBox.setText(""); 332 break; 333 case (BLUE_TOOTH_OFF): 334 Toast.makeText(MainActivity.this, "No Bluetooth", 335 Toast.LENGTH_SHORT).show(); 336 tglBtnConnect.setChecked(false); 337 tvStatus.setText("Bluetooth not connected"); 338 break; 339 case (MESSAGE_RECEIVED): 340 //byte[] readBuff = (byte[])msg.obj; 341 String readBuff = (String) msg.obj; 342 if (readBuff.equals("Call")) { 343 344 CallNumber(sharedPreferences.getString("myNumber","")); 345 } 346 if (readBuff.equals("Reset")){ 347 tglBtnTest.setChecked(false); 348 tvStatus.setText("Reset from Device"); 349 } 350 ETReceivedTxt.setText(readBuff); 351 352 break; 353 case (SOCKET_NOT_CONNECTED): 354 tvStatus.setText("Socket not Connected. Check Remote Device"); 355 tglBtnConnect.setChecked(false); 356 357 358 } 359 return false; 360 } 361 }); 362 363 public void CallNumber(String varNumber) { 364 // 365 // Toast.makeText(this,varNumber,Toast.LENGTH_SHORT).show(); 366 367 if (canCall) { 368 Intent callIntent = new Intent(Intent.ACTION_CALL); 369 callIntent.setData(Uri.parse("tel:"+ varNumber)); 370 try { 371 this.startActivity(callIntent); 372 } catch (SecurityException e) { 373 Toast.makeText(MainActivity.this, 374 "Check Permissions", Toast.LENGTH_SHORT).show(); 375 } 376 catch (Exception e){ 377 Log.d ("CallNumber",e.toString()); 378 } 379 380 } 381 382 } 383 384 public void RequestPermission() { 385 if (ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != 386 PackageManager.PERMISSION_GRANTED) { 387 ActivityCompat.requestPermissions(this, 388 new String[]{Manifest.permission.CALL_PHONE}, 389 MY_APP_PERMISSION_CALL_PHONE); 390 } else { // if permission already exists 391 392 canCall = true; 393 } 394 395 396 } 397 @Override 398 public boolean onCreateOptionsMenu (Menu menu){ 399 400 MenuInflater menuInflater = getMenuInflater(); 401 menuInflater.inflate(R.menu.main_menu,menu); 402 403 404 return true; 405 406 } 407 408 @Override 409 public boolean onOptionsItemSelected(MenuItem menuItem){ 410 Intent intent; 411 412 switch(menuItem.getItemId()){ 413 414 case(R.id.AddPhone): 415 intent = new Intent(this,AddPhone.class); 416 startActivity(intent); 417 break; 418 case(R.id.About): 419 intent = new Intent(this,About.class); 420 startActivity(intent); 421 break; 422 case(R.id.CloseApp): 423 ///this.finish(); 424 System.exit(0); 425 break; 426 427 } 428 429 return true; 430 } 431} 432 433 434 435 436
App Manifest
xml
This is the manifest file for the application
1<?xml version="1.0" encoding="utf-8"?> 2<manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.ratnadeep.myapplication"> 4 5 <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> 6 <uses-permission android:name="android.permission.BLUETOOTH" /> 7 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 8 <uses-permission android:name="android.permission.READ_PHONE_STATE" /> 9 <uses-permission android:name="android.permission.READ_CALL_LOG" /> 10 <uses-permission android:name="android.permission.CALL_PHONE" /> 11 <uses-permission android:name="android.permission.READ_PHONE_NUMBERS" /> 12 13 14 15 <application 16 android:allowBackup="true" 17 android:icon="@mipmap/rjoshi_ic_launcher1" 18 android:label="@string/app_name" 19 android:roundIcon="@mipmap/rjoshi_ic_launcher1" 20 android:supportsRtl="true" 21 android:theme="@style/AppTheme"> 22 <activity android:name=".About"></activity> 23 <activity 24 android:name=".AddPhone" 25 android:label="@string/title_activity_add_phone" 26 android:theme="@style/AppTheme.NoActionBar" /> 27 <activity android:name=".MainActivity" 28 android:launchMode="singleTop" android:configChanges="orientation|screenSize"> 29 30 <intent-filter> 31 <action android:name="android.intent.action.MAIN" /> 32 33 <category android:name="android.intent.category.LAUNCHER" /> 34 </intent-filter> 35 </activity> 36 37 38 </application> 39 40</manifest>
AddPhone.java
java
This is an activity called by MainActivity to add a phone number
1package com.ratnadeep.myapplication; 2 3import android.content.SharedPreferences; 4import android.os.Bundle; 5import android.support.design.widget.FloatingActionButton; 6import android.support.design.widget.Snackbar; 7import android.support.v7.app.AppCompatActivity; 8import android.support.v7.widget.Toolbar; 9import android.view.View; 10import android.widget.Button; 11import android.widget.EditText; 12 13public class AddPhone extends AppCompatActivity { 14 EditText PhoneNo; 15 //PreferenceEditor preferenceEditor = new PreferenceEditor(this); 16 Button SaveBtn,CancelBtn; 17 SharedPreferences sharedPreferences; 18 @Override 19 protected void onCreate(Bundle savedInstanceState) { 20 super.onCreate(savedInstanceState); 21 setContentView(R.layout.activity_add_phone); 22 PhoneNo = (EditText)findViewById(R.id.etPhoneNo); 23 SaveBtn = (Button)findViewById(R.id.btnSave); 24 CancelBtn = (Button)findViewById(R.id.btnCancel); 25 sharedPreferences = this.getSharedPreferences(getString(R.string.file_name),MODE_PRIVATE); 26 27 PhoneNo.setText(sharedPreferences.getString("myNumber","")); 28 SaveBtn.setOnClickListener(new View.OnClickListener() { 29 @Override 30 public void onClick(View v) { 31 SharedPreferences.Editor editor = sharedPreferences.edit(); 32 editor.putString("myNumber",PhoneNo.getText().toString()); 33 editor.commit(); 34 finish(); 35 } 36 }); 37 CancelBtn.setOnClickListener(new View.OnClickListener() { 38 @Override 39 public void onClick(View v) { 40 finish(); 41 } 42 }); 43 } 44 45 46} 47
MainActivity.java
java
This is the main activity for the android app (called myapplication)
1package com.ratnadeep.myapplication; 2 3import android.Manifest; 4import 5 android.bluetooth.BluetoothAdapter; 6import android.bluetooth.BluetoothDevice; 7import 8 android.bluetooth.BluetoothSocket; 9import android.content.BroadcastReceiver; 10import 11 android.content.Context; 12import android.content.Intent; 13import android.content.IntentFilter; 14import 15 android.content.SharedPreferences; 16import android.content.pm.PackageManager; 17import 18 android.graphics.Color; 19import android.net.Uri; 20import android.os.Handler; 21import 22 android.os.Message; 23import android.support.v4.app.ActivityCompat; 24import android.support.v4.content.ContextCompat; 25import 26 android.support.v7.app.AppCompatActivity; 27import android.os.Bundle; 28import 29 android.telephony.PhoneStateListener; 30import android.telephony.TelephonyManager; 31import 32 android.util.Log; 33import android.view.Menu; 34import android.view.MenuInflater; 35import 36 android.view.MenuItem; 37import android.view.View; 38import android.widget.Button; 39import 40 android.widget.CompoundButton; 41import android.widget.EditText; 42import android.widget.TextView; 43import 44 android.widget.Toast; 45import android.widget.ToggleButton; 46 47import java.io.IOException; 48import 49 java.io.InputStream; 50import java.io.OutputStream; 51import java.util.Set; 52import 53 java.util.UUID; 54 55 56public class MainActivity extends AppCompatActivity { 57 58 Button btnSend, BtnArm, BtnReset; 59 ToggleButton tglBtnConnect, tglBtnTest; 60 61 TextView tvStatus, ETReceivedTxt; 62 EditText etMsgBox; 63 BluetoothAdapter 64 btAdapter; 65 BluetoothDevice btDevice; 66 public BluetoothSocket socket; 67 68 Set<BluetoothDevice> mPairedDevices; 69 OutputStream outputStream; 70 InputStream 71 inputStream; 72 SendRecieve sendRecieve; 73 private final UUID MY_UUID = 74 UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); 75 private final 76 int MESSAGE_SENT = 10; 77 private final int BLUE_TOOTH_OFF = 11; 78 private 79 final int MESSAGE_RECEIVED = 12; 80 private final int SOCKET_NOT_CONNECTED = 81 13; 82 private final int SOCKET_OPENED = 1; 83 public static boolean cont 84 = false; 85 int MY_APP_PERMISSION_CALL_PHONE; 86 Boolean canCall = false; 87 // cannot call without permissions 88 SharedPreferences sharedPreferences ; 89 90 @Override 91 protected void onCreate(Bundle savedInstanceState) { 92 super.onCreate(savedInstanceState); 93 94 setContentView(R.layout.activity_main); 95 findViewbyIDs(); 96 97 RequestPermission(); 98 99 100 101 if (ContextCompat.checkSelfPermission(MainActivity.this, 102 103 Manifest.permission.READ_PHONE_STATE)!= PackageManager.PERMISSION_GRANTED){ 104 105 ActivityCompat.requestPermissions(this, 106 new String[] 107 {Manifest.permission.READ_PHONE_STATE},1); 108 } 109 110 tglBtnConnect.setOnCheckedChangeListener(new 111 CompoundButton.OnCheckedChangeListener() { 112 @Override 113 public 114 void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 115 if(isChecked){ 116 117 tvStatus.setText("Connecting..."); 118 btAdapter 119 = BluetoothAdapter.getDefaultAdapter(); 120 if (btAdapter != 121 null) { 122 Connect myconnect = new Connect(); 123 if 124 (cont) { 125 myconnect.start(); 126 } 127 128 } else { 129 Toast.makeText(MainActivity.this, 130 "Check if Bluetooth is on", 131 Toast.LENGTH_SHORT).show(); 132 133 //tglBtnConnect.setChecked(false); 134 } 135 136 137 } 138 else { 139 btAdapter = null; 140 141 socket = null; 142 cont = false; 143 tvStatus.setText("Remote 144 Device Disconnected"); 145 ETReceivedTxt.setText(""); 146 147 BtnArm.setBackgroundResource(android.R.drawable.btn_default); 148 149 } 150 151 } 152 }); 153 tglBtnTest.setOnCheckedChangeListener(new 154 CompoundButton.OnCheckedChangeListener() { 155 @Override 156 public 157 void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 158 if(isChecked){ 159 160 if(cont){ 161 sendRecieve.SendMessage("P"); 162 163 } 164 else{ 165 tvStatus.setText("Caannot 166 Continue, Check status"); 167 tglBtnTest.setChecked(false); 168 169 170 } 171 } 172 else { 173 if(cont) 174 { 175 sendRecieve.SendMessage("R"); 176 177 } 178 179 } 180 } 181 }); 182 183 184 185 186 187 BtnArm.setOnClickListener(new 188 View.OnClickListener() { 189 @Override 190 public void onClick(View 191 v) { 192 193 TelephonyManager tmgr = 194 (TelephonyManager) 195 MainActivity.this.getSystemService(Context.TELEPHONY_SERVICE); 196 if 197 (socket != null) { 198 199 //Create new Receiver and register 200 it 201 myReceiver myNewReceiver = new myReceiver(); 202 IntentFilter 203 filter = new IntentFilter(); 204 filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED); 205 206 registerReceiver(myNewReceiver,filter); 207 208 BtnArm.setBackgroundColor(Color.RED); 209 210 } else { 211 Toast.makeText(MainActivity.this, 212 "No Socket", 213 Toast.LENGTH_SHORT).show(); 214 } 215 216 } 217 }); 218 219 BtnReset.setOnClickListener(new View.OnClickListener() 220 { 221 @Override 222 public void onClick(View v) { 223 String 224 strMsg = "R" + "\ 225"; 226 //SendRecieve sendRecieve = new SendRecieve(socket); 227 228 sendRecieve.SendMessage(strMsg); 229 230 231 } 232 }); 233 234 235 236 sharedPreferences = this.getSharedPreferences(getString(R.string.file_name),MODE_PRIVATE); 237 238 } 239 240 241 242 private void findViewbyIDs() { 243 tglBtnConnect 244 = (ToggleButton) findViewById(R.id.btnCnnct); 245 btnSend = (Button) findViewById(R.id.btnSend); 246 247 tvStatus = (TextView) findViewById(R.id.tvStatus); 248 // etMsgBox 249 = (EditText) findViewById(R.id.MsgBox); 250 BtnArm = (Button) findViewById(R.id.btnArm); 251 252 tglBtnTest = (ToggleButton)findViewById(R.id.tglTestReset); 253 BtnReset 254 = (Button) findViewById(R.id.btnReset); 255 ETReceivedTxt = (TextView) findViewById(R.id.etReceivedText); 256 257 258 } 259 260 private class Connect extends Thread { 261 private BluetoothSocket 262 mmSocket; 263 264 public Connect() { 265 btAdapter = BluetoothAdapter.getDefaultAdapter(); 266 267 if (btAdapter.isEnabled()) { 268 btDevice = (BluetoothDevice) 269 (btAdapter.getBondedDevices().toArray())[0]; 270 btAdapter.getRemoteDevice(String.valueOf(btDevice)); 271 272 cont = true; 273 } else { 274 Message 275 message = Message.obtain(); 276 message.what = BLUE_TOOTH_OFF; 277 278 handler.sendMessage(message); 279 280 } 281 282 } 283 284 285 public void run() { 286 try { 287 mmSocket = btDevice.createInsecureRfcommSocketToServiceRecord(MY_UUID); 288 289 mmSocket.connect(); 290 } catch (IOException e) { 291 292 cont = false; 293 Message message = Message.obtain(); 294 295 message.what = SOCKET_NOT_CONNECTED; 296 handler.sendMessage(message); 297 298 e.printStackTrace(); 299 } 300 if ((mmSocket 301 != null) && (cont)) { 302 Message message = Message.obtain(); 303 304 message.what = SOCKET_OPENED; 305 handler.sendMessage(message); 306 307 socket = mmSocket; 308 } 309 } 310 } 311 312 313 public class SendRecieve extends Thread { 314 315 private OutputStream 316 outputStream; 317 private InputStream inputStream; 318 int inByte; 319 320 byte[] buffer = new byte[512]; 321 byte[] newBuffer = new byte[1024]; 322 323 int bytes, availableBytes; 324 String bufferMessage = ""; 325 char 326 Ascii; 327 328 public SendRecieve(BluetoothSocket mySocket) { 329 try 330 { 331 outputStream = mySocket.getOutputStream(); 332 inputStream 333 = mySocket.getInputStream(); 334 } catch (IOException e) { 335 e.printStackTrace(); 336 337 } 338 339 } 340 341 public void run() { 342 bytes 343 = 0; 344 345 while (true) { 346 try { 347 348 //bytes 349 = inputStream.read(buffer,0,buffer.length); 350 inByte = inputStream.read(); 351 352 if (inByte != -1) Ascii = (char) inByte; 353 if 354 ((inByte != 42) && (inByte != -1)) { 355 bufferMessage += 356 (char) inByte; 357 358 Log.e("Char REceived", String.valueOf((char) 359 inByte)); 360 } else { 361 handler.obtainMessage(MESSAGE_RECEIVED, 362 bufferMessage.length(), -1, bufferMessage).sendToTarget(); 363 bufferMessage 364 = ""; 365 366 } 367 368 //Log.e("REceived 369 Buffer Length",String.valueOf(bytes)); 370 371 } catch (IOException 372 e) { 373 e.printStackTrace(); 374 } 375 376 } 377 378 379 380 } 381 382 public void SendMessage(String msg) { 383 try 384 { 385 outputStream.write(msg.getBytes()); 386 Message 387 message = Message.obtain(); 388 message.what = MESSAGE_SENT; 389 390 handler.sendMessage(message); 391 } catch (IOException 392 e) { 393 e.printStackTrace(); 394 } 395 } 396 } 397 398 399 400 401 public class myReceiver extends BroadcastReceiver{ 402 403 404 public 405 myReceiver(){ 406 407 408 } 409 410 @Override 411 public void 412 onReceive(Context context, Intent intent) { 413 414 try { 415 String 416 state = intent.getStringExtra(TelephonyManager.EXTRA_STATE); 417 if (state.equals("RINGING")) 418 { 419 Toast.makeText(context, state, Toast.LENGTH_SHORT).show(); 420 421 sendRecieve.SendMessage("P" + "\ 422"); 423 } 424 } 425 catch (Exception e) { 426 Log.e("Error", e.toString()); 427 } 428 429 } 430 431 432 433 } 434 435 436 437 438 Handler handler = new Handler(new 439 Handler.Callback() { 440 @Override 441 public boolean handleMessage(Message 442 msg) { 443 switch (msg.what) { 444 case (SOCKET_OPENED): 445 446 tvStatus.setText("Socket is open"); 447 sendRecieve 448 = new SendRecieve(socket); 449 sendRecieve.start(); 450 break; 451 452 case (MESSAGE_SENT): 453 Toast.makeText(MainActivity.this, 454 "Message Sent", 455 Toast.LENGTH_SHORT).show(); 456// 457 etMsgBox.setText(""); 458 break; 459 case 460 (BLUE_TOOTH_OFF): 461 Toast.makeText(MainActivity.this, "No 462 Bluetooth", 463 Toast.LENGTH_SHORT).show(); 464 tglBtnConnect.setChecked(false); 465 466 tvStatus.setText("Bluetooth not connected"); 467 break; 468 469 case (MESSAGE_RECEIVED): 470 //byte[] readBuff 471 = (byte[])msg.obj; 472 String readBuff = (String) msg.obj; 473 474 if (readBuff.equals("Call")) { 475 476 CallNumber(sharedPreferences.getString("myNumber","")); 477 478 } 479 if (readBuff.equals("Reset")){ 480 481 tglBtnTest.setChecked(false); 482 tvStatus.setText("Reset 483 from Device"); 484 } 485 ETReceivedTxt.setText(readBuff); 486 487 488 break; 489 case (SOCKET_NOT_CONNECTED): 490 tvStatus.setText("Socket 491 not Connected. Check Remote Device"); 492 tglBtnConnect.setChecked(false); 493 494 495 496 } 497 return false; 498 } 499 }); 500 501 public 502 void CallNumber(String varNumber) { 503 // 504 // Toast.makeText(this,varNumber,Toast.LENGTH_SHORT).show(); 505 506 507 if (canCall) { 508 Intent callIntent = new Intent(Intent.ACTION_CALL); 509 510 callIntent.setData(Uri.parse("tel:"+ varNumber)); 511 try 512 { 513 this.startActivity(callIntent); 514 } catch (SecurityException 515 e) { 516 Toast.makeText(MainActivity.this, 517 "Check 518 Permissions", Toast.LENGTH_SHORT).show(); 519 } 520 catch 521 (Exception e){ 522 Log.d ("CallNumber",e.toString()); 523 } 524 525 526 } 527 528 } 529 530 public void RequestPermission() { 531 if 532 (ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != 533 534 PackageManager.PERMISSION_GRANTED) { 535 ActivityCompat.requestPermissions(this, 536 537 new String[]{Manifest.permission.CALL_PHONE}, 538 MY_APP_PERMISSION_CALL_PHONE); 539 540 } else { // if permission already exists 541 542 canCall = true; 543 544 } 545 546 547 } 548 @Override 549 public boolean onCreateOptionsMenu 550 (Menu menu){ 551 552 MenuInflater menuInflater = getMenuInflater(); 553 menuInflater.inflate(R.menu.main_menu,menu); 554 555 556 557 return true; 558 559 } 560 561 @Override 562 public boolean onOptionsItemSelected(MenuItem 563 menuItem){ 564 Intent intent; 565 566 switch(menuItem.getItemId()){ 567 568 569 case(R.id.AddPhone): 570 intent = new Intent(this,AddPhone.class); 571 572 startActivity(intent); 573 break; 574 case(R.id.About): 575 576 intent = new Intent(this,About.class); 577 startActivity(intent); 578 579 break; 580 case(R.id.CloseApp): 581 ///this.finish(); 582 583 System.exit(0); 584 break; 585 586 } 587 588 589 return true; 590 } 591} 592 593 594 595 596
HC05_04_withBuzzer_OneSwitch.ino
arduino
Arduino Code for the project
1#include <SoftwareSerial.h> 2const int RX_PIN = 2; 3const int TX_PIN 4 = 3; 5const int BuzzerPin = 8; 6const int ManualReset = 11; 7const int CallPin 8 = 10; 9SoftwareSerial mySerial(RX_PIN,TX_PIN); 10char CommandChar; 11String 12 message; 13int Lock; 14 15void setup() { 16 // put your setup code here, to 17 run once: 18 19 mySerial.begin(9600); 20 Serial.begin(9600); 21 pinMode(13,OUTPUT); 22 //Red LED incoming Call 23 pinMode(12,OUTPUT); //Yellow LED Normal 24 pinMode(BuzzerPin,OUTPUT); 25 26 digitalWrite(BuzzerPin,LOW); 27 pinMode(ManualReset,INPUT); 28 pinMode(CallPin,INPUT); 29 30 // noTone(BuzzerPin); 31 32 Lock = 0; 33 34} 35 36void loop() { 37 // put your main code here, to run 38 repeatedly: 39 40if(mySerial.available()) 41{ 42 //delay(10); 43 CommandChar 44 = (char)mySerial.read(); 45 if(CommandChar != "\ 46") 47 { 48 Serial.print(CommandChar); 49 50 if ((String)CommandChar == "P") 51 { 52 Lock = 1; 53 pinMode(BuzzerPin,OUTPUT); 54 55 digitalWrite(12,LOW); 56 digitalWrite(13,HIGH); 57 digitalWrite(BuzzerPin,HIGH); 58 59 //Serial.println("Received P"); 60 } 61 if (((String)CommandChar 62 =="R") ) 63 { 64 Lock = 0; 65 digitalWrite(13, LOW); 66 digitalWrite(12,HIGH); 67 68 digitalWrite(BuzzerPin,LOW); 69 } 70 if ((String)CommandChar 71 == "C") 72 { 73 mySerial.print("Call*"); 74 } 75 } 76 else 77 78 { 79 Serial.println(CommandChar); 80 } 81}//End of mySerial loop 82 83if 84 ((digitalRead(ManualReset) == 1) && (digitalRead(12)==HIGH)) 85{ 86// Lock 87 = 0; 88// digitalWrite(13, LOW); 89// digitalWrite(12,HIGH); 90// pinMode(BuzzerPin,INPUT); 91 92 mySerial.print("Call*"); 93 Serial.println("called from box"); 94} 95 96if 97 ((digitalRead(ManualReset) == 1) && (digitalRead(12)==LOW)) 98{ 99 Lock = 100 0; 101 digitalWrite(13, LOW); 102 digitalWrite(12,HIGH); 103 pinMode(BuzzerPin,INPUT); 104 105 mySerial.print("Reset*"); 106 Serial.println("Reset from box"); 107} 108 109if 110 (digitalRead(CallPin) ==1) 111{ 112 mySerial.print("Call*"); 113} 114 115if (Lock 116 == 1) 117 { 118 digitalWrite(13,HIGH); 119 digitalWrite(BuzzerPin,HIGH); 120 121 delay(300); 122 digitalWrite(13,LOW); 123 digitalWrite(BuzzerPin,LOW); 124 125 // noTone(BuzzerPin); 126 delay(300); 127 } 128 129 130if (Lock == 0) 131{ 132 133 digitalWrite(13,LOW); 134 digitalWrite(12,HIGH); 135 pinMode(BuzzerPin,INPUT); 136} 137Serial.println(digitalRead(ManualReset)); 138mySerial.print("Hello*"); 139//mySerial.println(); 140 141delay(2000); 142 143} 144 145 146 147 148 149 150
activity_main.xml
xml
layout for the MainActivity
1<?xml version="1.0" encoding="utf-8"?> 2<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context=".MainActivity"> 8 9 <TextView 10 android:id="@+id/tvStatus" 11 android:layout_width="wrap_content" 12 android:layout_height="wrap_content" 13 android:text="Not Connected" 14 app:layout_constraintBottom_toBottomOf="parent" 15 app:layout_constraintLeft_toLeftOf="parent" 16 app:layout_constraintRight_toRightOf="parent" 17 app:layout_constraintTop_toTopOf="parent" /> 18 19 <ToggleButton 20 android:id="@+id/btnCnnct" 21 android:layout_width="wrap_content" 22 android:layout_height="wrap_content" 23 android:layout_marginStart="8dp" 24 android:layout_marginLeft="8dp" 25 android:layout_marginTop="8dp" 26 android:textOff="Connect" 27 android:textOn="Disconnect" 28 app:layout_constraintStart_toStartOf="parent" 29 app:layout_constraintTop_toTopOf="parent" /> 30 31 <Button 32 android:id="@+id/btnSend" 33 android:layout_width="wrap_content" 34 android:layout_height="wrap_content" 35 android:layout_marginStart="8dp" 36 android:layout_marginLeft="8dp" 37 android:layout_marginTop="32dp" 38 android:text="Test" 39 app:layout_constraintStart_toStartOf="parent" 40 app:layout_constraintTop_toBottomOf="@+id/btnCnnct" /> 41 42 <!--<EditText 43 android:id="@+id/MsgBox" 44 android:layout_width="wrap_content" 45 android:layout_height="wrap_content" 46 android:layout_marginStart="8dp" 47 android:layout_marginLeft="8dp" 48 android:layout_marginTop="8dp" 49 android:ems="10" 50 android:inputType="textPersonName" 51 app:layout_constraintStart_toStartOf="parent" 52 app:layout_constraintTop_toBottomOf="@+id/btnCnnct" />--> 53 54 <Button 55 android:id="@+id/btnArm" 56 android:layout_width="wrap_content" 57 android:layout_height="wrap_content" 58 android:layout_marginTop="8dp" 59 android:layout_marginEnd="8dp" 60 android:layout_marginRight="8dp" 61 android:text="Arm" 62 app:layout_constraintEnd_toEndOf="parent" 63 app:layout_constraintTop_toTopOf="parent" /> 64 65 <Button 66 android:id="@+id/btnReset" 67 android:layout_width="wrap_content" 68 android:layout_height="wrap_content" 69 android:layout_marginStart="16dp" 70 android:layout_marginLeft="16dp" 71 android:layout_marginTop="32dp" 72 android:text="Reset" 73 app:layout_constraintStart_toEndOf="@+id/btnSend" 74 app:layout_constraintTop_toBottomOf="@+id/btnArm" /> 75 76 <TextView 77 android:id="@+id/etReceivedText" 78 android:layout_width="wrap_content" 79 android:layout_height="wrap_content" 80 android:layout_marginStart="8dp" 81 android:layout_marginLeft="8dp" 82 android:layout_marginTop="8dp" 83 android:layout_marginEnd="8dp" 84 android:layout_marginRight="8dp" 85 android:ems="10" 86 android:inputType="textPersonName" 87 app:layout_constraintEnd_toEndOf="parent" 88 app:layout_constraintStart_toStartOf="parent" 89 app:layout_constraintTop_toBottomOf="@+id/tvStatus" /> 90 91 <ToggleButton 92 android:id="@+id/tglTestReset" 93 android:layout_width="wrap_content" 94 android:layout_height="wrap_content" 95 android:layout_marginStart="8dp" 96 android:layout_marginLeft="8dp" 97 android:layout_marginTop="8dp" 98 android:textOff="Test" 99 android:textOn="Reset" 100 app:layout_constraintStart_toEndOf="@+id/btnCnnct" 101 app:layout_constraintTop_toTopOf="parent" /> 102 103 <!--<ToggleButton 104 android:id="@+id/toggleButton" 105 android:layout_width="wrap_content" 106 android:layout_height="wrap_content" 107 android:layout_marginStart="104dp" 108 android:layout_marginLeft="104dp" 109 android:layout_marginTop="8dp" 110 android:text="ToggleButton" 111 app:layout_constraintStart_toStartOf="parent" 112 app:layout_constraintTop_toBottomOf="@+id/btnCnnct" />--> 113 114</android.support.constraint.ConstraintLayout>
App Manifest
xml
This is the manifest file for the application
1<?xml version="1.0" encoding="utf-8"?> 2<manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.ratnadeep.myapplication"> 4 5 <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> 6 <uses-permission android:name="android.permission.BLUETOOTH" /> 7 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 8 <uses-permission android:name="android.permission.READ_PHONE_STATE" /> 9 <uses-permission android:name="android.permission.READ_CALL_LOG" /> 10 <uses-permission android:name="android.permission.CALL_PHONE" /> 11 <uses-permission android:name="android.permission.READ_PHONE_NUMBERS" /> 12 13 14 15 <application 16 android:allowBackup="true" 17 android:icon="@mipmap/rjoshi_ic_launcher1" 18 android:label="@string/app_name" 19 android:roundIcon="@mipmap/rjoshi_ic_launcher1" 20 android:supportsRtl="true" 21 android:theme="@style/AppTheme"> 22 <activity android:name=".About"></activity> 23 <activity 24 android:name=".AddPhone" 25 android:label="@string/title_activity_add_phone" 26 android:theme="@style/AppTheme.NoActionBar" /> 27 <activity android:name=".MainActivity" 28 android:launchMode="singleTop" android:configChanges="orientation|screenSize"> 29 30 <intent-filter> 31 <action android:name="android.intent.action.MAIN" /> 32 33 <category android:name="android.intent.category.LAUNCHER" /> 34 </intent-filter> 35 </activity> 36 37 38 </application> 39 40</manifest>
AddPhone.java
java
This is an activity called by MainActivity to add a phone number
1package com.ratnadeep.myapplication; 2 3import android.content.SharedPreferences; 4import android.os.Bundle; 5import android.support.design.widget.FloatingActionButton; 6import android.support.design.widget.Snackbar; 7import android.support.v7.app.AppCompatActivity; 8import android.support.v7.widget.Toolbar; 9import android.view.View; 10import android.widget.Button; 11import android.widget.EditText; 12 13public class AddPhone extends AppCompatActivity { 14 EditText PhoneNo; 15 //PreferenceEditor preferenceEditor = new PreferenceEditor(this); 16 Button SaveBtn,CancelBtn; 17 SharedPreferences sharedPreferences; 18 @Override 19 protected void onCreate(Bundle savedInstanceState) { 20 super.onCreate(savedInstanceState); 21 setContentView(R.layout.activity_add_phone); 22 PhoneNo = (EditText)findViewById(R.id.etPhoneNo); 23 SaveBtn = (Button)findViewById(R.id.btnSave); 24 CancelBtn = (Button)findViewById(R.id.btnCancel); 25 sharedPreferences = this.getSharedPreferences(getString(R.string.file_name),MODE_PRIVATE); 26 27 PhoneNo.setText(sharedPreferences.getString("myNumber","")); 28 SaveBtn.setOnClickListener(new View.OnClickListener() { 29 @Override 30 public void onClick(View v) { 31 SharedPreferences.Editor editor = sharedPreferences.edit(); 32 editor.putString("myNumber",PhoneNo.getText().toString()); 33 editor.commit(); 34 finish(); 35 } 36 }); 37 CancelBtn.setOnClickListener(new View.OnClickListener() { 38 @Override 39 public void onClick(View v) { 40 finish(); 41 } 42 }); 43 } 44 45 46} 47
Downloadable files
Basic Sketch
Basic Sketch
Basic Sketch
Basic Sketch
Comments
Only logged in users can leave comments
ratnaddeepjoshi
0 Followers
•0 Projects
Table of contents
Intro
1
0