0% found this document useful (0 votes)
67 views8 pages

Nonnull

The document is a Java class for an Android application that manages Bluetooth communication and user data retrieval from Firebase. It includes functionality to connect to Bluetooth devices, send SMS alerts with location information, and display images in an ImageView. The app retrieves emergency contact information and geographical coordinates from Firebase and handles Bluetooth socket connections for data transmission.

Uploaded by

gabiionut
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views8 pages

Nonnull

The document is a Java class for an Android application that manages Bluetooth communication and user data retrieval from Firebase. It includes functionality to connect to Bluetooth devices, send SMS alerts with location information, and display images in an ImageView. The app retrieves emergency contact information and geographical coordinates from Firebase and handles Bluetooth socket connections for data transmission.

Uploaded by

gabiionut
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

;package com.example.android.

womensecurity6

;import android.bluetooth.BluetoothAdapter
;import android.bluetooth.BluetoothDevice
;import android.bluetooth.BluetoothSocket
;import android.content.Intent
;import android.graphics.Bitmap
;import android.graphics.BitmapFactory
;import android.os.Bundle
;import android.telephony.SmsManager
;import android.util.Log
;import android.view.View
;import android.widget.Button
;import android.widget.ImageView
;import android.widget.TextView

;import com.google.firebase.auth.FirebaseAuth
;import com.google.firebase.auth.FirebaseUser
;import com.google.firebase.database.DataSnapshot
;import com.google.firebase.database.DatabaseError
;import com.google.firebase.database.DatabaseReference
;import com.google.firebase.database.FirebaseDatabase
;import com.google.firebase.database.ValueEventListener
;import com.squareup.picasso.Picasso

;import java.io.IOException
;import java.io.InputStream
;import java.io.OutputStream
;import java.util.UUID

;import androidx.annotation.NonNull
;import androidx.appcompat.app.AppCompatActivity
{ public class CommsActivity extends AppCompatActivity
public BluetoothAdapter BTAdapter =
;)(BluetoothAdapter.getDefaultAdapter
;"private static final String TAG = "CommsActivity
;BluetoothSocket mmSocket
;BluetoothDevice mmDevice
////
;FirebaseDatabase firebaseDatabase
;DatabaseReference ref1,ref2,ref3
;FirebaseUser user
;FirebaseAuth auth
;String id
;String eContact
;Double latitude,longitude
;TextView t1,t2,t3
;ImageView imageViewCAM
////

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

;)t1=(TextView)findViewById(R.id.tvContact
;)t2=(TextView)findViewById(R.id.tvLat
;)t3=(TextView)findViewById(R.id.tvLng
;)imageViewCAM = (ImageView)findViewById(R.id.imgCamera

;)(auth=FirebaseAuth.getInstance
;)(user=auth.getCurrentUser
;)(id=user.getUid
;)(firebaseDatabase=FirebaseDatabase.getInstance
ref1
)"=firebaseDatabase.getReference("Users").child(id).child("econtact
;
ref2
;)"=firebaseDatabase.getReference("Users").child(id).child("lat
ref3
;)"=firebaseDatabase.getReference("Users").child(id).child("lng

read Econtacts//
{ )(ref1.addValueEventListener(new ValueEventListener
Override@
public void onDataChange(@NonNull DataSnapshot
{ )dataSnapshot
;)eContact = dataSnapshot.getValue(String.class
;)t1.setText(eContact
}

Override@
public void onCancelled(@NonNull DatabaseError
{ )databaseError

}
;)}
read Lat//
{ )(ref2.addValueEventListener(new ValueEventListener
Override@
public void onDataChange(@NonNull DataSnapshot
{ )dataSnapshot
;)latitude = dataSnapshot.getValue(Double.class
;)""+t2.setText(latitude
}

Override@
public void onCancelled(@NonNull DatabaseError
{ )databaseError

}
;)}
read Lng//
{ )(ref3.addValueEventListener(new ValueEventListener
Override@
public void onDataChange(@NonNull DataSnapshot
{ )dataSnapshot
;)longitude = dataSnapshot.getValue(Double.class
;)""+t3.setText(longitude
}

Override@
public void onCancelled(@NonNull DatabaseError
{ )databaseError

}
;)}
BluetoothAdapter mBluetoothAdapter =
;)(BluetoothAdapter.getDefaultAdapter
;)(final Intent intent = getIntent
final String address =
;)intent.getStringExtra(BluetoothActivity.EXTRA_ADDRESS
;)Button connect = (Button) findViewById(R.id.connectBtn

{ )(connect.setOnClickListener(new View.OnClickListener
{ )public void onClick(View v
final BluetoothDevice device =
;)BTAdapter.getRemoteDevice(address
{ try
;)(new ConnectThread(device).start
{ )catch (IOException e }
;)(e.printStackTrace
}
}
;)}

{ ))(if (!mBluetoothAdapter.isEnabled
Intent enableBluetooth = new
;)Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE
;)startActivityForResult(enableBluetooth, 0
}
}
Override@
{ )(protected void onStop
;)(super.onStop
{ try
;)(mmSocket.close
{ )catch (IOException e }
;)(e.printStackTrace
}
}

connected class //
{ public class ConnectThread extends Thread
private ConnectThread(BluetoothDevice device) throws
{ IOException
;BluetoothSocket tmp = null
;mmDevice = device
{ try
UUID uuid =
;)"UUID.fromString("94f39d29-7d6d-437d-973b-fba39e49d4ee
tmp =
;)mmDevice.createRfcommSocketToServiceRecord(uuid
{ )catch (IOException e }
;)Log.e(TAG, "Socket's create() method failed", e
}
;mmSocket = tmp
;)(BTAdapter.cancelDiscovery
{ try
;)(mmSocket.connect
{ )catch (IOException connectException }
;)"!Log.v(TAG, "Connection exception
{ try
;)(mmSocket.close
{ )catch (IOException closeException }

}
}
;)(send
}

{ public void send() throws IOException


;"String msg = "connect
OutputStream mmOutputStream =
;)(mmSocket.getOutputStream
;))(mmOutputStream.write(msg.getBytes
;)(receive
}

{ public void receive() throws IOException


;)(InputStream mmInputStream = mmSocket.getInputStream
;]byte[] buffer = new byte[8192
;int bytes
{ try
;)bytes = mmInputStream.read(buffer
String readMessage = new String(buffer, 0, bytes); // to
read message
to read an image //
*/
{ )if (bytes != -1
.ensure DATAMAXSIZE Byte is read//
;int byteNo2 = bytes
;int bufferSize = 7340
{)while(byteNo2 != bufferSize
;bufferSize = bufferSize - byteNo2
byteNo2 =
;)mmInputStream.read(buffer,bytes,bufferSize
{)if(byteNo2 == -1
;break
}
;bytes = bytes+byteNo2
}
}
/*
end of requirements to read an image ///

to load an image into an Image view ===> convert it to //


bitmap
*/
{))"if(readMessage.equals("A
;)(SmsManager sm = SmsManager.getDefault
sm.sendTextMessage(eContact, null, "I am in danger
please help me ,,, My Location is :" +
"https://www.google.com/maps/@"+latitude+","+longitude+",17z",
;)null, null
}
/*
{else//
Bitmap bm1 =
;)BitmapFactory.decodeByteArray(buffer,0,buffer.length
;)imageViewCAM.setImageBitmap(bm1

;)Picasso.get().load(readMessage).into(imageViewCAM//
} //
;)Log.d(TAG, "Received: " + readMessage
;)TextView status = (TextView) findViewById(R.id.btnState
;) status.setText("Button : "+ readMessage
;)(mmSocket.close
{ )catch (IOException e }
;)"!Log.e(TAG, "Problems occurred
;return
}
}
end of connectThread class // }

You might also like