Android Question Bluetooth Problem: Connection is Broken

PierPaduan

Active Member
Licensed User
Longtime User
Hi All,
I'm using the example app from this tutorial: https://www.b4x.com/android/forum/threads/android-bluetooth-bluetoothadmin-tutorial.14768/

I already succesfully used it to connect a Galaxy S3 with a BT module RN42.
Now I'm trying to connect the same Galaxy with a Microchip BM77 BT module (it's a dual mode module, with both 4.0 BLE and 3.0 classic modules).
I have already performed the scanning from Settings/Bluetooth and I have paired this device.

If I use:
"serial1.Connect(connectedDevice.Mac)"
or "serial1.Connect2(connectedDevice.Mac,"00001101-0000-1000-8000-00805F9B34FB")"
I obtain the error: " java.io.IOException: Connection refused"

If I use:
"serial1.Connect3(connectedDevice.Mac ,1)"
I obtain: "java.io.IOException: Permission denied"

Only if I use:
"serial1.ConnectInsecure(admin,connectedDevice.Mac,1)"
I obtain "connected: true" but after one second start the AStream_Error event and it result in "Connection is Broken".

If I use instead the app "Bluetooth spp pro" from Google Play, it is able to connect to the BM77 and I can chat with it.

I really don't know how to establish a comunication with the BM77.
Someone have suggestions?
Thanks a Lot and Best Regards.

Pier.
 

inakigarm

Well-Known Member
Licensed User
Longtime User
Are you using Asyncstream "prefix mode"? I've tried between B4J and Arduino HC-06 and didn't work.

I had to use Asycnstream non prefix mode for a succesfull connection
 
Upvote 0

PierPaduan

Active Member
Licensed User
Longtime User
Thank for your help inakigarm but I still have the same problem.
Instead of:
AStream.InitializePrefix(Main.serial1.InputStream, True, Main.serial1.OutputStream, "AStream")

I have changed to:
AStream.Initialize(Main.serial1.InputStream, Main.serial1.OutputStream, "AStream")

but the result is the same as above. Or "Connection refused" or "Connection denied" or "connected: true" but after one second start the AStream_Error event and it result in "Connection is Broken".

You or someone else has other ideas on how to obtain a stable connection?
Thanks to everybody.
Pier.
 
Upvote 0

PierPaduan

Active Member
Licensed User
Longtime User
Hi Erel. I think that I don't send anything.

In "Main" activity the connection is created. Then "ChatActivity" is started.
In ActivityCreate of "ChatActivity" initialization of AStream and after that AStream_Error event occour.
Using Initialize or InitializePrefix give the same error.
Please see the attached ChatActivityLog.jpg.
The error is: Software caused connection abort.

I upload the Bluetooth example with the modifications tested. In it There is a ChatActivity2 where I tried to use TextReader and TextWriter instead of Astream but with no success.

Any idea is welcome.
Thanks a lot.
Pier.
 

Attachments

  • ChatActivityLog.JPG
    ChatActivityLog.JPG
    157.3 KB · Views: 336
  • Bluetooth_modified.zip
    70.2 KB · Views: 298
Upvote 0

PierPaduan

Active Member
Licensed User
Longtime User
The use of TextReader and TextWriter in ChatActivity2 was only a little test. Anyway I don't have errors in ChatActivity2 if I don't do anything in it.
The only line that give an error when I try to send is "TextWriter1.Flush" and the error is "java.io.IOException: Connection reset by peer".
If I rem that line and try to send something, I don't receive nothing on the hyperterminal connected to the BM77, and if I write something on the hyperteminal, nothing is received from the app.

Anyway, what do you think about ChatActivity1? What can be the reason of the error: "Software caused connection abort".

Any idea is welcome.
Thanks a lot.
Pier.
 
Upvote 0

PierPaduan

Active Member
Licensed User
Longtime User
In that example I don't find any hello message.
I don't know really what to do for the moment.

I have tested the same example with an RN41 and with "serial1.connect(..." and "AStream.Initialize(..." it works!!!

Have a nice evening.
Thanks a Lot.
 
Upvote 0

PierPaduan

Active Member
Licensed User
Longtime User
Hi.
After the Bluetooth connection is established, ChatActivity starts. After this I don't find in the source code any message that could be sent.

I've tried to initialize the Astream with the following ways:
AStream.InitializePrefix(Main.serial1.InputStream, True, Main.serial1.OutputStream, "AStream")
AStream.InitializePrefix(Main.serial1.InputStream, False, Main.serial1.OutputStream, "AStream")
AStream.Initialize (Main.serial1.InputStream, Main.serial1.OutputStream, "AStream")

, but the result is every time the same: about after 80 - 100ms the AStream_Error Event occours: "Software caused connection abort".
No other events occours.

I think there is a problem in the RandomAccessFile library with the initialization process that is not compatible with the BM77. I use library version 2.00.
Exists a newer version?
Exists other additional libraries that can be used to create an AStream?
Is possible to import 3rd party java libraries to create an Astream?

Anyway the communication with the BM77 works if I use app "Bluetooth spp tools pro" from:
https://play.google.com/store/apps/details?id=mobi.dzs.android.BLE_SPP_PRO&hl=en
I would like to know why.

Any idea is welcome.
Thanks a lot.
Pier.
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
As written above prefix mode will surely not work as your BT device doesn't implement this protocol.

I don't think that the problem is in RandomAccessFile or AsyncStreams. They don't send anything unless you call AStream.Write.
The latest version of RandomAccessFile is v2.10 though it will behave exactly like v2.00.

Exists other additional libraries that can be used to create an AStream?
Is possible to import 3rd party java libraries to create an Astream?
No. AsyncStreams is an object that belongs to RandomAccessFile.

I recommend you to create a small program from scratch, not based on any other example.
 
Upvote 0

Jaume Guillem

Member
Licensed User
Longtime User
I'm using the BM77 with standart Bluetooth and BLE, and booth are working Ok.
In SPP I use AStream.Initialize (serial1.InputStream, serial1.OutputStream, "AStream")

Maybe the main difference with your code is that all the conexions and transmit and receive data, are placed in a service
 
Upvote 0
Top