I don't see why the code below istn't working. No error messages are shown, the TextView simply doesn't show the name and address. My goad ist to show all discovered devices, so that in the end I have a list of them (not yet implemented, but in planning).
public class MainActivity extends AppCompatActivity {
public Button changeTextButton;
public TextView helloWorldTextView;
public TextView listOfDevices;
public BluetoothAdapter bluetoothAdapter;
public ArrayList<BluetoothDevice> devices;
public BroadcastReceiver receiver;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
changeTextButton = (Button) findViewById(R.id.button);
helloWorldTextView = (TextView) findViewById(R.id.textView);
listOfDevices = (TextView) findViewById(R.id.listOfBluetooth);
devices = new ArrayList<>();
changeTextButton.setOnClickListener(buttonClickListener);
doBluetoothStuff();
/*changeTextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
helloWorldTextView.setText(R.string.working);
}
});*/
}
public void doBluetoothStuff(){
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(receiver, intentFilter);
if(bluetoothAdapter == null){
new AlertDialog.Builder(this).setTitle("No adapter found: error")
.setMessage("Apparently there is a problem with your Smartphone")
.setPositiveButton("Damn...", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
System.exit(0);
}
})
.show();
}
listOfDevices.setText("Name of the adapter " + bluetoothAdapter);
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals(BluetoothDevice.ACTION_FOUND)){
BluetoothDevice d = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
listOfDevices.setText("Device found: " + d.getName() + "Address: " + d.getAddress());
Log.e("Test", d.getName());
}
}
};
if (bluetoothAdapter.isDiscovering()) {
// Bluetooth is already in modo discovery mode, we cancel to restart it again
bluetoothAdapter.cancelDiscovery();
}
bluetoothAdapter.startDiscovery();
}
View.OnClickListener buttonClickListener = new View.OnClickListener(){
@Override
public void onClick(View v) {
String text = "Working";
helloWorldTextView.setText(R.string.working);
}
};
}
Anyone know why??? I cannot find an error...
Thank you