Program on Turning ON,OFF and Enable device Discoverable in Bluetooth on Android devices
I have used button and switch .
activity_main.xml(using Button)
<RelativeLayout android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:text="" android:layout_width="match_parent" android:layout_height="100dp" android:layout_alignParentTop="true" android:layout_marginTop="281dp" android:id="@+id/textView" /> <Button android:text="TURN_ON" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="41dp" android:id="@+id/button" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <Button android:text="DISCOVERABLE" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/button" android:layout_toRightOf="@+id/button" android:layout_toEndOf="@+id/button" android:layout_marginLeft="10dp" android:layout_marginStart="10dp" android:id="@+id/button2" /> <Button android:text="TURN_OFF" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/button2" android:layout_toRightOf="@+id/button2" android:layout_toEndOf="@+id/button2" android:layout_marginLeft="12dp" android:layout_marginStart="12dp" android:id="@+id/button3" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" app:srcCompat="@mipmap/ic_launcher" android:layout_alignTop="@+id/textView" android:id="@+id/imgv" android:layout_alignRight="@+id/button3" android:layout_alignEnd="@+id/button3" android:layout_alignBottom="@+id/textView" /> </RelativeLayout>
discoverswitch.xml(using Switch)
<RelativeLayout android:id="@+id/discoverswitch" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".discoverswitch"> <Switch android:text="ON/OFF" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/textView2" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:layout_marginRight="48dp" android:layout_marginEnd="48dp" android:id="@+id/switch1" /> <TextView android:text="Bluetooth" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginStart="20dp" android:layout_marginTop="19dp" android:id="@+id/textView2" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <TextView android:text="Discover Devices" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView2" android:layout_alignLeft="@+id/textView2" android:layout_alignStart="@+id/textView2" android:layout_marginTop="67dp" android:id="@+id/textView3" /> <Switch android:text="ON/OFF" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/textView3" android:layout_alignRight="@+id/switch1" android:layout_alignEnd="@+id/switch1" android:id="@+id/switch2" /> <ListView android:layout_width="match_parent" android:layout_height="320dp" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:id="@+id/listview"/> </RelativeLayout>MainActivity.java
public class MainActivity extends AppCompatActivity { private static final int REQUEST_ENABLE_BT = 0; private static final int REQUEST_DISCOVERABLE_BT = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final TextView out = (TextView) findViewById(R.id.textView); final Button Turn_On = (Button) findViewById(R.id.button); final Button Discover = (Button) findViewById(R.id.button2); final Button Turn_Off = (Button) findViewById(R.id.button3); final BluetoothAdapter BAdapter = BluetoothAdapter.getDefaultAdapter(); if (BAdapter == null) { out.append("Device not Supported"); } Turn_On.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (!BAdapter.isEnabled()) { Intent enableBIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBIntent, REQUEST_ENABLE_BT); } } }); Discover.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (!BAdapter.isDiscovering()) { Toast.makeText(getApplicationContext(), "Making your Device Discoverable", Toast.LENGTH_LONG).show(); Intent enableBIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); startActivityForResult(enableBIntent, REQUEST_DISCOVERABLE_BT); } } }); Turn_Off.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { BAdapter.disable(); Toast.makeText(getApplicationContext(), "Turning Off Bluetooth", Toast.LENGTH_LONG).show(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main,menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch(item.getItemId()) { case R.id.item1: Intent i=new Intent(getApplicationContext(),discoverswitch.class); startActivity(i); return true; case R.id.item2: ImageView imgview=(ImageView)findViewById(R.id.imgv); imgview.setImageResource(R.drawable.apple); return true; default: return super.onOptionsItemSelected(item); } } }discoverswitch.java
public class discoverswitch extends Activity { Setpaired; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.discoverswitch); final Switch onoffswitch=(Switch)findViewById(R.id.switch1); final Switch discoverswitch=(Switch)findViewById(R.id.switch2); final BluetoothAdapter Badapter=BluetoothAdapter.getDefaultAdapter(); final ListView listView=(ListView)findViewById(R.id.listview); onoffswitch.setChecked(true); if(Badapter==null) { Toast.makeText(getApplicationContext(),"This Device not supported",Toast.LENGTH_LONG).show(); } onoffswitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { if(b){ if(!Badapter.isEnabled()) { Intent turnon=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(turnon,0); } else { Toast.makeText(getApplicationContext(),"Bluetooth is Already turned ON",Toast.LENGTH_LONG).show(); } } else { Badapter.disable(); Toast.makeText(getApplicationContext(),"Bluetooth is turned OFF",Toast.LENGTH_LONG).show(); } } }); discoverswitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { if(b) { if(!Badapter.isDiscovering()) { Intent discovering=new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); startActivityForResult(discovering,0); try{ paired=Badapter.getBondedDevices(); ArrayList list= new ArrayList (); for(BluetoothDevice btd: paired) list.add(btd.getName()); Toast.makeText(getApplicationContext(),"Paired Devices",Toast.LENGTH_LONG).show(); ArrayAdapter adapter=new ArrayAdapter (getApplicationContext(),android.R.layout.simple_list_item_1,list); listView.setAdapter(adapter); } catch(Exception e) { Toast.makeText(getApplicationContext(),"No bluetooth Device found",Toast.LENGTH_LONG).show(); } } else { Toast.makeText(getApplicationContext(),"Sorry Unable to discover",Toast.LENGTH_LONG).show(); } } else { Toast.makeText(getApplicationContext(),"Discover is not turned on",Toast.LENGTH_LONG).show(); } } }); } } I have added options menu in Action bar for changing to switch mode.
Comments
Post a Comment