Reputation: 3
I follow this tutorial to light on a LED with a phone. But I have an error in this code :
package com.irobotechart.controlbt;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Set;
public class DevicesBT extends AppCompatActivity {
//1)
// Depuración de LOGCAT
private static final String TAG = "DevicesBT"; //<-<- PARTE A MODIFICAR >->->
// Declaracion de ListView
ListView IdLista;
// String que se enviara a la actividad principal, mainactivity
public static String EXTRA_DEVICE_ADDRESS = "device_address";
// Declaracion de campos
private BluetoothAdapter mBtAdapter;
private ArrayAdapter mPairedDevicesArrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_devices_bt);
}
@Override
public void onResume()
{
super.onResume();
//---------------------------------
VerificarEstadoBT();
// Inicializa la array que contendra la lista de los dispositivos bluetooth vinculados
mPairedDevicesArrayAdapter = new ArrayAdapter(this, R.layout.name_devices);//<-<- PARTE A MODIFICAR >->->
// Presenta los disposisitivos vinculados en el ListView
IdLista = (ListView) findViewById(R.id.IdList);
IdLista.setAdapter(mPairedDevicesArrayAdapter);
IdLista.setOnItemClickListener(mDeviceClickListener);
// Obtiene el adaptador local Bluetooth adapter
mBtAdapter = BluetoothAdapter.getDefaultAdapter();
// Obtiene un conjunto de dispositivos actualmente emparejados y agregua a 'pairedDevices'
Set pairedDevices = mBtAdapter.getBondedDevices();
// Adiciona un dispositivos previo emparejado al array
if (pairedDevices.size() > 0)
{
for (BluetoothDevice device : pairedDevices) {
mPairedDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}
}
}
// Configura un (on-click) para la lista
private AdapterView.OnItemClickListener mDeviceClickListener = new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView av, View v, int arg2, long arg3) {
// Obtener la dirección MAC del dispositivo, que son los últimos 17 caracteres en la vista
String info = ((TextView) v).getText().toString();
String address = info.substring(info.length() - 17);
// Realiza un intent para iniciar la siguiente actividad
// mientras toma un EXTRA_DEVICE_ADDRESS que es la dirección MAC.
Intent i = new Intent(DevicesBT.this, UserInterface.class);//<-<- PARTE A MODIFICAR >->->
i.putExtra(EXTRA_DEVICE_ADDRESS, address);
startActivity(i);
}
};
private void VerificarEstadoBT() {
// Comprueba que el dispositivo tiene Bluetooth y que está encendido.
mBtAdapter= BluetoothAdapter.getDefaultAdapter();
if(mBtAdapter==null) {
Toast.makeText(getBaseContext(), "El dispositivo no soporta Bluetooth", Toast.LENGTH_SHORT).show();
} else {
if (mBtAdapter.isEnabled()) {
Log.d(TAG, "...Bluetooth Activado...");
} else {
//Solicita al usuario que active Bluetooth
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
}
}
}
}
The error is : Incompatible types : Found :'android.bluetooth.BluetoothDevice', require: 'java.lang.Object'
I don't know how I can solve this problem.
Thank you in advance for your help
I hope to be clear, I'm sorry if there are mistakes
Upvotes: 0
Views: 1254
Reputation: 37404
You have used a Raw-type Set
instead of generic
mean, while using collections you can define the type of data and if you don't define then the type will be of Object
so use
Set<BluetoothDevice> pairedDevices = mBtAdapter.getBondedDevices();
instead of
Set pairedDevices = mBtAdapter.getBondedDevices();
Note : remove mBtAdapter = BluetoothAdapter.getDefaultAdapter();
from onresume
it's redundant, already done in VerificarEstadoBT()
Further Read
What is a raw type and why shouldn't we use it?
Upvotes: 1