NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

package com.example.soobash_app;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
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.bluetooth.BluetoothSocket;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

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

public class bluetooth extends AppCompatActivity {


private static final int REQUEST_ENABLE_BT = 0;
private static final int REQUEST_DISCOVER_BT = 1;

private final String DEVICE_ADDRESS = "00:18:E4:40:00:06"; //MAC Address of Bluetooth Module
private final UUID PORT_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");


//status bluetooth and paired bluetooth respectively
TextView mStatusBlue, mPairedBlue;
Button onBtn, offBtn, discoverBtn, pairedBtn, scanBtn;


// scan device manage
ListView scan_device;
ArrayList<String> stringArrayList = new ArrayList<String>();
ArrayAdapter<String> arrayAdapter;
// BluetoothAdapter


String command;
private OutputStream outputStream;
private BluetoothSocket socket;
private BluetoothDevice device;
BluetoothAdapter bluetoothAdapter= BluetoothAdapter.getDefaultAdapter();

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

mStatusBlue = findViewById(R.id.bluetooth_status); //status text field
mPairedBlue = findViewById(R.id.pairedText); //pair button
onBtn = findViewById(R.id.btnBluetoothOn); //button bluetooth on
offBtn = findViewById(R.id.btnBluetoothOff); // button bluetooth off
discoverBtn = findViewById(R.id.btnBluetoothDiscover);
pairedBtn = findViewById(R.id.btnPaired);


// scan device action
scanBtn = (Button) findViewById(R.id.btnScan);
scan_device = (ListView) findViewById(R.id.scanListDevice);

// bluetooth adapter
// bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
// System.out.println(bluetoothAdapter.getBondedDevices());


// checking if bluetooth is available or not
// if (bluetoothAdapter == null) {
// mStatusBlue.setText("bluetooth devices are not available. ".toUpperCase());
//
// } else {
// mStatusBlue.setText("bluetooth devices are available. ".toUpperCase());
// }

// bluetooth button on
onBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!bluetoothAdapter.isEnabled()) {
showToast("Bluetooth is turning on...");
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent, REQUEST_ENABLE_BT);

} else {
showToast("Bluetooth is already in on status...");

}
}

});


// bluetooth discover on
discoverBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!bluetoothAdapter.isDiscovering()) {
showToast("Making Device Discoverable");
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(intent, REQUEST_DISCOVER_BT);
}
}
});


// blutooth off button
offBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (bluetoothAdapter.isEnabled()) {
bluetoothAdapter.disable();
showToast("Turning off bluetooth...");
// can set the image below

} else {
showToast("Bluetooth is off already...");
}

}
});


//Button that connects the device to the bluetooth module when pressed
pairedBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

if (BTinit()) {
BTconnect();
}

}
});








scanBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bluetoothAdapter.startDiscovery();

}
});

IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(myReceiver, intentFilter);

arrayAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_2, stringArrayList);
scan_device.setAdapter(arrayAdapter);
}

BroadcastReceiver myReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device= intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
stringArrayList.add(device.getName());
arrayAdapter.notifyDataSetChanged();

}
}
};

// }








// paired devices text displays
// pairedBtn.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
//
//// paires();
//
// if (bluetoothAdapter.isEnabled()) {
//// mPairedBlue.setText("Paired Devices");
//// Set<BluetoothDevice> devices= bluetoothAdapter.getBondedDevices();
// BTconnect();
//// for (BluetoothDevice device: devices){
//// BTconnect();
//// mPairedBlue.append("nDevice: "+ device.getName()+ "," + device);
//
//// }
// } else {
//// bluetooth is off so couldn't pair any devices
// showToast("Turn on bluetooth to pair with devices...");
//
// }
// }
// });

// }

// public void paires(){
// Intent pair= new Intent(this, pairedDevices.class);
// startActivity(pair);
// }

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
switch (requestCode) {
case REQUEST_ENABLE_BT:
if (resultCode == RESULT_OK) {
// BLUETOOTH IS ON
//below can set an image for bluetooth on
showToast("Bluetooth is on...");
} else {
// set image for blueooth off
showToast("Bluetooth turn on failed...");
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}

// Message display function
private void showToast(String msg) {

Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();


}








// TRY CODE FOR PAIRING WITH BLUETOOTH MODULE
//Initializes bluetooth module
public boolean BTinit() {
boolean found = false;

BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

if (bluetoothAdapter == null) //Checks if the device supports bluetooth
{
Toast.makeText(getApplicationContext(), "Device doesn't support bluetooth", Toast.LENGTH_SHORT).show();
}

if (!bluetoothAdapter.isEnabled()) //Checks if bluetooth is enabled. If not, the program will ask permission from the user to enable it
{
Intent enableAdapter = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableAdapter, 0);

try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

Set<BluetoothDevice> bondedDevices = bluetoothAdapter.getBondedDevices();

if (bondedDevices.isEmpty()) //Checks for paired bluetooth devices
{
Toast.makeText(getApplicationContext(), "Please pair the device first", Toast.LENGTH_SHORT).show();
} else {
for (BluetoothDevice iterator : bondedDevices) {
if (iterator.getAddress().equals(DEVICE_ADDRESS)) {
device = iterator;
found = true;
break;
}
}
}

return found;
}

public void dispTxt(String msg) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}


public boolean BTconnect() {
boolean connected = true;

try {
socket = device.createRfcommSocketToServiceRecord(PORT_UUID); //Creates a socket to handle the outgoing connection
socket.connect();

Toast.makeText(getApplicationContext(),
"Connection to bluetooth device successful", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
connected = false;
}

if (connected) {
try {
outputStream = socket.getOutputStream(); //gets the output stream of the socket
} catch (IOException e) {
e.printStackTrace();
}
}

return connected;
}

@Override
protected void onStart() {
super.onStart();
}
}




     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.