Это класс Actitity: < /p>
Код: Выделить всё
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import it.eresult.silvermountain.databinding.ActivityFindDeviceBinding;
import it.eresult.silvermountain.helpers.adapters.DeviceDiscoveredViewAdapter;
import it.eresult.silvermountain.model.dataModel.Device;
import it.eresult.silvermountain.model.dataModel.TypeDevice;
public class FindDeviceActivity extends AppCompatActivity {
private ActivityFindDeviceBinding binding;
private BluetoothAdapter mBluetoothAdapter;
private boolean mScanning;
private Handler mHandler;
private final static int REQUEST_ENABLE_BT = 1;
TypeDevice deviceToFind;
List listDeviceDiscovered;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityFindDeviceBinding.inflate(getLayoutInflater());
View view = binding.getRoot();
setContentView(view);
listDeviceDiscovered = new ArrayList();
binding.listViewDeviceDiscovered.setAdapter(new DeviceDiscoveredViewAdapter(getApplicationContext(), listDeviceDiscovered));
binding.listViewDeviceDiscovered.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView arg0, View arg1, int position, long arg3) {
Device deviceSelected = listDeviceDiscovered.get(position);
Log.v("", deviceSelected.getNome()+" device selezionato per il pairing");
}
});
deviceToFind = (TypeDevice) getIntent().getSerializableExtra("device");
if (ContextCompat.checkSelfPermission( this, Manifest.permission.ACCESS_COARSE_LOCATION ) != PackageManager.PERMISSION_GRANTED ||
ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED ||
ContextCompat.checkSelfPermission( this, Manifest.permission.BLUETOOTH_SCAN ) != PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(
this,
new String [] { Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.BLUETOOTH_CONNECT, Manifest.permission.BLUETOOTH_SCAN },1
);
}
int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(
this,
new String [] { android.Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.BLUETOOTH_CONNECT,
Manifest.permission.BLUETOOTH_SCAN },1
);
}
if(areLocationServicesEnabled(this)) {
mHandler = new Handler();
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
Toast.makeText(this, "BLE Non supportato", Toast.LENGTH_SHORT).show();
finish();
}
final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
scanLeDevice(true);
}
}
public boolean areLocationServicesEnabled(Context context) {
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
try {
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) ||
locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
private void scanLeDevice(final boolean enable) {
if (enable) {
mScanning = true;
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_SCAN) != 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;
}
mBluetoothAdapter.startLeScan(mLeScanCallback);
} else {
mScanning = false;
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_SCAN) != 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;
}
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
}
// Device scan callback.
private BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi,
byte[] scanRecord) {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
return;
}
if(device.getName()!=null && device.getName().startsWith(deviceToFind.getAbbreviazione())){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
Log.i("NUOVO DEVICE SCOPERTO",device.getName() + " " +device.getAddress()+" "+device.getAlias()+" "+device.getType());
}
//devo mostrare in pagina il nuovo device.
addDeviceDiscovered(new Device(device.getName(), device.getAddress()));
binding.listViewDeviceDiscovered.setAdapter(new DeviceDiscoveredViewAdapter(getApplicationContext(), listDeviceDiscovered));
}
}
});
}
};
private void addDeviceDiscovered(Device newDevice){
try{
if(listDeviceDiscovered==null)
listDeviceDiscovered = new ArrayList();
boolean find = false;
for (Device d: listDeviceDiscovered) {
if(d.getMacAddress().equalsIgnoreCase(newDevice.getMacAddress())){
find=true;
break;
}
}
if(!find)
listDeviceDiscovered.add(newDevice);
}catch(Exception e){
Log.e("",e.getMessage());
}
}
}
< /code>
Теперь в методе ClickListener < /p>
binding.listViewDeviceDiscovered.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView arg0, View arg1, int position, long arg3) {
Device deviceSelected = listDeviceDiscovered.get(position);
Log.v("", deviceSelected.getNome()+" device selezionato per il pairing");
}
});
Подробнее здесь: https://stackoverflow.com/questions/794 ... in-android