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.
Step 1: Change the Application backgorund.
Save this image to use as background in your application. You can use another image if your prefer.
- Click on Upload Now as show on the screenshot and load the background image.
- Click Choose File from open window and browse to the file location.
- Select the image file.
- Click Open
- Click OK
This will display the Image file in the Media Section.
Select Screen1 by clicking on it on the component Section. Change the Screen properties:
- BackgroundColor = None
- BackgroundImage = Select the image from the dropdown list.
- 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)
2. Notifer (Other stuff -> 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 = com.android.settings.Settings$BluetoothSettingsActivity
- ActivityPackage = com.android.settings
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.
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.
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.
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.
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.
This section of code deals with two parts.
- 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.
- 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