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

1 comment:

  1. How to download the Sega Mega Drive games on your mobile
    You can get the same 나주 출장마사지 amount 경산 출장안마 of Sega Genesis games as the SNES 제주 출장안마 in your PC with the Genesis. The two games 동해 출장안마 are 서산 출장마사지 Genesis/Mega Drive games.

    ReplyDelete