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

terwelu

New Member
Licensed User
Longtime User
Have you tried to pair the device from Android bluetooth settings menu?

The passkey might be 1234 or any other value.
Hi Erel,

Yes, with other application, I can connect my Arduino with PC and my Galaxy Tab. The passkey is 0000 and it works for both connections.

Do you think I should try with ConnectInsecure option?

Cheers,
Ricki
 

terwelu

New Member
Licensed User
Longtime User
Hi Erel,

Forgive me for my limited knowledge of device communication :sign0013:, but what is SDP UUID?
And what is the easiest way to test if my device support that method?

Regards,
Ricki
 

ppgirl

Member
Licensed User
Longtime User
How can I handle bluetooth error

Hi Erel,

I make a program for bluetooth stream A/V with serial, It sends data realtime .

but , the connect will be error after moving my mobile sometimes, then raise "Sub AStream_Error" .


1.I try to close AStream and reopen , it looks like Serial1.InputStream/Outputstream is null

AStream.InitializePrefix(Serial1.InputStream, True, Serial1.OutputStream, "AStream")

2.I don't want to serial.connect , because it need a little time

3.How can I handle those error?

thank in advance!
 

ppgirl

Member
Licensed User
Longtime User
Other question

Hi Erel,

I make a program for bluetooth stream A/V with serial, It sends data realtime .

but , the connect will be error after moving my mobile sometimes, then raise "Sub AStream_Error" .


1.I try to close AStream and reopen , it looks like Serial1.InputStream/Outputstream is null

AStream.InitializePrefix(Serial1.InputStream, True, Serial1.OutputStream, "AStream")

2.I don't want to serial.connect , because it need a little time

3.How can I handle those error?

thank in advance!

How can I detect "AsyncStreams" buffer status , byte count ,etc?
 

terwelu

New Member
Licensed User
Longtime User
Each bluetooth connection is identified with a UUID. The default UUID for serial profile is: 00001101-0000-1000-8000-00805F9B34FB

I guess that your device listens to this UUID. However there are devices that listen to other UUIDs. You can only find it in the documentation.
Hi Erel,

I'll try to look for the documentation.
On the other hand, using another android bluetooth app and simple "Hyperterminal monitor", the communication is ok. It's just the app is not open source and moreover, I want to make my own bluetooth communication app with Basic for Android software.

Any other suggestions ?

Cheers,
Ricki
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
How can I detect "AsyncStreams" buffer status , byte count ,etc?
If you are using prefix mode then the event will be raised when there is a full message in the buffer. In standard mode there is no buffer. NewData is raised whenever there is data available.

@terwelu, you should try the other connection options. Also try to unpair the device and pair it again.
 

spcw1234

Member
Licensed User
Longtime User
For the life of me I can not figure out how to use prefix. I am using Pic Basic with a RN-42 bluetooth module. I can see the data fine with prefix disabled, but with prefix enabled it doesn't work. I am simply trying to output 1 byte right now.

Prefix1, prefix2, prefix3, prefix4, mybyte

I seem to be confused on what the prefix is, how to find it, and how to change it on the android app. I try to send a 1 with the following data but it doesn't work.

1,0,0,0,1

or

0,0,0,1,1

Any tips appreciated!
 

spcw1234

Member
Licensed User
Longtime User
That's the problem, from the other side no matter what I send serially the Android app crashes when I have prefix enabled. The prefix is 3 ascii zeros, correct?
 

NeoTechni

Well-Known Member
Licensed User
Longtime User
Is there a way to get raw data from any kind of device? Like say, a video game controller?
 

irda

Member
Licensed User
Longtime User
Ere, Terwelu, I have the same problem with a "Linvor" bluetooth module, I get the same error message, have you been able to find any solution?

I need to contact one of these modules and not how. Many thanks.


Hi Erel,

I'll try to look for the documentation.
On the other hand, using another android bluetooth app and simple "Hyperterminal monitor", the communication is ok. It's just the app is not open source and moreover, I want to make my own bluetooth communication app with Basic for Android software.

Any other suggestions ?

Cheers,
Ricki
 
Status
Not open for further replies.
Top