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,724
Last edited:

GMan

Well-Known Member
Licensed User
Longtime User
But still nothing is transferred...the RX-Led blinks, but the format should be wrong.
 

karthikeyan

New Member
hello friends,
am new to b4c. i tried this blutooth admin tutorial. i have installed this apk to my tablet. when i am trying to connect the found device, i received an error message like- unable to start service discovery. please help me to fix this issue. am using the code provided in this tutorial. thank you
 

GMan

Well-Known Member
Licensed User
Longtime User
Hoi,
which BT-Modul are you using and what is the version of your Android-OS ?
 

pin71

Member
Licensed User
Longtime User
Hi Erel,

Is it possible to have 3 devices connected?
1 master and 2 slaves?

Thanks
 
Last edited:

pin71

Member
Licensed User
Longtime User
Yes. You need to create a Serial object for each connection.
Thank you, but should I use only ListenInsecure setting a different Port for each connection, or can I use the regular Listen?

Thanks
 

pablan

Member
Hi,
is there a way to skip automatically the passkey step for pairing to a specific device which has a default passkey (1234)?
Thank you
 

Flosch

Member
Licensed User
Longtime User
I want to talk to a Bluetooth Device and I have to send:
6 214 0 171 15 66 7 (as Byte) to get the device Status.

I tried your Lib and modified your code:

B4X:
Sub btnSend_Click
 
    Dim Poll(7) As Byte
   
    Poll(0) = 6
    Poll(1) = 214
    Poll(2) = 0
    Poll(3) = 171
    Poll(4) = 15
    Poll(5) = 66
    Poll(6) = 7
   
    AStream.Write(Poll)
    LogMessage("Me", " Pol-CMD Send")
 
End Sub

But I didn't received an answer from the device.
I checked the received code with an oscilloscope and figuerd out I will allways send

0 0 7 6 214 0 171 15 66 7

So there is a 0 0 7 in front.

I connected my android to a PC and tried it with H-Term. There is also the 007 in front.
How can I get rid of the 0 0 7?
 

cubetennis

Member
Licensed User
Longtime User
Hi Erel
Where is the new thread for this question?
Could you give me a example how can I send binary data via bleutooth.
Thank you
 

GMan

Well-Known Member
Licensed User
Longtime User
Hoi,
can i shorten the search time when discovering ?
 

GMan

Well-Known Member
Licensed User
Longtime User
OK, i want to stop it after 4 Seconds searching...how can i do that ?
 

GMan

Well-Known Member
Licensed User
Longtime User
Hoi Erel,
thx - that works !
 

GMan

Well-Known Member
Licensed User
Longtime User
Hoi Erel,

did not - the searching stops and teh found device was shown, but cant connect.
I think that the admin.CancelDiscovery returns false ?
 
Status
Not open for further replies.
Top