How do you rate this blog

Thursday, August 16, 2012

Bluetooth - Scan for devices


In this post I have created a simple application which searches for and displays a list of all the devices which have their Bluetooth turned on.
                The logic for the following is –
1)      Create a Bluetooth adapter.
2)      Create a Listview to store all the devices which have been discovered.
3)      See if the device has Bluetooth capability and turn it on if it is not enabled.
4)      Search for devices and get the names

Now lets start creating this simple application, this application requires access to the Bluetooth in the device so provide access to Bluetooth in the manifest file as shown below.

Then create the layout as shown below





The code for the same is as shown below 



Then implement the logic as described earlier,
1)      Declaring the bluetooth adapter
BluetoothAdapter BA = BluetoothAdapter.getDefaultAdapter();

2)      Creating the listview which stores all the device names in an array adapter
BTadapter=new ArrayAdapter <String>(BluetoothDemoActivity.this,android.R.layout.simple_list_item_1);

3)      Then we need to check if the device has bluetooth capability, if the device has bluetooth check if it is enabled and if it is not request the user to turn on the Bluetooth as shown in the code below

private void findBluetoothState() {
                               
                                if(BA==null){
                                                BTState.setText("Bluetooth service not available in the device");
                                }
                                else{
                                                if(BA.isEnabled()){
                                                                if(BA.isDiscovering()){
                                                                                BTState.setText("Finding Devices");
                                                                }
                                                                else{
                                                                                BTState.setText("Bluetooth is Enabled");
                                                                                BTState.setEnabled(true);
                                                                }
                                                }
                                                else{
                                                                BTState.setText("Bluetooth is not enabled");
                                                                Intent startBT=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                                                                startActivityForResult(startBT,REQUEST_ENABLE_BLUETOOTH);
                                                }
                                }
                               
                }

4)      Next search for devices and get the devices which have their bluetooth turned on.
                private final BroadcastReceiver BR=new BroadcastReceiver(){
                                @Override
                                public void onReceive(Context context, Intent intent) {
                                                String act=intent.getAction();
                                                if(BluetoothDevice.ACTION_FOUND.equals(act)){
                                                                BluetoothDevice device=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                                                                BTadapter.add(device.getName()+" - "+device.getAddress());
                                                                BTadapter.notifyDataSetChanged();
                                                }
                                }
                };
The code will look as shown in the image below – 



When you start the app and it finds that the Bluetooth is not enabled then you will get a screen shot as shown below.



Once you provide permission for it to turn on the Bluetooth and click on scan for devices then you will get the list of devices along with the MAC address as shown in the image below.



No comments:

Post a Comment