B4A Library BleExtended

KY Leng

Active Member
Licensed User
Longtime User

Now I can solve the problem of compiling the code by using the API 18.
However, I still cannot connect my phone (LG-E975) with HM-10 BLE.
In the log file, I can only see my Sony Watch and the log is as below:


NotificationPosted, package = com.sonymobile.smartconnect.smartwatch2, id = 4, text = SmartWatch 2 disconnected

(Bundle) Bundle[{android.title=SmartWatch 2, android.subText=null, android.showChronometer=false, android.icon=2130837649, android.text=Disconnected, android.progress=0, android.progressMax=0, android.showWhen=true, android.infoText=null, android.progressIndeterminate=false, android.scoreModified=false}]
Title = SmartWatch 2
** Service (notificationservice) Start **
NotificationRemoved, package = com.sonymobile.smartconnect.smartwatch2, id = 1, text = SmartWatch 2 connected


BLUE4LED, 00:17:EA:926:7F

Connecting...

Discovering services.
** Service (notificationservice) Start **


NotificationRemoved, package = com.sonymobile.smartconnect.smartwatch2, id = 4, text = SmartWatch 2 disconnected

** Service (notificationservice) Start **

NotificationPosted, package = com.sonymobile.smartconnect.smartwatch2, id = 1, text = SmartWatch 2 connected
(Bundle) Bundle[{android.title=SmartWatch 2, android.subText=null, android.showChronometer=false, android.icon=2130837651, android.text=Connected, android.progress=0, android.progressMax=0, android.showWhen=true, android.infoText=null, android.progressIndeterminate=false, android.scoreModified=false}]


Title = SmartWatch 2

and so on...

Do you have any idea ?

Best regards,
 

KY Leng

Active Member
Licensed User
Longtime User
Good morning mh42,

If I use the latest library (BleExtEx_1_10.zip), I got the following log:

Start Discover
BLUE4LED, 00:17:EA:926:7F
Connecting...
.
.
inlib onConnectionStateChange 133 2
Discovering services.


I do not know what is the reason that I cannot connect to my HM-10 BLE.

Best regards,
 

KY Leng

Active Member
Licensed User
Longtime User
Good morning Erel,

I try the library of mh42. If I change the code on line 73
from: Manager.Connect(MacAddress, False)
to: Manager.Connect(MacAddress, True)

I can connect but with error as below

** Activity (main) Create, isFirst = true **
** Activity (main) Resume **

Start Discover

HMSoft, 00:17:EA:92:CD:CE

Connecting...
inlib onConnectionStateChange 0 2

Discovering services.
Connected

Service[0]: 00001800-0000-1000-8000-00805f9b34fb

C[0] : Uuid 00002a00-0000-1000-8000-00805f9b34fb
C[0]: Permissions
C[0]: Properties READ
C[0]: WriteType TYPE_DEFAULT
C[1] : Uuid 00002a01-0000-1000-8000-00805f9b34fb
C[1]: Permissions
C[1]: Properties READ
C[1]: WriteType TYPE_DEFAULT
C[2] : Uuid 00002a02-0000-1000-8000-00805f9b34fb
C[2]: Permissions
C[2]: Properties READ WRITE
C[2]: WriteType TYPE_DEFAULT
C[3] : Uuid 00002a03-0000-1000-8000-00805f9b34fb
C[3]: Permissions
C[3]: Properties READ WRITE
C[3]: WriteType TYPE_DEFAULT
C[4] : Uuid 00002a04-0000-1000-8000-00805f9b34fb
C[4]: Permissions
C[4]: Properties READ
C[4]: WriteType TYPE_DEFAULT
Service[1]: 00001801-0000-1000-8000-00805f9b34fb
C[0] : Uuid 00002a05-0000-1000-8000-00805f9b34fb
C[0]: Permissions
C[0]: Properties EXTENDED_INDICATE
C[0]: WriteType TYPE_DEFAULT
Service[2]: 0000ffe0-0000-1000-8000-00805f9b34fb
C[0] : Uuid 0000ffe1-0000-1000-8000-00805f9b34fb
C[0]: Permissions
C[0]: Properties NOTIFY READ WRITE
C[0]: WriteType TYPE_DEFAULT
AanzahlServices3
Service[2]: 0000ffe0-0000-1000-8000-00805f9b34fb
read: 0000ffe1-0000-1000-8000-00805f9b34fb

java.util.NoSuchElementException
at java.util.LinkedHashMap$LinkedHashIterator.nextEntry(LinkedHashMap.java:350)
at java.util.LinkedHashMap$EntryIterator.next(LinkedHashMap.java:376)
at java.util.LinkedHashMap$EntryIterator.next(LinkedHashMap.java:374)
at anywheresoftware.b4a.objects.collections.Map$MyMap.getEntry(Map.java:215)
at anywheresoftware.b4a.objects.collections.Map$MyMap.getValue(Map.java:198)
at anywheresoftware.b4a.objects.collections.Map.GetValueAt(Map.java:106)
at b4a.example.main._ble_connected(main.java:453)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
at anywheresoftware.b4a.BA$3.run(BA.java:320)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5476)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
java.util.NoSuchElementException


ble_CharacteristicRead

Data: 0
Message 0
write Val : 0

And I also see the error message box on the phone. If I ignore and click 'Continue',
and then click button 'Send', I see the error as below again:


java.lang.RuntimeException: Object should first be initialized (BleCharacteristic).
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:49)
at anywheresoftware.b4a.objects.BleManager$GattCharacteristic.SetIntValue(BleManager.java:457)
at b4a.example.main._button3_click(main.java:694)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:163)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:159)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:77)
at android.view.View.performClick(View.java:4640)
at android.view.View$PerformClick.run(View.java:19421)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5476)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
java.lang.RuntimeException: Object should first be initialized (BleCharacteristic).


Message 0
write Val : 0


java.lang.RuntimeException: Object should first be initialized (BleCharacteristic).
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:49)
at anywheresoftware.b4a.objects.BleManager$GattCharacteristic.SetIntValue(BleManager.java:457)
at b4a.example.main._button3_click(main.java:694)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:163)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:159)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:77)
at android.view.View.performClick(View.java:4640)
at android.view.View$PerformClick.run(View.java:19421)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5476)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
java.lang.RuntimeException: Object should first be initialized (BleCharacteristic).


When I try to disconnect, it is OK.

Disconenct OK

inlib onConnectionStateChange 0 0

_disconnect
Close OK
ble_Disconnected


So, could you please tell me what is the problem? I try with 2 phones (Samsung SM-P601, and LG-E975) and it is the same. I try to change the SDK to APK Level 20, and it is still the same.
My Android version is version 4.4.2 (both of them).

If I try with app on Play Store (Blue Light), it can send the data to HM-10 BLE.

Looking forward to hearing from you soon,
Best regards,
 

KY Leng

Active Member
Licensed User
Longtime User
OK, Erel,

For HM-10, I change the code in the Sub ble_Connected as below:

n = 2
Log ("AanzahlServices" & Services.Size)
s = Services.GetValueAt(n)
Log("Service[" & n & "]: " & Services.GetKeyAt(n))

cr = 0
c = s.GetCharacteristics.GetValueAt(cr)
Log("read: " & c.Uuid)
Manager.ReadCharacteristic(c)
Manager.SetCharacteristicNotification(c, True)

cw = 0
cwrite = s.GetCharacteristics.GetValueAt(cw)
Log("Cwrite: " & cwrite.Uuid)


And everything is fine.
Now, I can send x from [0 to 255]
but I don't know whether we can only send data 1 byte a time or can send string as well.

Hope this can be a help for other who have the same problem as me.

Best regards.
 

bioident

Member
Licensed User
Longtime User
I have a problem.
I use BLE Extended library.

I don't get characteristic from BLE Device.
This is a Log:

** Activity (main) Create, isFirst = true **
(Main, 31) FullScreen or IncludeTitle properties in layout file do not match the activity attributes settings. (warning #1004)
BLE Supported: true
** Activity (main) Resume **
null, D6:FC:13:0F:37:5D
Connecting...
Discovering services.


Phone: Samsung S5 , Android: 4.4.2
 

KY Leng

Active Member
Licensed User
Longtime User

For me, I change the code on line 73 (without any modification of the example from mh42)
from: Manager.Connect(MacAddress, False)
to: Manager.Connect(MacAddress, True)

and it solve my problem.
 

KY Leng

Active Member
Licensed User
Longtime User
With the library from mh42, now I can send the data as stated in post #84 of mine.
However, I got some problem when sending string (just about 10 to 20 characters).

When I cut the string and send it one by one, it is fine but with the constraint that
I must put the timer to send not faster than 200ms interval => slow.

Moreover, it cannot receive data send from MCU to the HM-10 BLE module with normal 9600 bps rate.

Anyone know how to deal with this problem?

The attached project is workable with slow speed data (1 character every 200ms).
Hope this can help some other too.

Thank you in advance.
 

Attachments

  • Bluetooth-USART-Serial Terminal-BLE.zip
    28 KB · Views: 434
Last edited:

gelay

Member
Licensed User
Longtime User

Hi there

For speed up the write process you should use write without response method (if the characteristic supports it) the default is "write with response".

B4X:
 cwrite = s.GetCharacteristics.GetValueAt(cw)  ' the second characteristic is the write characteristic
       Log("Cwrite: " & cwrite.uuid)
      
       Connect = cstateConnect
        
   cwrite.WriteType = 1 'write without response

Now we are using our own BLE module with our own firmware on it and we are able to send with the speed up to 56700kbps, the ble packet size is limited to 20 bytes so don't even try to send more in one package or you have to split it, as we do.... but if you send only 1 byte in the package your throughput will be 1/20...so....
 

KY Leng

Active Member
Licensed User
Longtime User

Thank you very much gelay,

Now, it work fine for sending, however, I still have problem when there are many HM-10 BLE active at the same time, because in the example of mh42, there is no code to retrieve all the MAC address into the list as Erel did for normal Bluetooth library.
Another big problem is that: It take very long time to connect with the HM-10 and connect/disconnect cannot be made so quick. I do not know whether it is the behaviors of the BLE or Android OS because the same HM-10 BLE can connect/disconnect with the iPhone very quick.

Thank you again.
Best regards,
 

CHK

Member
Licensed User
Longtime User

I am working on a converter box, which shall translate commands received via BLE from an Android smartphone to other communication protocols. The converter box will use a Microchip RN4020 module. When I first read Denny Hopp's post, I was thinking ... done deal, I just have to sit and wait … But unfortunately the clients were pressing and I had to use my own brains. With the help of this forum and thanks to my predecessors in this thread, I finally figured out how to set up the RN4020 and how to connect to it via my smartphone. Now I want to share my experiences so far, perhaps paying back some of the debts I owe to others here.

I use a Microchip RN4020 PICTAIL (photo attached). It consists of a RN4020 module which can be accessed via an USB connection. I have connected it to my PC (Win7, 32 bit) and it installed as COM Port, without any problems. As terminal software I use Termite 2.9 by CompuPhase. With this I can send and receive data to and from the module, in particular commands to configure the module as needed, via USB.

The other part of my development setup forms a Samsung Galaxy S 3 neo, with a terminal app I have written, using mkh42's blueSenddata_V2 example as a template (many thanks to him). With this I write and read strings of bytes to and from the RN4020, via BLE.

At first I wanted to use the MLDP feature of the RN4020, which is said to allow a kind of direct data connection. But due to the almost total lack of information from Microchip on this subject, I could not figure out, how to use it. So I did it the classic way using a private service. The maximum length of one transmission is therefore limited to 20 bytes. Longer strings are not supported by the RN4020. In my project this does not matter.

The details can be learned from the attachments to this post:
  • Description how to set up the RN4020 module
  • Screenshot of the terminal app
  • The B4A project of the terminal app. The app can send and receive raw byte and hex data. I have tried to comment the source code as much as feasible.
  • Screenshot of the B4A log after a successful connection


Best regards, CHK

After working a while with the BLE Terminal, I realized, that my conversion of bytes to string is not correct. It works fine for byte values below 128, but for values between 128 - 255 the conversion is false. The reason is, that I have used the function StringToBytes from the ByteConverter library. This conversion is done using a certain encoding, and the encoding I have chosen translates byte values above 127 into 2 byte strings. Unfortunately I could not find another encoding, which does the translation in the desired way. So I changed the BLE Terminal at this point. I do no longer use the SetStringValue function, but the SetIntValue function, writing consecutive int values instead of a single string. The conversion of read bytes above 127 back into characters for displaying still remains a bit arbitrary, depending on the encoding used.

I post here a second version BLE_Term_2 with the corrected conversion routines.

Regards, CHK
 

Attachments

  • RN4020_setup.pdf
    223.3 KB · Views: 416
  • BLE_Terminal.zip
    397.3 KB · Views: 417
  • BLE_Term_2.zip
    8.3 KB · Views: 373
Last edited:

bioident

Member
Licensed User
Longtime User

Hi,

I get error like this:

LogCat connected to: B4A-Bridge: samsung SM-G900F
--------- beginning of /dev/log/main
Installing file.
** Activity (main) Pause, UserClosed = false **
PackageAdded: package:b4a.example
** Activity (main) Create, isFirst = true **
(Main, 43) FullScreen or IncludeTitle properties in layout file do not match the activity attributes settings. (warning #1004)
** Activity (main) Resume **
Start Discover
null, DE:0A:8B:E2:9B:4B
Connecting...
inlib onConnectionStateChange 133 2
Discovering services.

Why ?
 

CHK

Member
Licensed User
Longtime User
Why I have this problem?
what should I to do ?

Hi Bioident,

what errors do you mean ? You posted the beginning of the program log. There are no error messages in it (besides the warning #1004). Does the program get stuck after displaying these messages ? Or what else ?

About warning #1004 you can find all kind of informations by searching for 'warning #1004' in this forum.

Regards, CHK
 

bioident

Member
Licensed User
Longtime User
Hi,

I try use BLE_Term_2 but I get errors.
Why ? What is wrong .
########################################
Installing file.
** Activity (main) Pause, UserClosed = false **
PackageAdded: package:BLE2.terminal
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Start Discover
null, D6:FC:13:0F:37:8D
Connecting...
null, CC:9F:4F:E4:2F:45
Connecting...
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
inlib onConnectionStateChange 133 0
Close failed gatt is null
ble_Disconnected
###################################################
 

CHK

Member
Licensed User
Longtime User

Hi bioident,

seems that you cannot connect to your device. Your log reports 2 devices as found. BLE-Terminal tries to connect to the first one (see line 111 in source code). Are you sure, the first device found is the correct one ? If it is the correct one, you have probably not set up the correct characteristics in the device (see line 128 in source code). The code, as it is, requires an exact setup of the bluetooth device you want to connect with (see line 5 of the source). You have to exactly match the setup of the device and the provisions in the source code. It is a development tool after all, not a finalized program.

Regards,

CHK
 

Philip Prins

Active Member
Licensed User
Longtime User
I would like to retrieve the RSSI while discovering;

Public Constructors
public ScanResult (BluetoothDevice device, ScanRecord scanRecord, int rssi, long timestampNanos)

Added in API level 21
Constructor of scan result.

Parameters
device Remote bluetooth device that is found.
scanRecord Scan record including both advertising data and scan response data.
rssi Received signal strength.
timestampNanos Device timestamp when the scan result was observed.

Is this possible, now i have to connect to retrieve the RSSI.

Regards
Philip

 

tmav

Member
Licensed User
Longtime User
I am also interesting for retrieving the RSSI during BLE discovery.
Is this possible with this or another ble library?
 

Philip Prins

Active Member
Licensed User
Longtime User
I am also interesting for retrieving the RSSI during BLE discovery.
Is this possible with this or another ble library?

I found a library that can do this;
B4X:
Sub BLE_DeviceFound (Name As String, MacAddress As String,rssi As Int, scanRecord() As Byte)
   Dim i As Int
   Dim sBatt As String = "0", sTemp As String = "0", sHumidity As String = "0"
   For i = 0 To scanRecord.Length -1
       If (i+7 < scanRecord.Length) Then
           If (scanRecord(i) = 7 AND scanRecord(i+1) = 22 AND scanRecord(i+2) = 0 AND scanRecord(i+3) = -80) Then
           'scanRecord[i]==0x07 && scanRecord[i+1]==0x16 && scanRecord[i+2]==0x00 && scanRecord[i+3]==0xB0
                   sBatt = scanRecord(i+7)
                sHumidity = scanRecord(i+6)
                sTemp = scanRecord(i+5)
            End If
       End If
   Next
   ListView1.AddTwoLines2(Name,rssi & " dBm: batt " & sBatt & " %: temp " & sTemp & " c: humi " & sHumidity & " %", MacAddress)
   'manager.Connect(MacAddress, True)
End Sub
 

Attachments

  • BLEManager_RSSI_Batt.zip
    243.2 KB · Views: 386
Cookies are required to use this site. You must accept them to continue using the site. Learn more…