0% found this document useful (0 votes)
4 views3 pages

code2

The document is an Android application code that implements Bluetooth functionality, allowing users to discover and connect to Bluetooth devices. It includes features for sending messages to connected devices and managing the Bluetooth adapter lifecycle. The application handles UI interactions and broadcasts for device discovery, ensuring proper resource management upon destruction.

Uploaded by

ivrhowjb
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)
4 views3 pages

code2

The document is an Android application code that implements Bluetooth functionality, allowing users to discover and connect to Bluetooth devices. It includes features for sending messages to connected devices and managing the Bluetooth adapter lifecycle. The application handles UI interactions and broadcasts for device discovery, ensuring proper resource management upon destruction.

Uploaded by

ivrhowjb
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/ 3

import android.bluetooth.

BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Set;
import java.util.UUID;

public class MainActivity extends AppCompatActivity {

private BluetoothAdapter bluetoothAdapter;


private ArrayAdapter<String> devicesAdapter;
private ArrayList<BluetoothDevice> devicesList = new ArrayList<>();
private BluetoothSocket socket;
private OutputStream outputStream;
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-
00805F9B34FB");

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

// Initialize Bluetooth adapter


bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
Toast.makeText(this, "Bluetooth not supported", Toast.LENGTH_SHORT).show();
finish();
return;
}

// Request Bluetooth enable if disabled


if (!bluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
}
// Setup UI
ListView lvDevices = findViewById(R.id.lvDevices);
devicesAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1);
lvDevices.setAdapter(devicesAdapter);

Button btnFindDevices = findViewById(R.id.btnFindDevices);


btnFindDevices.setOnClickListener(v -> findDevices());

Button btnSend = findViewById(R.id.btnSend);


EditText etMessage = findViewById(R.id.etMessage);
btnSend.setOnClickListener(v -> {
String message = etMessage.getText().toString();
if (socket != null && socket.isConnected()) {
sendMessage(message);
} else {
Toast.makeText(this, "Not connected to any device", Toast.LENGTH_SHORT).show();
}
});

// Register for broadcasts when a device is discovered


IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(receiver, filter);
}

private final BroadcastReceiver receiver = new BroadcastReceiver() {


public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device =
intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
devicesList.add(device);
devicesAdapter.add(device.getName() + "\n" + device.getAddress());
}
}
};

private void findDevices() {


devicesAdapter.clear();
devicesList.clear();

// Get paired devices


Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
devicesAdapter.add(device.getName() + "\n" + device.getAddress());
devicesList.add(device);
}
}

// Start discovery
if (bluetoothAdapter.isDiscovering()) {
bluetoothAdapter.cancelDiscovery();
}
bluetoothAdapter.startDiscovery();
}

private void sendMessage(String message) {


try {
outputStream = socket.getOutputStream();
outputStream.write(message.getBytes());
Toast.makeText(this, "Message sent", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(this, "Error sending message", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}

@Override
protected void onDestroy() {
super.onDestroy();
// Don't forget to unregister the ACTION_FOUND receiver.
unregisterReceiver(receiver);
try {
if (socket != null) {
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

You might also like