Model Scenery Tutorials

A Comprenshive Modeler's And Model Railroader's Guide

model scenery center

list picker

Creating an app with MIT App Inventor that connects to an HC-05 Bluetooth module without using MAC addresses involves using the built-in Bluetooth client functionality of App Inventor. Instead of using MAC addresses, you can identify devices by their names. Here’s a step-by-step guide:

1. **Set Up the Project:**
– Open MIT App Inventor and start a new project.
– Design your app layout. Add the following components from the palette:
– A `ListPicker` component (to list available Bluetooth devices)
– A `Label` component (to display connection status)
– A `Button` component (to initiate connection)
– A `BluetoothClient` component (non-visible)

2. **Design the Screen:**
– Arrange the components in the designer view. For example:
– Place the `ListPicker` at the top and rename it to `DevicePicker`.
– Below the `ListPicker`, place the `Button` and rename it to `ConnectButton`.
– Place the `Label` below the `Button` to show the connection status.

3. **Configure the Components:**
– Set the `Text` property of `DevicePicker` to “Choose Device”.
– Set the `Text` property of `ConnectButton` to “Connect”.
– Set the `Text` property of the `Label` to “Status: Not Connected”.

4. **Blocks Editor:**
– Switch to the Blocks editor to add the logic for listing and connecting to Bluetooth devices.

5. **List Available Devices:**
– When the `DevicePicker` is clicked, it should show a list of available Bluetooth devices by their names.

“`blocks
DevicePicker.BeforePicking
// Get the list of paired devices
set global deviceList to BluetoothClient1.PairedDevices
// Create a list of device names from the paired devices
set global deviceNameList to create empty list
for each device in global deviceList
add item to list (select list item (device) index 1) to global deviceNameList
// Set the elements of the ListPicker to the device names
set DevicePicker.Elements to global deviceNameList
“`

6. **Handle Device Selection:**
– After the user selects a device, connect to the selected device.

“`blocks
DevicePicker.AfterPicking
// Get the index of the selected device
set global selectedIndex to DevicePicker.SelectionIndex
// Get the corresponding MAC address from the paired devices list
set global selectedDeviceAddress to select list item (select list item (global deviceList) global selectedIndex) index 2
“`

7. **Connect to the Selected Device:**
– When the `ConnectButton` is clicked, attempt to connect to the selected device.

“`blocks
ConnectButton.Click
if (BluetoothClient1.Connect (global selectedDeviceAddress))
set Label1.Text to “Status: Connected to ” & DevicePicker.Selection
else
set Label1.Text to “Status: Connection Failed”
“`

8. **Finalize the App:**
– Make sure all blocks are connected correctly.
– Test the app on your Android device with the HC-05 Bluetooth module.

### Complete Block Diagram

Here’s a summary of the block diagram in text form:

1. `DevicePicker.BeforePicking`
– Set global `deviceList` to `BluetoothClient1.PairedDevices`
– Initialize global `deviceNameList` as an empty list
– For each `device` in `deviceList`
– Add `select list item (device) index 1` to `deviceNameList`
– Set `DevicePicker.Elements` to `deviceNameList`

2. `DevicePicker.AfterPicking`
– Set global `selectedIndex` to `DevicePicker.SelectionIndex`
– Set global `selectedDeviceAddress` to `select list item (select list item (deviceList) selectedIndex) index 2`

3. `ConnectButton.Click`
– If `BluetoothClient1.Connect (selectedDeviceAddress)`
– Set `Label1.Text` to “Status: Connected to ” & `DevicePicker.Selection`
– Else
– Set `Label1.Text` to “Status: Connection Failed”

This will allow you to select a Bluetooth device by name and connect to it without needing to manually input MAC addresses.

Scroll to top