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

Bluetooth_code&_send_mail (1)

The document contains multiple Java code snippets demonstrating different functionalities: Bluetooth device discovery, sending emails using SMTP, sending SMS via Twilio, retrieving user location using IP, and capturing images from a camera using OpenCV. Each code snippet includes necessary imports, class definitions, and methods to perform the respective tasks. The examples are designed to be straightforward and serve as templates for developers to implement similar features in their applications.

Uploaded by

suhaskadam0011
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)
16 views8 pages

Bluetooth_code&_send_mail (1)

The document contains multiple Java code snippets demonstrating different functionalities: Bluetooth device discovery, sending emails using SMTP, sending SMS via Twilio, retrieving user location using IP, and capturing images from a camera using OpenCV. Each code snippet includes necessary imports, class definitions, and methods to perform the respective tasks. The examples are designed to be straightforward and serve as templates for developers to implement similar features in their applications.

Uploaded by

suhaskadam0011
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

Bluetooth code :-

import javax.bluetooth.*;

import javax.microedition.io.*;

import java.io.IOException;

public class BluetoothExample {

public static void main(String[] args) {

try {

final Object inquiryCompletedEvent = new Object();

DiscoveryListener listener = new DiscoveryListener() {

public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {

try {

System.out.println("Device found: " + btDevice.getFriendlyName(false));

} catch (IOException e) {

System.out.println("Device found: " + btDevice.getBluetoothAddress());

public void inquiryCompleted(int discType) {

synchronized (inquiryCompletedEvent) {

inquiryCompletedEvent.notifyAll();

public void serviceSearchCompleted(int transID, int respCode) {}

public void servicesDiscovered(int transID, ServiceRecord[] records) {}

};

DiscoveryAgent agent = LocalDevice.getLocalDevice().getDiscoveryAgent();

agent.startInquiry(DiscoveryAgent.PREKNOWN, listener);

synchronized (inquiryCompletedEvent) {

inquiryCompletedEvent.wait();

} catch (BluetoothStateException | InterruptedException e) {

e.printStackTrace(); }}}

1
send email code :-
import java.util.Properties;

import javax.mail.*;

import javax.mail.internet.*;

public class SendEmail {

private static final String SMTP_HOST = "smtp.gmail.com";

private static final String SMTP_PORT = "587";

private static final String USERNAME = "[email protected]";

private static final String PASSWORD = "your_email_password_or_app_password";

public static void sendEmail(String toEmail, String subject, String body) {

Properties props = new Properties();

props.put("mail.smtp.auth", "true");

props.put("mail.smtp.starttls.enable", "true");

props.put("mail.smtp.host", SMTP_HOST);

props.put("mail.smtp.port", SMTP_PORT);

Session session = Session.getInstance(props, new Authenticator() {

protected PasswordAuthentication getPasswordAuthentication() {

return new PasswordAuthentication(USERNAME, PASSWORD);

});

try {

Message message = new MimeMessage(session);

message.setFrom(new InternetAddress(USERNAME));

message.setRecipients(

Message.RecipientType.TO, InternetAddress.parse(toEmail));

message.setSubject(subject);

message.setText(body);

Transport.send(message);

System.out.println("Email sent successfully to " + toEmail);

} catch (MessagingException e) {

e.printStackTrace();

2
System.err.println("Error sending email: " + e.getMessage());

public static void main(String[] args) {

// Replace with recipient email, subject and message body

String toEmail = "[email protected]";

String subject = "Test Email from Java";

String body = "Hello!\nThis is a simple email sent from Java.";

sendEmail(toEmail, subject, body);

</content>

</create_file>

3
Sms code :-
import com.twilio.Twilio;

import com.twilio.rest.api.v2010.account.Message;

import com.twilio.type.PhoneNumber;

public class SendSms {

// Replace these with your Twilio account details

public static final String ACCOUNT_SID = "YOUR_ACCOUNT_SID";

public static final String AUTH_TOKEN = "YOUR_AUTH_TOKEN";

public static void main(String[] args) {

// Initialize the Twilio client

Twilio.init(ACCOUNT_SID, AUTH_TOKEN);

// Your Twilio phone number (or sender ID)

String fromPhone = "+12345678901"; // Replace with your Twilio number

// Recipient phone number

String toPhone = "+10987654321"; // Replace with recipient's phone number

// Message to send

String messageBody = "Hello from Java via Twilio!";

// Send the SMS message

Message message = Message.creator(

new PhoneNumber(toPhone),

new PhoneNumber(fromPhone),

messageBody)

.create();

// Print the message SID (for reference)

System.out.println("SMS sent successfully. Message SID: " + message.getSid());

</content> </create_file>

4
Current location code :-
import java.awt.HeadlessException;

import javax.swing.JFrame;

import javax.swing.JLabel;

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.net.HttpURLConnection;

import java.net.URL;

import org.json.JSONObject;

public class UserLocation extends JFrame {

public UserLocation() throws HeadlessException {

setTitle("User Current Location");

setSize(400, 200);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setLocationRelativeTo(null);

String location = getUserLocation();

JLabel label = new JLabel("<html>Your Approximate Location:<br>" + location + "</html>");

add(label);

public String getUserLocation() {

String result = "Location not available";

try {

// Use ip-api.com to fetch user location based on IP

String urlStr = "http://ip-api.com/json";

URL url = new URL(urlStr);

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setRequestMethod("GET");

BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));

5
String inputLine;

StringBuilder response = new StringBuilder();

while ((inputLine = in.readLine()) != null) {

response.append(inputLine);

in.close();

JSONObject json = new JSONObject(response.toString());

if ("success".equals(json.getString("status"))) {

result = "City: " + json.getString("city") + ", Region: " + json.getString("regionName") +

", Country: " + json.getString("country") + "<br>Latitude: " + json.getDouble("lat") +

", Longitude: " + json.getDouble("lon");

} catch (Exception e) {

result = "Error retrieving location";

return result;

public static void main(String[] args) {

javax.swing.SwingUtilities.invokeLater(() -> {

UserLocation app = new UserLocation();

app.setVisible(true);

});

</content>

</create_file>

6
Camera code :-
import org.opencv.core.Core;

import org.opencv.core.Mat;

import org.opencv.highgui.HighGui;

import org.opencv.videoio.VideoCapture;

public class CameraCapture {

static { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); }

public static void main(String[] args) {

VideoCapture camera = new VideoCapture(0); // 0 is the default camera

Mat frame = new Mat();

if (!camera.isOpened()) {

System.out.println("Error: Camera is not available.");

return;

camera.read(frame); // Capture a frame

if (!frame.empty()) {

HighGui.imshow("Captured Image", frame); // Display the captured image

HighGui.waitKey(0); // Wait for a key press

} else {

System.out.println("Error: No frame captured.");

camera.release(); // Release the camera

7
8

You might also like