App Inventor and the Bluetooth Component


A simple Bluetooth Chat with App Inventor 2

This App Inventor 2 example is based on the following 2 examples:

I now tried to set up an App Inventor 2 example as simple as possible. As in the other examples, both the bluetooth client and server modules are used to create a chat session. Both components are in the same app. The client component will be used for the device which starts the connection, the server component will be used for the device accepting the connection. The devices must have been paired first. There is no searching for available chat clients. The example only works to send messages between 2 devices.

Screenshots

Do you need help with your App Inventor project?

I can help you: for one hour I charge 25 USD.
Please contact me at info [at] puravidaapps [dot] com for details!
For questions about App Inventor, please use the App Inventor forum. Thank you.

App Inventor Blocks

Initialization

Set the nickname to a random user name, general initialization.
The procedure print will print the current text to the label while the previous text will scroll down.

Client: Connect

In the listpicker.BeforePicking event the listpicker will be initialized with the MAC addresses and names of the available paired devices. After selecting a MAC address, the server module will stop listening for further connection requests. In the screen title "Role=Client" will be displayed.

The procedure connected (parameter=true) will enable the Send button, disable the Connect listpicker, start the Clock and enable the Disconnect button.




Server: Connection accepted

If the app receives a connection request, it accepts it. In this case in the screen title "Role=Server" will be displayed.

Timer: Check if new messages have been sent

Every second it will be checked, if there has been received some text. If yes, it will be displayed in the result label. Depending on role=server or client, different blocks have to be used.

Disconnect

The procedure connected (parameter=false) will disable the Send button, enable the Connect listpicker, stop the Clock component and disable the Disconnect button.

Other blocks used

The Screen.ErrorOccurred block catches errors and displays them in the result label.

Test

Tested successfully on HTC Desire running Android 2.2 and Samsung Galaxy Tab 10.1N running Android 3.2.

Known Issues

Questions and Answers

Q1: My problem is to connect the Android Device in a way that the PC thinks it ist a BT keyboard (i have a real one that works and i know how to pair devices) and receives keystrokes that finally go to word processors etc.
A: See this thread and use PC Wedge and Datasnip serial to keybord redirection on your Windows PC.

Q2: I need to create a Multiple bluetooth connections to multiple devices to implement a "chat room". I don't know how to make it?
A: The Bluetooth component of App Inventor only offers communication between 2 devices. You could use several bluetooth server and client components, but this will be very cumbersome. For a chat room, I therefore recommend you to use the web component instead, see also my Chat Demo with App Inventor 2 and PubNub.

Download


Developing and maintaining snippets, tutorials and extensions for App Inventor takes a lot of time.
I hope it saved some of your time. If yes, then you might consider to donate a small amount!

Donation amount:

or donate some mBTC to Bitcoin Address:
1Jd8kXLHu2Vkuhi15TWHiQm4uE9AGPYxi8
Bitcoin

Thank you! Taifun
 

Download aia file for App Inventor
Back to top of page ...

Creative Commons License
This work by Pura Vida Apps is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License
with attribution (name=Pura Vida Apps and link to the source site) required.


Home | Snippets | Tutorials | Extensions | News | Forums | Links | Search | Contact