Android Bluetooth Example Part 1 Android Developer Bluetooth Bluetooth Android Studio

public class Connect_Bluetooth extends Activity {

    ProgressDialog mProgressDlg;
    TextView onBtn;
    TextView offBtn;
    TextView listBtn;
    TextView findBtn,dk;
    TextView text;
    BluetoothAdapter myBluetoothAdapter;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_band);
        text = (TextView) findViewById(R.id.text);
        onBtn = (TextView)findViewById(R.id.turnOn);
        offBtn = (TextView)findViewById(R.id.turnOff);
        dk = (TextView) findViewById(R.id.mk);
        listBtn = (TextView)findViewById(R.id.paired);
        findBtn = (TextView)findViewById(R.id.search);
        myListView = (ListView)findViewById(R.id.listView1);
        mDeviceList = new ArrayList<BluetoothDevice>();
        mAdapter = new DeviceListAdapter(this);


        mProgressDlg = new ProgressDialog(this);
        mProgressDlg.setMessage("Scanning...");
        myBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        mProgressDlg.setCancelable(false);
        mProgressDlg.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();

                myBluetoothAdapter.cancelDiscovery();
            }
        });
        dk.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                makeDiscoverable();
            }
        });

        if (myBluetoothAdapter == null) {
            showUnsupported();
        } else {
         
//find the bluetooth enable or disable
            if (myBluetoothAdapter.isEnabled()) {
                showEnabled();
            } else {
                showDisabled();
            }

            findBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    if (myBluetoothAdapter.isDiscovering()) {
                        // the button is pressed when it discovers, so cancel the discovery
                        myBluetoothAdapter.cancelDiscovery();
                    }
                    else {
                        // BTArrayAdapter.clear();
                        myBluetoothAdapter.startDiscovery();

                        registerReceiver(mReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
                    }
                }
            });

            onBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Intent turnOnIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                    startActivityForResult(turnOnIntent, REQUEST_ENABLE_BT);



                }
            });

            offBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    myBluetoothAdapter.disable();

                    showDisabled();

                }
            });


        }





    }
 

 


    @Override
    public void onPause() {
        if (myBluetoothAdapter != null) {
            if (myBluetoothAdapter.isDiscovering()) {
                myBluetoothAdapter.cancelDiscovery();
            }
        }

        super.onPause();
    }

    @Override
    public void onDestroy() {
        unregisterReceiver(mReceiver);

        super.onDestroy();
    }

    private void showEnabled() {
        text.setText("Bluetooth is On");
        text.setTextColor(Color.BLUE);
        offBtn.setEnabled(true);
        onBtn.setEnabled(false);

    }

    private void showDisabled() {
        text.setText("Bluetooth is Off");
        text.setTextColor(Color.RED);
        offBtn.setEnabled(false);
        onBtn.setEnabled(true);

    }

    private void showUnsupported() {
        text.setText("Bluetooth is unsupported by this device");


    }

    private void makeDiscoverable() {
        Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
        discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
        startActivity(discoverableIntent);

    }}


this is the video link to understate more about  Bluetooth Example

https://www.youtube.com/watch?v=jHpG9h2Szao&lc=z13zens5mve3zdomq23bin2b2qrkyhnng

Android Custom Dialog

This post will show the android custom dialog with custom layout.



In the xml file you can create any type of layout.

XML

    <<
    ? xml version = "1.0"
encoding = "utf-8" ? >
    <
    RelativeLayout xmlns : android = "http://schemas.android.com/apk/res/android"
android: layout_width = "match_parent"
android: layout_height = "match_parent" >


    <
    LinearLayout
android: layout_width = "match_parent"
android: layout_height = "wrap_content"
android: layout_margin = "10dp"
android: layout_below = "@+id/editText11"
android: background = "@android:color/transparent"
android: orientation = "horizontal"
android: id = "@+id/linearLayout2" >

    <
    Button android: id = "@+id/ok"
android: layout_width = "match_parent"
android: layout_height = "50dp"
android: layout_weight = "1"
android: background = "@color/colorPrimary"
android: padding = "5dp"
android: text = "ok"
android: textColor = "@android:color/white"
android: textSize = "15dp"
android: textStyle = "bold" / >

    <
    Button android: id = "@+id/gallery"
android: layout_width = "match_parent"
android: layout_height = "50dp"
android: layout_marginLeft = "5dp"
android: layout_weight = "1"
android: background = "@color/colorPrimary"
android: padding = "5dp"
android: text = "Gallery"
android: visibility = "invisible"
android: textColor = "@android:color/white"
android: textSize = "15dp"
android: textStyle = "bold" / >

    <
    Button android: id = "@+id/cancel"
android: layout_width = "match_parent"
android: layout_height = "50dp"
android: layout_marginLeft = "5dp"
android: layout_weight = "1"
android: background = "@color/colorPrimary"
android: padding = "5dp"
android: text = "Cancel"
android: textColor = "@android:color/white"
android: textSize = "15dp"
android: textStyle = "bold" / >
    <
    /LinearLayout>

    <
    EditText android: layout_width = "match_parent"
android: textColor = "#000"
android: gravity = "center"
android: layout_height = "wrap_content"
android: inputType = "textPersonName"
android: hint = "Enter video link"
android: singleLine = "true"
android: ems = "10"
android: layout_centerHorizontal = "true"
android: id = "@+id/editText11" / >
    < /RelativeLayout>


Perfect Picker Date Android

DatePickerDialog mDatePickerDialog = null;
               final Calendar mCalendar = Calendar.getInstance();
               if (mDatePickerDialog == null) {
                   mDatePickerDialog = new DatePickerDialog(getActivity(),
                           new DatePickerDialog.OnDateSetListener() {
                               @Override
                               public void onDateSet(DatePicker view, int year,
                                                     int monthOfYear, int dayOfMonth) {
                                   // TODO Auto-generated method stub
                                     /*  edit_gi_age.setText(String.format(
                                               "%04d-%02d-%02d", year, monthOfYear + 1,
                                               dayOfMonth));*/
                                   edit_gi_age.setText(String.valueOf(getAge1(year, monthOfYear, dayOfMonth)));
                                   txt_ov_age.setText(String.valueOf(getAge1(year, monthOfYear, dayOfMonth)));
                                   mCalendar.set(Calendar.YEAR, year);
                                   mCalendar.set(Calendar.MONTH, monthOfYear);
                                   mCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                               }
                           }, 1970, 0, 1);
                   mCalendar.set(Calendar.YEAR, 1970);
                   mCalendar.set(Calendar.MONTH, 0);
                   mCalendar.set(Calendar.DAY_OF_MONTH, 1);
                   mDatePickerDialog.setCanceledOnTouchOutside(true);
                   mDatePickerDialog
                           .setTitle(getString(R.string.app_name));
                   mDatePickerDialog.getDatePicker().setMaxDate(calendar.getTimeInMillis());
                   mDatePickerDialog.getDatePicker().setMinDate(mCalendar.getTimeInMillis());
               }
               mDatePickerDialog.show();