My device can not find other Bluetooth devices with startDiscovery ();

0

Hello, I was developing an app to learn how bluetooth devices work and create a simple, one button to activate startDiscovery (); and save 3 attempts, when it starts looking for another when it finishes searching and another when it finds a device but the latter never finds a device here I leave the code I hope you can help me ...

public class MainActivity extends AppCompatActivity {

private BluetoothAdapter mBluetoothAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    IntentFilter search = new IntentFilter();
    search.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
    search.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    search.addAction(BluetoothDevice.ACTION_FOUND);
    registerReceiver(mReceiver, search);
    Button F = (Button) findViewById(R.id.Find);
    TextView Txt = (TextView) findViewById(R.id.textView);
    Txt.setEnabled(true);
    F.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(mBluetoothAdapter.isDiscovering()){
                Toast toast = Toast.makeText(getApplicationContext(), 
                "Already Discovering", Toast.LENGTH_LONG);
                toast.show();
            }else if(mBluetoothAdapter.startDiscovery()){
                Toast toast = Toast.makeText(getApplicationContext(), 
                "Discovering devices now", Toast.LENGTH_LONG);
                toast.show();
            }else{
                Toast toast = Toast.makeText(getApplicationContext(), 
                "Error", Toast.LENGTH_LONG);
                toast.show();
            }
        }
    });
}
@Override
protected void onDestroy() {
 unregisterReceiver(mReceiver);
    super.onDestroy();
} 
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
       String action = intent.getAction();
        switch (action){
            case BluetoothAdapter.ACTION_DISCOVERY_STARTED:
                Toast toast = Toast.makeText(getApplicationContext(), 
                "Discovering", Toast.LENGTH_LONG);
                toast.show();
                TextView Txt = (TextView) findViewById(R.id.textView);
                Txt.setText(action);
                break;
            case BluetoothAdapter.ACTION_DISCOVERY_FINISHED:
                Toast toastF = Toast.makeText(getApplicationContext(), 
                "Discovery Finished", Toast.LENGTH_LONG);
                toastF.show();
                break;
            case BluetoothDevice.ACTION_FOUND:
                BluetoothDevice device = 
              intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                Toast toastD = Toast.makeText(getApplicationContext(), 
                "Device Found", Toast.LENGTH_LONG);
                toastD.show();
        }
    }
};

}

    
asked by Alejandro Lopez 10.07.2018 в 18:04
source

0 answers