Broadcastreceiver для обнаружения устройств Bluetooth не работает - PullRequest
0 голосов
/ 14 сентября 2018

Я не понимаю, почему приведенный ниже код не работает. Сообщения об ошибках не отображаются, TextView просто не отображается имя и адрес. Моя задача показать все обнаруженные устройства, так что в итоге У меня есть их список (еще не реализован, но в планировании).

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);
    }
};

}

Кто-нибудь знает почему ??? Я не могу найти ошибку ...

Спасибо

...