Click here to Skip to main content
15,888,610 members
Articles / Web Development / HTML
Tip/Trick

Bluetooth Desktop Locker with Android Application

Rate me:
Please Sign up or sign in to vote.
4.88/5 (11 votes)
5 Nov 2014CPOL2 min read 17.1K   2.7K   15  
Bluetooth Desktop Locker with Android Application

Introduction

After creating a USB locker, here it's time for a Bluetooth locker. You can check my article on USB Desktop locker. And after doing this, I thought why don't I create an Android application to access the operation. So let's come and check how to create our very own Bluetooth desktop locker with an Android app.

Process

The logic between a USB locker and a Bluetooth locker is a little bit different. In the USB locker, it's searching for a file and the content of that file. But here, we will check to discover the Bluetooth device in an interval. So before you proceed into this tip, you can go through the USB desktop locker post of mine. It will clear the logic to lock and unlock your desktop.

Using the Code

Let's come to the code. First, you need to discover the on bluetooth devices near your device. To do that, follow the below code:

C#
InTheHand.Net.Sockets.BluetoothClient bc = new InTheHand.Net.Sockets.BluetoothClient();
InTheHand.Net.Sockets.BluetoothDeviceInfo[] array = bc.DiscoverDevices();

In the array, all the discoverable devices will be enlisted. Now you have to check the mac address of each devices.

C#
for (int i = 0; i < array.Length; i++)
{
    this.address_array[i] = array[i].DeviceAddress;
    mac = mac + this.address_array[i].ToString();
}

Now the checking will be done. Check the mac address of your device is present in the discoverable devices or not. If found, do nothing else lock the computer. How to lock the computer, it is available in the USB desktop locker post. Hence, I am posting it once again.

C#
[DllImport("user32.dll", SetLastError = true)]
static extern bool LockWorkStation();

bool result = LockWorkStation();

if (result == false)
{
     // An error occurred
     throw new Win32Exception(Marshal.GetLastWin32Error());
}

Now the last one. Add a timer and set the interval, enable it and on the tick method, write down the code that I have explained.

After doing the desktop application, allow me to proceed to the second section...the Android application to operate it. Logic behind this is very simple. Just make an app to turn off and on the Bluetooth. I know you will say it's a stupid one. Everybody can switch on and off the Bluetooth of a device. But we believe in JO DIKHTA HAI WOH BIKTA HAI (that which is seen can be sold).

So why not make an Android app for this one. I kept it very simple. You guys made it as colorful as you want.

Write down the code to turn on and off the Bluetooth of an Android device.

Design

C#
<relativelayout xmlns:tools="http://schemas.android.com/tools" 
xmlns:android="http://schemas.android.com/apk/res/android" 
tools:context=".MainActivity" android:paddingtop="@dimen/activity_vertical_margin" 
android:paddingright="@dimen/activity_horizontal_margin" 
android:paddingleft="@dimen/activity_horizontal_margin" 
android:paddingbottom="@dimen/activity_vertical_margin" 
android:layout_width="match_parent" android:layout_height="match_parent">

    <textview android:id="@+id/out" android:layout_width="wrap_content" 
    android:layout_height="wrap_content">  
    <button android:id="@+id/button1" 
    android:layout_alignparentleft="true" 
    android:layout_alignparenttop="true" 
    android:layout_height="wrap_content" 
    android:layout_marginleft="30dp" 
    android:layout_margintop="49dp" 
    android:layout_width="wrap_content" 
    android:text="TURN_ON"></button>
    <button android:id="@+id/button2" 
    android:layout_alignleft="@+id/button1" 
    android:layout_below="@+id/button1" 
    android:layout_height="wrap_content" 
    android:layout_margintop="27dp" 
    android:layout_width="wrap_content" 
    android:text="DISCOVERABLE"></button>
    <button android:id="@+id/button3" 
    android:layout_alignleft="@+id/button2" 
    android:layout_below="@+id/button2" 
    android:layout_height="wrap_content" 
    android:layout_margintop="28dp" 
    android:layout_width="wrap_content" 
    android:text="TURN_OFF"></button>
    </relativelayout>

Code

C#
package com.example.blutooth01;

import android.os.Bundle;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
     private static final int REQUEST_ENABLE_BT = 0;  
     private static final int REQUEST_DISCOVERABLE_BT = 0;     

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         final TextView out=(TextView)findViewById(R.id.out);  
            final Button button1 = (Button) findViewById(R.id.button1);  
            final Button button2 = (Button) findViewById(R.id.button2);  
            final Button button3 = (Button) findViewById(R.id.button3);  
            final BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();  
            if (mBluetoothAdapter == null) {  
                out.append("device not supported");  
             }  
             button1.setOnClickListener(new View.OnClickListener() {  
                 public void onClick(View v) {  
                     if (!mBluetoothAdapter.isEnabled()) {  
                         Intent enableBtIntent = 
                         new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);  
                         startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);  
                     }  
                 }  
             });  
             button2.setOnClickListener(new View.OnClickListener() {  
                 @Override  
                    public void onClick(View arg0) {  
                        if (!mBluetoothAdapter.isDiscovering()) {  
                              //out.append("MAKING YOUR DEVICE DISCOVERABLE");  
                               Toast.makeText(getApplicationContext(), 
                               "MAKING YOUR DEVICE DISCOVERABLE",  
                         Toast.LENGTH_LONG);  
              
                            Intent enableBtIntent = 
                            new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);  
                            startActivityForResult(enableBtIntent, REQUEST_DISCOVERABLE_BT);  
                                
                        }  
                    }  
                });  
             button3.setOnClickListener(new View.OnClickListener() {  
                 @Override  
                 public void onClick(View arg0) {     
                     mBluetoothAdapter.disable();  
                     //out.append("TURN_OFF BLUETOOTH");  
                     Toast.makeText(getApplicationContext(), 
                     "TURNING_OFF BLUETOOTH", Toast.LENGTH_LONG);                     
                     }  
             });  
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

And in the AndroidMainfest.XML file write down the code.

Download the full source code. Links are available at the top of the post.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer PwC
India India
I am a Software developer having an experience of 5 years in application development. To get me you can mail me at arkadeepde@gmail.com or you can visit my blog at ASP With Arka

Comments and Discussions

 
-- There are no messages in this forum --