Search code examples
androidbluetoothbroadcastreceiver

Can anyone tell me what went wrong in this code?


I'm a newbie in Android. I'm trying to scan other Bluetooth devices. Please guide me and help me in finding a solution. Here is the part of code:

scan_btn.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        mBluetoothAdapter.startDiscovery();
        mReceiver = new BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
                if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                    BluetoothDevice device = Untent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
                }
            }
        };
        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); 
        registerReceiver(mReceiver, filter); 
    }
});

@Override
protected void onDestroy() {
    super.onDestroy();
    unregisterReceiver(mReceiver);//unregister the receiver
}

enter image description here


Solution

  • Your button is toggle button. Make sure your code is -

     mToggleButton = (ToggleButton) findViewById(R.id.toggleButton);
     mToggleButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
        }