I am trying to scan for available devices but the scan does not seem to 
find any devices. The app works and displays using Toast when the discovery 
starts and finishes but doesnt show a device, the turning on & off of BT 
works as well as displaying the paired devices.. I know there are available 
devices as the scan in settings show that they are there. I followed quite 
a few tutorials (this is my 6th attempt) and each of them come out with 
something similar which doesnt work. I have been to the play store and 
downloaded 8 different bluetooth scanners and only 50% of them actually 
worked.

I am using a Samsung S6 for testing and development. My goal is to find and 
connect to certain devices for communication and control but i want to be 
able to scan for them first.
Can anyone spot/see anything in the code which could cause it not to work 
or something I have missed?

MainActivity.java

public class MainActivity extends Activity {
private Button bluetoothOn;
private Button bluetoothOff;
private Button bluetoothScan;
private Button bluetoothPaired;
private BluetoothAdapter myBluetoothAdapter;
private ArrayAdapter<String> btArrayAdapter;
private ListView listDevicesFound;
private Set<BluetoothDevice>pairedDevices;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    bluetoothOn= (Button)findViewById(R.id.btnOn);
    bluetoothOff= (Button)findViewById(R.id.btnOff);
    bluetoothPaired = (Button)findViewById(R.id.btnPaired);
    bluetoothScan = (Button)findViewById(R.id.btnScan);

    listDevicesFound = (ListView)findViewById(R.id.devicesfound);
    btArrayAdapter = new ArrayAdapter<String>(MainActivity.this, 
android.R.layout.simple_list_item_1);
    btArrayAdapter.setNotifyOnChange(true);
    myBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    listDevicesFound.setAdapter(btArrayAdapter);

    registerReceiver(myBluetoothReceiver, new 
IntentFilter(BluetoothDevice.ACTION_FOUND));
}

public void scanonClick(View v) {
        // TODO Auto-generated method stub
        btArrayAdapter.clear();
        myBluetoothAdapter.startDiscovery();
    }

public void btonClick(View v) {
        // TODO Auto-generated method stub
        if (!myBluetoothAdapter.isEnabled()) {
            Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(turnOn, 0);
            Toast.makeText(getApplicationContext(),"Turned on"
                    ,Toast.LENGTH_LONG).show();
        }
        else {
            Toast.makeText(getApplicationContext(), "Already on",
                    Toast.LENGTH_LONG).show();
        }
}

public void btoffClick(View v) {
        // TODO Auto-generated method stub
        myBluetoothAdapter.disable();
        Toast.makeText(getApplicationContext(),"Turned off" ,
                Toast.LENGTH_LONG).show();
}

public void paironClick(View v) {
        // TODO Auto-generated method stub
        pairedDevices = myBluetoothAdapter.getBondedDevices();

        @SuppressWarnings("rawtypes")
        ArrayList list = new ArrayList();
        for(BluetoothDevice bt : pairedDevices)
            list.add(bt.getName());

        Toast.makeText(getApplicationContext(),"Showing Paired Devices",
                Toast.LENGTH_SHORT).show();
        @SuppressWarnings("rawtypes")
        final ArrayAdapter adapter = new 
ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1, list);
        listDevicesFound.setAdapter(adapter);
    }

private final BroadcastReceiver myBluetoothReceiver = new BroadcastReceiver(){

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        String action = intent.getAction();
        if(BluetoothDevice.ACTION_FOUND.equals(action)) {
            BluetoothDevice device = 
intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            btArrayAdapter.add(device.getName() + "\n" + device.getAddress());
            btArrayAdapter.notifyDataSetChanged();
            Toast.makeText(getApplicationContext(), "Run 
G",Toast.LENGTH_LONG).show();
        }
    }};

@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    unregisterReceiver(myBluetoothReceiver);
}
}



-- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to android-developers+unsubscr...@googlegroups.com.
To post to this group, send email to android-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/android-developers/1f34cb3e-5782-4ca5-ad76-3d62e465ac68%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to