Untitled
unknown
java
3 years ago
3.9 kB
12
Indexable
package fr.sti2d.crover;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import java.util.ArrayList;
import java.util.Set;
public class MainActivity extends AppCompatActivity {
private BluetoothAdapter bluetoothAdapter;
private ListView deviceList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnConnect = findViewById(R.id.boutonconnexion1);
deviceList = findViewById(R.id.list_devices);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
Toast.makeText(this, "Bluetooth is not supported on this device", Toast.LENGTH_SHORT).show();
finish();
return;
}
btnConnect.setOnClickListener(view -> showPairedDevices());
}
private void showPairedDevices() {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.BLUETOOTH) != 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> pairedDevices = bluetoothAdapter.getBondedDevices();
ArrayList<String> deviceNames = new ArrayList<>();
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
if (ActivityCompat.checkSelfPermission(this, android.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;
}
deviceNames.add(device.getName() + "\n" + device.getAddress());
}
} else {
Toast.makeText(this, "No paired devices found", Toast.LENGTH_SHORT).show();
}
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, deviceNames);
deviceList.setAdapter(adapter);
deviceList.setOnItemClickListener((adapterView, view, i, l) -> {
String deviceName = (String) adapterView.getItemAtPosition(i);
String deviceAddress = deviceName.substring(deviceName.length() - 17);
// Launch the next activity and pass the selected device's address as an extra
Intent intent = new Intent(MainActivity.this, roues.class);
intent.putExtra("device_address", deviceAddress);
startActivity(intent);
});
}
}
Editor is loading...