It is very important many time to control bluetooth programmatically, so i have come with this post"How to control Bluetooth Programmatically ...........!"
let's start.....
Step:1
Enter in app development mode in Android Studio.
if you don't know how to do that then read this post.
Step:2
Now just drag & drop button into virtual mobile screen as shown in figure below.....
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
ArrayAdapter<BluetoothDevice> mArrayAdapter;
BluetoothAdapter mbt;
ArrayList<String> arr=new ArrayList<String>();
ArrayList<BluetoothDevice> ar=new ArrayList<BluetoothDevice>();
let's start.....
Step:1
Enter in app development mode in Android Studio.
if you don't know how to do that then read this post.
Step:2
Now just drag & drop button into virtual mobile screen as shown in figure below.....
Step:3
now just double click on dragged buttons and rename them as "On" and "Off".
Step:4
Now copy & paste following permission into manifest file.
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
Step:5
just copy and paste following code outside the OnCreate method:
ArrayAdapter<BluetoothDevice> mArrayAdapter;
BluetoothAdapter mbt;
ArrayList<String> arr=new ArrayList<String>();
ArrayList<BluetoothDevice> ar=new ArrayList<BluetoothDevice>();
Now copy and paste following code into OnCreate method:
Button bon=(Button) findViewById(R.id.button); Button boff=(Button) findViewById(R.id.button2); final ArrayAdapter<String> obj=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arr); mArrayAdapter =new ArrayAdapter<BluetoothDevice>(this, android.R.layout.simple_list_item_1,ar); mbt=BluetoothAdapter.getDefaultAdapter(); bon.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mbt.enable(); } }); boff.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mbt.disable(); } });
Step:6
Now you can control bluetooth by clicking on button.
For any query please comment below.......!
Comments
Post a Comment