Exemple #1
0
  @Override
  public synchronized void start() {

    if (isConnected()) return;

    receiver =
        new BroadcastReceiver() {
          @Override
          public void onReceive(Context context, Intent intent) {
            switch (intent.getIntExtra(BluetoothAdapter.EXTRA_SCAN_MODE, -1)) {
              case BluetoothAdapter.SCAN_MODE_NONE:
                setConnected(false);
                break;

              case BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE:
                isDiscoverable = true;
                if (server != null && server.isRunning()) {
                  setConnected(true);
                }
                break;

                // Only other is BluetoothAdapter.SCAN_MODE_CONNECTABLE. For now don't handle that.
            }
          }
        };
    context.registerReceiver(receiver, new IntentFilter(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED));

    /*
    if (server != null) {
        Utils.debugLog(TAG, "Attempting to start Bluetooth swap, but it appears to be running already. Will cancel it so it can be restarted.");
        server.close();
        server = null;
    }*/

    if (server == null) server = new BluetoothServer(this, context.getFilesDir());

    sendBroadcast(SwapService.EXTRA_STARTING);

    // store the original bluetoothname, and update this one to be unique
    deviceBluetoothName = adapter.getName();

    /*
    Utils.debugLog(TAG, "Prefixing Bluetooth adapter name with " + BLUETOOTH_NAME_TAG + " to make it identifiable as a swap device.");
    if (!deviceBluetoothName.startsWith(BLUETOOTH_NAME_TAG))
        adapter.setName(BLUETOOTH_NAME_TAG + deviceBluetoothName);

    if (!adapter.getName().startsWith(BLUETOOTH_NAME_TAG)) {
        Log.e(TAG, "Couldn't change the name of the Bluetooth adapter, it will not get recognized by other swap clients.");
        // TODO: Should we bail here?
    }*/

    if (!adapter.isEnabled()) {
      Utils.debugLog(TAG, "Bluetooth adapter is disabled, attempting to enable.");
      if (!adapter.enable()) {
        Utils.debugLog(
            TAG, "Could not enable Bluetooth adapter, so bailing out of Bluetooth swap.");
        setConnected(false);
        return;
      }
    }

    if (adapter.isEnabled()) {
      setConnected(true);
    } else {
      Log.i(
          TAG,
          "Didn't start Bluetooth swapping server, because Bluetooth is disabled and couldn't be enabled.");
      setConnected(false);
    }
  }
Exemple #2
0
 protected void onStopped() {
   Utils.debugLog(
       TAG, "Resetting bluetooth device name to " + deviceBluetoothName + " after swapping.");
   adapter.setName(deviceBluetoothName);
 }