Skip to content
pkarira edited this page Sep 23, 2016 · 7 revisions

##Sample code for MainActivity

class MainActivity extends BluetoothActivity {
  BluetoothManager bluetoothManager;
    receiceMessage rm;
    DeviceList dl;
    String type = "";
    ListView listview;//ListView for fetching list of detected devices
    int c=0;
    EditText et1;//EditText for putting text
    EditText et2;//EditText for putting playerId
    private ArrayAdapter adapter;//adapter for listView

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bluetoothManager= BluetoothManager.getInstance();
        rm = new receiceMessage();
        dl=new DeviceList();
        listview = (ListView) findViewById(R.id.listView);
        et1=(EditText)findViewById(R.id.editText);
        et2=(EditText)findViewById(R.id.editText2);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String itemValue = (String) l.getItemAtPosition(position);
                bluetoothManager.connectTo(itemValue);
            }
        });
        adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1);
        listview.setAdapter(adapter);
    }

    public void client(View v) { // Button setting type as CLIENT
        type = "CLIENT";
        bluetoothManager.Type(type);
    }

    public void server(View v) {//Button setting type as SERVER
        type = "SERVER";
        bluetoothManager.Type(type);
    }
 public void start(View v) {//button for starting the connection
         bluetoothManager.setMessageObject(rm);
         bluetoothManager.setListObject(dl);
        if (type.equals("CLIENT")) {
            enableBluetooth();
        }
        if (type.equals("SERVER")) {
           enableBluetooth();
        }
    }
public void clienttoclient (View v)  //Button for sending message from client to client
{
        bluetoothManager.clientToClient(et1.getText().toString(), Integer.parseInt(et2.getText().toString()));
    }
    public void devicelist(View v)//button for getting list of all connected devices
    {
         if(type.equals("SERVER"))
        Toast.makeText(getApplicationContext(), bluetoothManager.getAllConnectedDevices(),Toast.LENGTH_LONG).show();
        else 
          bluetoothManager.getAllConnectedDevices();
    }
public void send(View v)//Button for sending message from client to server or server to client
{
    if (type.equals("CLIENT")) {
        bluetoothManager.sendText(et1.getText().toString());
    }
    if (type.equals("SERVER")) {
        bluetoothManager.sendText(et1.getText().toString(),Integer.parseInt(et2.getText().toString()));
    }
public void disconnect(View v) //Button for disconnecting
 {
   Toast.makeText(getApplicationContext(), bm.disconnect(),Toast.LENGTH_LONG).show();
}
    
}
    class receiceMessage implements Observer {
        @Override
        public void update(Observable observable, Object data) {
           final String msg = ((receivemsg)observable).getMessage();
            runOnUiThread(new Runnable() {
                public void run() {
                    Toast.makeText(MainActivity.this,msg,Toast.LENGTH_LONG).show();
                }
            });
        }
    }
    class DeviceList implements Observer {
        @Override
        public void update(Observable observable, Object data) {
            if(((deviceList)observable).getContent().equals("bluetooth enabled"))
            {
                bluetoothManager.scanClients();
            }else
            adapter.add(((deviceList)observable).getContent());
        }
    }
}
Clone this wiki locally