Bluetooth
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:transitionGroup="true">
<TextView android:text="Bluetooth Example"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textview"
android:textSize="35dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Turn On"
android:id="@+id/button"
android:layout_below="@+id/textview"
android:clickable="true"
android:onClick="on" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get visible"
android:onClick="visible"
android:id="@+id/button2"
android:layout_below="@+id/textview"
android:layout_toRightOf="@+id/button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="List devices"
android:onClick="list"
android:layout_below="@+id/button"
android:id="@+id/button3"
android:layout_toRightOf="@+id/button4"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="turn off"
android:onClick="off"
android:id="@+id/button4"
android:layout_below="@+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_alignParentBottom="true"
android:layout_alignLeft="@+id/button"
android:layout_alignStart="@+id/button"
android:layout_below="@+id/textView2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Paired devices:"
android:id="@+id/textView2"
android:textColor="#ff34ff06"
android:textSize="25dp"
android:layout_below="@+id/button4"
android:layout_alignLeft="@+id/listView"
android:layout_alignStart="@+id/listView" />
</RelativeLayout>
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
Button b1,b2,b3,b4;
private BluetoothAdapter BA;
private Set<BluetoothDevice>pairedDevices;
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
b1 = (Button) findViewById([Link]);
b2=(Button)findViewById([Link].button2);
b3=(Button)findViewById([Link].button3);
b4=(Button)findViewById([Link].button4);
BA = [Link]();
lv = (ListView)findViewById([Link]);
}
public void on(View v){
if (![Link]()) {
Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnOn, 0);
[Link](getApplicationContext(), "Turned
on",Toast.LENGTH_LONG).show();
} else {
[Link](getApplicationContext(), "Already on",
Toast.LENGTH_LONG).show();
}
}
public void off(View v){
[Link]();
[Link](getApplicationContext(), "Turned off" ,Toast.LENGTH_LONG).show();
}
public void visible(View v){
Intent getVisible = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(getVisible, 0);
}
public void list(View v){
pairedDevices = [Link]();
ArrayList list = new ArrayList();
for(BluetoothDevice bt : pairedDevices) [Link]([Link]());
[Link](getApplicationContext(), "Showing Paired
Devices",Toast.LENGTH_SHORT).show();
final ArrayAdapter adapter = new
ArrayAdapter(this,[Link].simple_list_item_1, list);
[Link](adapter);
}
}
[Link]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="[Link]
package="[Link]">
<uses-permission android:name="[Link]"/>
<uses-permission android:name="[Link].BLUETOOTH_ADMIN"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="[Link]" />
<category android:name="[Link]" />
</intent-filter>
</activity>
</application>
</manifest>
Output
Map :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">
<EditText
android:id="@+id/editid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="ID" />
<EditText
android:id="@+id/editname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name" />
<EditText
android:id="@+id/editmobile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Mobile No." />
<EditText
android:id="@+id/editaddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Address"
android:lines="3" />
<EditText
android:id="@+id/editpincode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Pin Code" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Insert Data" />
<EditText
android:id="@+id/editsearchid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter ID" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Search Data" />
</LinearLayout>
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
SQLiteDatabase sqLiteDatabaseObj;
EditText editTextID, editTextName, editMobileNo, editAddress, editPincode, editSearchid;
String cid, cname, cmobile, caddress, cpincode, sql_query, sid;
Button EnterData, SearchData;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
EnterData = (Button)findViewById([Link]);
SearchData = (Button)findViewById([Link].button1);
editTextID = (EditText)findViewById([Link]);
editTextName = (EditText)findViewById([Link]);
editMobileNo = (EditText)findViewById([Link]);
editAddress = (EditText)findViewById([Link]);
editPincode = (EditText)findViewById([Link]);
editSearchid = (EditText)findViewById([Link]);
sqLiteDatabaseObj = openOrCreateDatabase("AndroidJSonDataBase",
Context.MODE_PRIVATE, null);
[Link]("CREATE TABLE IF NOT EXISTS AndroidJSonTable(id
INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, cid VARCHAR, name VARCHAR, mobile
VARCHAR, address VARCHAR, pincode VARCHAR);");
[Link](new [Link]() {
@Override
public void onClick(View view) {
cid = [Link]().toString();
cname = [Link]().toString() ;
cmobile = [Link]().toString();
caddress = [Link]().toString();
cpincode = [Link]().toString();
String sql_query = "INSERT INTO AndroidJSonTable (cid, name,
mobile, address, pincode) VALUES('"+cid+"', '"+cname+"', '"+cmobile+"', '"+caddress+"',
'"+cpincode+"');";
[Link](sql_query);
[Link](getApplicationContext(), "Data Insterted
Successfully", Toast.LENGTH_LONG).show();
});
[Link](new [Link]() {
@Override
public void onClick(View view) {
sid = [Link]().toString();
Cursor cursor = [Link]( "select * from AndroidJSonTable where
cid="+sid+"", null );
StringBuffer buffer= new StringBuffer();
while ([Link]())
String cid =[Link](1);
String name =[Link](2);
String mob =[Link](3);
String addr =[Link](4);
String pcode =[Link](5);
[Link](cid+ " " + name + " " + mob +" " + addr +" " + pcode +" \n");
[Link](getApplicationContext(), buffer, Toast.LENGTH_LONG).show();
);