0

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

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
SimmensK
  • 17
  • 7
  • Possible duplicate of [Register broadcast receiver dynamically does not work - BluetoothDevice.ACTION\_FOUND](https://stackoverflow.com/questions/32656510/register-broadcast-receiver-dynamically-does-not-work-bluetoothdevice-action-f) – Jitendra A Sep 14 '18 at 17:41
  • I’m asking myself why u register the receiver before initializing the receiver member variable... – Esses77 Sep 14 '18 at 17:42
  • @Esses77 Tried it both ways, it still doesn't show any bluetooth devices. But the adapter is registered: android.Bluetooth.BluetoothAdapter@b0... – SimmensK Sep 14 '18 at 17:55

0 Answers0