1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| public class Bluetooth extends AppCompatActivity {
final ListView Deviceslist = (ListView)findViewById(R.id.listView1);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bluetooth);
ArrayAdapter<String> btArrayAdapter = new ArrayAdapter<String>(Bluetooth.this, android.R.layout.simple_list_item_1);
final Button scanb = (Button)findViewById(R.id.button);
final BluetoothAdapter bluetoothAdapter =
BluetoothAdapter.getDefaultAdapter();
scanb.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
ArrayAdapter<String> btArrayAdapter = new ArrayAdapter<String>(Bluetooth.this, android.R.layout.simple_list_item_1);
btArrayAdapter.clear();
bluetoothAdapter.startDiscovery();
Toast.makeText(Bluetooth.this, "Recherche en cours, merci de patienter...", Toast.LENGTH_LONG).show();
Deviceslist.setAdapter(btArrayAdapter);
}
});
registerReceiver(FoundReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
unregisterReceiver(FoundReceiver);
}
private final BroadcastReceiver FoundReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
final ListView Deviceslist = (ListView)findViewById(R.id.listView1);
// TODO Auto-generated method stub
ArrayList<String> mDeviceList = new ArrayList<String>();
String action = intent.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
mDeviceList.add(device.getName()+ "\n" + device.getAddress() );
Deviceslist.setAdapter(new ArrayAdapter<String>(Bluetooth.this,android.R.layout.simple_list_item_1, mDeviceList));
Toast.makeText(Bluetooth.this, "Appareil(s) trouvé(s).", Toast.LENGTH_LONG).show();
}
}};
} |
Partager