Skip to content

Instantly share code, notes, and snippets.

@jonathankau
Forked from crazycoder1999/androidbtserver.java
Last active August 29, 2015 14:12

Revisions

  1. @crazycoder1999 crazycoder1999 revised this gist Aug 1, 2012. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions androidbtserver.java
    Original file line number Diff line number Diff line change
    @@ -12,8 +12,7 @@ public BluetoothAdapter findMyBT(BluetoothAdapter myBt){
    }
    }

    //ask to enable BT to user.. what Happenend if it answer NO? Should I have to check that REQUEST_ENABLE_BT?
    //only if it is enable.
    //ask to enable BT to user
    public void enableMyBT(BluetoothAdapter myBt) {
    if ( ! myBt.isEnabled()) {
    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    @@ -33,7 +32,7 @@ public void beVisible() {
    //this methoed must run inside a thread.
    private void listenForConnection() {
    try{
    BluetoothServerSocket bss = myBT.listenUsingRfcommWithServiceRecord(this.serviceName, BtConstants.btUUID);
    BluetoothServerSocket bss = myBT.listenUsingRfcommWithServiceRecord("YOURSERVICENAME", UUID); //the UUID need to be GENERATED
    bs = bss.accept();
    bss.close();
    out = bs.getOutputStream();
  2. @crazycoder1999 crazycoder1999 created this gist Aug 1, 2012.
    48 changes: 48 additions & 0 deletions androidbtserver.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    //Main necessary methoeds
    // get the bluetooth device reference
    public BluetoothAdapter findMyBT(BluetoothAdapter myBt){
    myBt = BluetoothAdapter.getDefaultAdapter();
    if (myBt!= null) {
    Log.i("BTDEV","BT Device Found!");
    Log.i("BTDEV","" + myBt.getName());
    return myBt;
    } else {
    Log.i("BTDEV","No BT Device Found!");
    return null;
    }
    }

    //ask to enable BT to user.. what Happenend if it answer NO? Should I have to check that REQUEST_ENABLE_BT?
    //only if it is enable.
    public void enableMyBT(BluetoothAdapter myBt) {
    if ( ! myBt.isEnabled()) {
    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    actv.startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    }
    }

    //ask to be visible for <visibility> seconds.
    public void beVisible() {
    Log.i("BTDEV","Let me be visible. thanks.");
    Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
    discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, visibility);
    this.actv.startActivity(discoverableIntent);
    }

    ///start listening for incoming data
    //this methoed must run inside a thread.
    private void listenForConnection() {
    try{
    BluetoothServerSocket bss = myBT.listenUsingRfcommWithServiceRecord(this.serviceName, BtConstants.btUUID);
    bs = bss.accept();
    bss.close();
    out = bs.getOutputStream();
    out.write("goodbye".getBytes()); //send a message
    //....

    out.close(); //close it
    bs.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }