Esempio n. 1
0
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (android.os.Build.VERSION.SDK_INT > 9) {
      StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
      StrictMode.setThreadPolicy(policy);
    }

    // Register the BroadcastReceiver
    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    filter.addAction(BluetoothDevice.ACTION_UUID);
    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    registerReceiver(mReceiver, filter); // Don't forget to unregister
    // during onDestroy

    // Editing beyond this point

    // Get local Bluetooth adapter
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    // If the adapter is null, then Bluetooth is not supported
    if (mBluetoothAdapter == null) {
      Toast.makeText(this, "Bluetooth is not supported on this device.", Toast.LENGTH_LONG).show();
      finish();
    }

    // This if-else statement is a request to turn Bluetooth on
    // The body of the 'if' section should go in the on-create after login
    // The else section should go in the onCreate of the "Play" activity.
    if (!mBluetoothAdapter.isEnabled()) {
      Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
      startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
      // Else it is on, so let's make some magic happen
    } else {
      // Initialize ArrayAdapters for comparison
      mScannedDevices = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
      mListUsers = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
      mNearbyUsers_MAC = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
      mNearbyUsers_NAME = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
      mListGames = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);

      // Scan for unpaired devices
      scanForPlayers();

      // Finally set the value of the ListView to the value of the
      // mScannedDevices
      // This is just a placeholder to make sure that the scan is behaving
      // the correct way
      listView = (ListView) findViewById(R.id.listOfPlayers);
      listView.setAdapter(mScannedDevices);
    }

    // This code sets the Play button to INVISIBLE and then after 12 seconds
    // (the length of time needed to scan for player) it sets it to VISIBLE
    findViewById(R.id.playButton).setVisibility(View.INVISIBLE);
    findViewById(R.id.playButton)
        .postDelayed(
            new Runnable() {
              public void run() {
                findViewById(R.id.playButton).setVisibility(View.VISIBLE);
              }
            },
            12000);
  }
Esempio n. 2
0
 public void scanForPlayers() {
   // Start discovery of Bluetooth devices
   if (!mBluetoothAdapter.isDiscovering()) mBluetoothAdapter.startDiscovery();
 }