Android tutorial part 4: Adding the pair bluetooth option in the App

GOAL: Add a background to the Android application and Change Screen Orientation to Landscape + upgrade the Android application to do following:

  • Automatically connect to Bluetooth when application is opened.
  • Check if Bluetooth is ON, request user to Turn Bluetooth ON if Bluetooth is OFF
  • Allow User to Pair to the Bluetooth.

Android Bit:
Step 1: Change the Application backgorund.

Save this image to use as background in your application. You can use another image if your prefer.

Image 64
  1. Click on Upload Now as show on the screenshot and load the background image.
  2. Click Choose File from open window and browse to the file location.
  3. Select the image file.
  4. Click Open
  5. Click OK

This will display the Image file in the Media Section.

image 64

Select Screen1 by clicking on it on the component Section. Change the Screen properties:

  1. BackgroundColor = None
  2. BackgroundImage = Select the image from the dropdown list.
  3. Click OK

The Image will appear on the screen as background Image.

Step 2: Upgrade Bluetooth Paring and Automatic Connection.

Add four invisiable components and change the properties as below.

1. TinyDB (Basic -> TinyDB)

  • Rename = db_MACAddress
2. Notifer (Other stuff -> Notifer)
  • Rename = Notifer
3. Two ActivityStarter (Other stuff -> ActivityStarter)
  • Rename ActivityStarter 1 = as_TurnONBT
  • Rename ActivityStarter 2 = as_PairDevice
4. Change the property for ActivityStarters as below:

  • Action = android.bluetooth.adapter.action.REQUEST_ENABLE
  • Action = android.intent.action.MAIN
  • ActivityClass =$BluetoothSettingsActivity
  • ActivityPackage =
image 86 updated.

Add a Button Component (Basic -> Button). Use HorizentalArrangements (Screen Arrangement -> HorizontalArrangement) to space it nicely on the screen. Change the Button properties as below:

  • Rename = btnBluetoothPair
  • BackgroundColor = Yellow
  • Shape = Rounded
  • Text = Pair Bluetooth
  • Width = 305 pixels
  • Height = 45 pixels

Step 2: Change Screen Orientation.

Change the screen orientation to Landscape to accomodate the layout to tablet screen. 

BlockBuilder Code:

Image 69

Look at the screenshot above. Drag the blocks to code as shown.

1. Create a variable to hold Bluetooth MAC Address.

Drag Screen1.Initialize from the Screen Component. Use a IF Else block to 

2. Check if there are any MAC address in the MACAddress database
3. If not then add the MAC Address to the database.

Image 70

Add a second If Else block as shown in above. This will check if Bluetooth is ON or OFF. If OFF then a application request is sent to turn bluetooth ON.

image 27

Add three level nested If Else blocks and other blocks as show in screenshot above. First If Else block checks if Bluetooth is ON, second If Else block check if there is MAC Address and bluetooth is paired, third If Else connects to the bluetooth and chnage the Connect button text to Disconnect.

Image 72

Insert the next two blocks on teh code as shown above. When the Pair Bluetooth button is clicked, this brings up bluetooth setting screen to pair available bluetooth. 

image 73

This section of code deals with two parts.
  1. First update the List BeforePicking Block to also check if the bluietooth is On and paired. Insert a IF Else block to check if the Bluetooth is ON and Paired. If Not then call   the. Bluetooth setting screen and display message to Turn ON bluetooth.
  2. Second block code on AfterPicking. This saves the bluetooth MAC address to use connecting on when the application is turn on next time. 

This completes this tutorial. Upload and run the application to test it.   

Zircon - This is a contributing Drupal Theme