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
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