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.



Tuesday, August 7, 2012

To get Latitude and Longitude of Current Location


This post explains how to get the current location using GPS. In this post the app will display your current latitude and longitude if it can access the network.

The logic for this is -
1)      Provide the app access to GPS in the device.
2)      Get location manager, which enables the device to get periodic location updates of the device.
3)      Then select the best provider based on the criteria.
4)      Then obtain the latitude and longitude if there is a provider.

Based on the logic mentioned above lets code the application –

1)      In the android manifest file provide access to the GPS using code as shown in the snapshot given below.

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

The code will look as shown below in the snapshot



2)      Get the location manager – 

LocationManager LC=(LocationManager) getSystemService(Context.LOCATION_SERVICE);

The above code creates a location manager to get the location updates.

3)      Select the best provider based on the criteria –
Criteria criteria=new Criteria();
provider=LC.getBestProvider(criteria, false);
        Location loc=LC.getLastKnownLocation(provider);
4)      Obtain the latitude and longitude –

@Override
        public void onLocationChanged(Location location) {
                        intlat=(int) (location.getLatitude());
                        intlon=(int) (location.getLongitude());
                        latitudeval.setText(String.valueOf(lat));
                        longitudeval.setText(String.valueOf(lon));
        }

The code will look as shown below.



Create an APK file out of it and install on your mobile and run the application. The output will be as shown in the snapshot shown below.