Android Tutorial Android Bluetooth / BluetoothAdmin Tutorial

Status
Not open for further replies.
Better implementation based on B4XPages:
Tutorial was rewritten in April 2018.
We will create a chat example between two Android devices.

All the non-UI code is implemented in a class named BluetoothManager. It is initialized in Service_Create of the starter service.

It is always better to implement communication related code in a service or a class initialized from a service. This way the communication state is not affected by the activities more complicated state.

The activities call the class methods directly. Calling the activities subs is done with CallSub. Remember that CallSub doesn't do anything if the activity is paused.

The first activity, the main activity, shows two buttons. One for searching other devices and one for listening for connections.

Searching other devices requires a "dangerous" permissions (see the runtime permissions tutorial for more information).
Note that you need to add the permission in the manifest editor:
B4X:
AddPermission(android.permission.ACCESS_FINE_LOCATION)

B4X:
Sub btnSearchForDevices_Click
   rp.CheckAndRequest(rp.PERMISSION_ACCESS_FINE_LOCATION)
   Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
   If Result = False Then
       ToastMessageShow("No permission...", False)
       Return
   End If
   Starter.Manager.SearchForDevices
End Sub

The second button sends an intent to the OS to make the device discoverable and then calls Serial.Listen.

Once a connection is established:
1. AsyncStreams is initialized in prefix mode (see the AsyncStreams tutorial for more information).
2. The ChatActivity is started.

SS-2018-04-04_12.02.01.jpg


Notes

- In this example we send text messages. Use B4XSerializator to send more complex types.
 

Attachments

  • Bluetooth.zip
    12.4 KB · Views: 6,821
Last edited:

Rusty

Well-Known Member
Licensed User
Longtime User
Erel,
Do you have an example of an Astreams implementation of receiving/sending a file over bluetooth?
The chat program works great but I need to send/receive files of 1mb (or so) in size.
Thanks,
Rusty
 

Rusty

Well-Known Member
Licensed User
Longtime User
Thanks Erel.
I anxiously await such an example.
As usual, you, Anywhere Software and the members of your group are great!
With appreciation.
Rusty
 

Rusty

Well-Known Member
Licensed User
Longtime User
Erel,
Your example works great, thank you.
Have you ever connected via Bluetooth to a Bluetooth RS-232 device.
For example, there are devices out there such as Bluetooth heart monitoring devices and others that I need to be able to stream their data to a file via Bluetooth. If so, can you recommend a code example or reference?
Thanks,
Rusty
 

Madf

New Member
Licensed User
Longtime User
Hello.

Tried this example. There is a problem, "txtInput.Text" returns for some reason is not only a text string, but another 4 bytes data length.
How can I get rid of them?
The function "left" does not help. How to cut these unnecessary data? How to transfer a text-only?


ps: in general, this is not BASIC - is a little of BASIC, and the rest is - Java! In BASIC there is no problem with the variables (usually automatically converted) and immediately access all the functions without any ads (then the compiler will optimize the code). (((
 

dschafer77

New Member
Licensed User
Longtime User
Hi Erel, great program.
I am running your bluetooth example, it works when i send text from phone to a bluetooth serial module, but sending any text to the phone forces closed the program.
Do you have any idea what would cause this?
Regards
Doug
 

Jayjay

New Member
Licensed User
Longtime User
Hi Erel,
I have now read through the forum to find a answer to my problem. Its related to the Bluetooth app found in the tutorial.
I noticed that you showed someone else away saying that its not a problem for this tread.
I have B4A working well as well and the bridge program using Bluetooth. However after compiling the example on Bluetooth and deploying it to the phone and tablet i get the same error message. Both devices are Samsung the phone being the S3. the error in question is this
java.io.ioException: Service Discovery Failed.
However there is another app on the net which does nearly the same as your example and works well. unfortnatunely its source is not available and i presume not written in B4A. (BTinterface Free trial version)
I am new to B4A and Bluetooth comms.
 

Jayjay

New Member
Licensed User
Longtime User
spend the day looking at your code on the example and found the source of the java.io.ioException: Service Discovery Failed.
Its in the serial1 Sub. and reports this when unable to connect.Its possible that the BTinterface Free trial version has been written in b4a. it connects to my laptop terminal first time. The example does not always do that i need to manually go online. But i will search the code for answers and in this way learn more.My two devices however once detected connect.Thanks.
 

sanduro

Member
Licensed User
Longtime User
This application works just fine between android devices, but how can I have a client in VB, C# ,processing or whatever, which can be used as another client, e.g. android > pc chat ?

San
 

GMan

Well-Known Member
Licensed User
Longtime User
how can i SEND (i.e. the number "1") via established BT-Connection when tapping on a button ?
 

KitCarlson

Active Member
Licensed User
Longtime User
B4X:
Sub btnOne_Click
    AStream.Write(Array As Byte(49))  ' "1"
End Sub

Make btnOne in designer.
 
Last edited:

GMan

Well-Known Member
Licensed User
Longtime User
I got the AsyncStreamsObject class and use it as
B4X:
Dim AStream As AsyncStreams

But i am using the BT-Example from Erel? and there is no AStream-x needed so far.
Its using a Serial1.xxxx

B4X:
If FirstTime Then
        admin.Initialize("admin")
        serial1.Initialize("serial1")
        txtLog.Initialize ("txtLog")
        txtInput.Initialize ("txtInput")
        BTConXXPicture.Initialize ("BTConXXPicture")
    End If
 
Last edited:

GMan

Well-Known Member
Licensed User
Longtime User
If i try
B4X:
 AStream.Write(Array As Byte(49))  ' "1"
nothing will be send (the indicator led does nothing)

If i use
B4X:
 txtInput.Text = "a"
AStream.Write(txtInput.Text.GetBytes("UTF8"))
the indocator led flashed, but nothing was sent (the monitoring keeps empty).

B4X:
Sub ToggleButton1_Click
If ToggleButton1.Checked Then
'  AStream.Write(Array As Byte(49))  ' "1"
 
  txtInput.Text = ""
  txtInput.Text = "1"
  AStream.Write(txtInput.Text.GetBytes("UTF8"))
  Else
'  AStream.Write(Array As Byte(48))  ' "0"

  txtInput.Text = "" 
  txtInput.Text = "0"
  AStream.Write(txtInput.Text.GetBytes("UTF8"))
End If
 
Last edited:
Status
Not open for further replies.
Top