0

I have an assignment where I'm required to create a Mobile Application via Android Studios and one part of the aspect is to allow the phone to scan for nearby Bluetooth-enabled devices and retrieve the hardware of the device after the scan was performed. So far I have come across some sources in StackOverflow and tried to implement it myself but it couldn't work. Could you guys please advise accordingly on what should i do? I am using Java and not Kotlin.

I want to make a bluetooth scanner with scanning function and listing down devices scanned with their hardware names, model number and manufacturer name displayed. Please advise accordingly thank you so much.

This is my code snippet for Java Class (I am referencing from Programming Experts in Youtube)

package com.example.bluetoothvulnerabilityscanner;

import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Build;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
                                                                                                            
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import android.view.View;


public class ScanAroundDevices extends AppCompatActivity {

BluetoothAdapter bluetoothAdapter;
ArrayAdapter<String> arrayAdapter;
ArrayList arrayList;
ListView 11;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_scan_around_devices);
    bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    11 = (ListView) findViewById(R.id.listView);
    arrayList = new ArrayList();

    arrayAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, arrayList);
    11. setAdapter(arrayAdapter);

    findPairedDevices();
}

private void findPairedDevices() {
    int index = 0;
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    Set<BluetoothDevice> bluetoothSet = bluetoothAdapter.getBondedDevices();
    String[] str = new String[bluetoothSet.size()];

    if (bluetoothSet.size() > 0) {
        for (BluetoothDevice device : bluetoothSet) {
            str[index] = device.getName();
            index++;
        }

        arrayAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, str);
        11. setAdapter(arrayAdapter);
    }

}

private void setAdapter(ArrayAdapter<String> arrayAdapter) {
}

public void discoverDevices(View v) {
    bluetoothAdapter.startDiscovery();
}

BroadcastReceiver broadcastReceiver = 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);
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                return;
            }
            arrayList.add(device.getName());
            arrayAdapter.notifyDataSetChanged();
        }
    }
};


@Override
protected void onStart() {
    super.onStart();
    IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    registerReceiver(broadcastReceiver.intentFilter);

}

public void on(View v) {
    if (bluetoothAdapter == null) {
        Toast.makeText(getApplicationContext(), "Bluetooth is not enabled currently.", Toast.LENGTH_SHORT).show();
    } else {
        if (bluetoothAdapter.isEnabled()) {
            Intent i = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                return;
            }
            startActivityForResult(i, 1);
        }
    }
}

@Override
protected void onActivityResult(int requestCode, Intent data) {
    if (requestCode == 1) {
        if (resultCode == RESULT_OK) {
            Toast.makeText(getApplicationContext(), "Bluetooth Enabled", Toast.LENGTH_SHORT).show();

        }
    } else {
        Toast.makeText(getApplicationContext(), "Error. Please Check Your Bluetooth.", Toast.LENGTH_SHORT).show();
    }
}

public void off(View v) {
    if (bluetoothAdapter.isEnabled()) {
        Intent i = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        startActivities(i,1);
    }
}

private void startActivities(Intent i, int i1) {
}

and this is for my XML file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ScanAroundDevices">

<Button
    android:id="@+id/btn1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="25dp"
    android:text="ON"/>

<Button
    android:id="@+id/btn2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignTop="@+id/btn1"
    android:text="OFF"/>

0 Answers0