コード例 #1
0
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == DroidTooth.BLUETOOTH_DISCOVERABILITY_REQUEST) {
      if (resultCode == Activity.RESULT_OK) {
        updateConsole("This device will continue to be discoverable");

      } else if (resultCode == Activity.RESULT_CANCELED) {
        DroidTooth.stopIndefiniteVisibility();
        updateConsole("User cancelled discoverability");
      }
    }
  }
コード例 #2
0
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    try {
      DroidTooth.init(this);
    } catch (Exception e) {
      Log.d(Constants.DEBUG_DROIDTOOTH, "Error Initializing DroidTooth: " + e);
    }
    console = (EditText) findViewById(R.id.console);
    deviceListContainer = (LinearLayout) findViewById(R.id.deviceListContainer);
    extraButtons = (LinearLayout) findViewById(R.id.extra_buttons);

    action1 = (Button) findViewById(R.id.action1);
    action1.setText("Radius Scan");

    action2 = (Button) findViewById(R.id.action2);
    action2.setText("Clear");

    action3 = (Button) findViewById(R.id.action3);
    action3.setText("Become Host");

    action4 = (Button) findViewById(R.id.action4);
    action4.setText("Join Host");

    indefDisco = (Button) findViewById(R.id.action5);
    indefDisco.setText("Indefinite Discoverability");

    becomeDisco = (Button) findViewById(R.id.action6);
    becomeDisco.setText("Become Visible");

    nameChange1 = (Button) findViewById(R.id.action7);
    nameChange1.setText("Name1");

    nameChange2 = (Button) findViewById(R.id.action8);
    nameChange2.setText("Name2");

    // create the list adapter, for managing the device list
    listAdapter = new DeviceListAdapter(this);
    deviceListView = (ListView) findViewById(R.id.deviceList);
    deviceListView.setAdapter(listAdapter);

    action1.setOnClickListener(
        new OnClickListener() {

          @Override
          public void onClick(View v) {
            Log.d(Constants.DEBUG_DROIDTOOTH, "Action 1 Clicked");
            DroidTooth.scanRadius(
                new DefaultCallback() {
                  // called whenever scanning starts
                  @Override
                  public void callback() {
                    updateConsole("Scanning started...");
                  }
                },
                new DeviceFoundCallback() {
                  @Override
                  public void callback(Object o) {
                    FoundDevice device = this.getFoundDeviceFromObject(o);
                    listAdapter.addDevice(device);
                    DroidToothActivity.this.updateConsole(
                        "Found device: "
                            + device.DEVICE_NAME
                            + " with address: "
                            + device.DEVICE.getAddress());
                  }
                },
                true,
                null);
          }
        });

    // clear screen
    action2.setOnClickListener(
        new OnClickListener() {

          @Override
          public void onClick(View arg0) {
            DroidToothActivity.this.clearScreens();
          }
        });

    indefDisco.setOnClickListener(
        new OnClickListener() {

          @Override
          public void onClick(View arg0) {
            DroidTooth.becomeVisibleIndefinitely();
          }
        });

    becomeDisco.setOnClickListener(
        new OnClickListener() {

          @Override
          public void onClick(View arg0) {
            updateConsole("Becoming visible...");
            DroidTooth.becomeVisible(DroidTooth.DEFAULT_DISCOVERABLE_DURATION);
          }
        });

    nameChange1.setOnClickListener(
        new OnClickListener() {

          @Override
          public void onClick(View arg0) {
            DroidTooth.changeDeviceName("Dr.Itan");
          }
        });

    nameChange2.setOnClickListener(
        new OnClickListener() {

          @Override
          public void onClick(View arg0) {
            DroidTooth.changeDeviceName("Dr.Dre");
          }
        });

    // become host
    action3.setOnClickListener(
        new OnClickListener() {

          @Override
          public void onClick(View v) {
            // initiate a new Host server with the specified broadcast name and 0th index key for
            // UUID
            DroidTooth.teeth(
                new DefaultCallback() {
                  /** callback when the server starts */
                  @Override
                  public void callback() {
                    action3.setText("Turn Off Host");
                    action3.setOnClickListener(
                        new OnClickListener() {

                          @Override
                          public void onClick(View arg0) {
                            DroidTooth.unteeth();
                          }
                        });
                    DroidToothActivity.this.updateConsole("DroidTooth Host Server Started...");
                  }
                },
                new NewIncomingServerConnectionCallback() {
                  @Override
                  public void callback(Object o) {
                    BluetoothSocket socket = this.getIncomingSocketFromObject(o);
                    updateConsole(
                        "New Incoming Connection to DroidToothServer from: "
                            + socket.getRemoteDevice().getName()
                            + " with address: "
                            + socket.getRemoteDevice().getAddress());
                    try {
                      socket.connect();
                    } catch (IOException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                    }
                  }
                });
          }
        });

    // join any public host
    action4.setOnClickListener(
        new OnClickListener() {

          @Override
          public void onClick(View v) {
            // initiate a new default Host server connectivity that has the specified suffix as in
            // Constants class.
            DroidTooth.tooth(
                new DefaultCallback() {
                  // callback for when scanning starts
                  @Override
                  public void callback() {
                    updateConsole("Scanning started...");
                  }
                },
                new NewOutgoingClientConnectionCallback() {

                  @Override
                  public void callback(Object o) {
                    final BluetoothSocket socket = this.getOutgoingSocketFromObject(o);
                    if (socket != null) {
                      runOnUiThread(
                          new Runnable() {
                            public void run() {
                              updateConsole(
                                  "New Client handshake connection to Server "
                                      + socket.getRemoteDevice().getName()
                                      + " with address: "
                                      + socket.getRemoteDevice().getAddress()
                                      + "!!");
                            }
                          });
                    }
                  }
                },
                new DeviceFoundCallback() {
                  @Override
                  public void callback(Object o) {

                    FoundDevice scannedDevice = this.getFoundDeviceFromObject(o);
                    listAdapter.addDevice(scannedDevice);
                    deviceListView.invalidate();
                    DroidToothActivity.this.updateConsole(
                        "Found device "
                            + scannedDevice.DEVICE_NAME
                            + " with address: "
                            + scannedDevice.DEVICE.getAddress()
                            + " while trying to tooth().");
                  }
                },
                null);
          }
        });
    console.setOnClickListener(
        new OnClickListener() {

          @Override
          public void onClick(View arg0) {
            if (currentOpenPane == 1) {
              expandPane(0);
            } else {
              expandPane(1);
            }
          }
        });
  }
コード例 #3
0
 @Override
 public void onStop() {
   DroidTooth.releaseResources(true);
   super.onStop();
 }