Android Question AsyncStreams initialisation crashes

rabbitBUSH

Well-Known Member
Licensed User
I have the code shown below. It listens for incoming serial data but will also send a request to the device when this is working.

When this gets to this point --> Log ("******** ASTREAM 1 ****************") - it crashes - prior to that is all fine and connection is established.

I don't need the PREFIX version of AsyncStreams for this purpose - and it will send one character ("?") to request and get a value back from the device.

the abridged unfiltered log is below the code.

Any ideas on a solution?

There again, maybe this is not the way to read/write to a "raw" serially connected device. o_O👁️‍🗨️

AsyncStreams initialisation error:
Private Sub btnReadSerialData_Click

    Private labelNAME As String
    Private labelMAC As String

    clv.Clear
    Log ("****** Pressed serial button *******")
    
    '' read serial data from here......
    '''''    serial.Connect("50:65:83:6F:92:15")

    Log ("******** SERIAL ****************")
    Log (" Serial initialised")
    Log (serial.IsInitialized)
    ''''
    
    serial.Listen

    labelNAME = ConnectedName
    labelMAC = ConnectedMAC
    Label3.text = labelNAME & " " & labelMAC
    Log ("************************")
    
    '''    Found: device-1.0,50:65:83:6F:92:15, RSSI = -42, (MyMap) {1=[B@30e2fe5, -1=[B@fc05aba, 9=[B@6677d6b, 18=[B@e0930c8, 10=[B@976e561, 2=[B@b896886, 0=[B@4fe8547}
'    If AStreams.IsInitialized Then AStreams.Close
'    'prefix mode! Change to non-prefix mode if communicating with non-B4X device.
'    AStream.InitializePrefix(serial.InputStream, False, serial.OutputStream, "astreams")
    ''''
    Log ("************************")
    Log ("*** GET THE PAIRED DEVICE ***")
    ToastMessageShow (" GET THE PAIRED DEVICE ", True)
    
    whosisthere
    Log ("************************")
    
'    Log ("******* Phone *****************")
'    Log(serial.Address) 
'    Log (serial.Name)
'    Log ("************************")
    
'    buffer = "?"
    ''''get stuff from raingauge-1.0 now
    Log ("******** ASTREAM 1 ****************")
'   
AStreams.Initialize(serial.InputStream, serial.OutputStream, "AStreams")
'    AStream.Write(buffer.GetBytes("utf8"))
'    AStreams.Close
    Log ("******** ASTREAM 2 ****************")

    Disconnect
End Sub

******** ASTREAM 1 ****************
b4xmainpage_btnreadserialdata_click (java line: 163)
java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.InputStream android.bluetooth.BluetoothSocket.getInputStream()' on a null object reference
at anywheresoftware.b4a.objects.Serial.getInputStream(Serial.java:256)
at b4a.Raingauge.b4xmainpage._btnreadserialdata_click(b4xmainpage.java:163)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:213)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:8160)
at android.widget.TextView.performClick(TextView.java:16222)
at android.view.View.performClickInternal(View.java:8137)
at android.view.View.access$3700(View.java:888)
at android.view.View$PerformClick.run(View.java:30236)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:246)
at android.app.ActivityThread.main(ActivityThread.java:8587)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
FATAL EXCEPTION: main
Process: b4a.Raingauge, PID: 12167
java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.InputStream android.bluetooth.BluetoothSocket.getInputStream()' on a null object reference
at anywheresoftware.b4a.objects.Serial.getInputStream(Serial.java:256)
at b4a.Raingauge.b4xmainpage._btnreadserialdata_click(b4xmainpage.java:163)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:213)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:8160)
at android.widget.TextView.performClick(TextView.java:16222)
at android.view.View.performClickInternal(View.java:8137)
at android.view.View.access$3700(View.java:888)
at android.view.View$PerformClick.run(View.java:30236)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:246)
at android.app.ActivityThread.main(ActivityThread.java:8587)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
isSocketAllowedBySecurityPolicy start : device null
connect() for device 506583 called by pid: 12167
getBluetoothService() called with no BluetoothManagerCallback
 

rabbitBUSH

Well-Known Member
Licensed User
Start with this example
Thanks I had previously looked through that example a few times. One of the things is that this is not a phone to phone - which I am sure you realize from #1. There is no display on the "other end" to see what is happening there.

I thought I had picked out the relevant processes from the quoted example.
You must connect first.
I realised that the code in #1 shows the serial.connect ("...") as commented out. I just reinstated that and got the same result in the log:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.InputStream android.bluetooth.BluetoothSocket.getInputStream()' on a null object reference .... etc
I do read the "remote" device's NAME, MAC, RSSI etc and its in the log (not the part quoted in #1). So I have a conversation started and its going through serial.connect / serial.listen.

There being no serial.READDATA or serial.WRITEDATA -> it is natural that AsyncStreams has to contain that functionality.

***** I interpret the above log line to mean that there is some issue with the stream from the BLE device and that I have not done something needed to setup the serial.InputStream to satisfy AStreams.Initialize(serial.InputStream, serial.OutputStream, "AStreams"). [[incidentally, after looking at the B4R HC-05 example I changed the last parameter to NULL and received same log result.]] Same might apply to the output stream then as well.

I will study the Chat example from #2 again - - - see what I conclude.

[[The code I am using is a modified version of the BLE example from one of your posting on Bluetooth/BLE.]]

Thanks for the response........
 
Upvote 0

rabbitBUSH

Well-Known Member
Licensed User
Sorry about that - it's probably clear as mud to me as well.

It's BLE.

Haven't had time to look over the other example again. Will find the *connect* event and report back later when things settle down for the day.
 
Upvote 0

rabbitBUSH

Well-Known Member
Licensed User
If it is BLE then the code is completely wrong.
OK - I have it correct now but for as below . . . .

I was going to post a copy of the logs but its become not relevant.

A reminder : the bones of the project are based on this example :
b4x-ble-2-bluetooth-low-energy.
Which is the example shown on the page that you linked in #6.

Connecting isn't the problem (your code has worked properly thanks).

I found the clue (in #65) and realised I should use manager.ReadData an manager.WriteData and not Astreams .....

So, NOW after a lot of back and forth comparisons I have a working code which does WriteData and ReadData to the BLE device. Rx and Tx LEDs flick on the device.
Whoopy !!!!!!!!!!! 💃

Now the challenge is : Where are the data returned to; and, how to access it?

I thought the answer was in #2 of the entire above quoted thread - and so it may be - but I get an error that "AdvertisingData" is undeclared.

Before we go there - the project counts "clicks" (just a simple INT) stored in a variable on the BLE device (bluno beetle). As and ARDUINO it writes and reads to SERIAL. I have another downloaded App that sends the "?" request character and then the number of clicks records to the Arduino serial monitor. (Its a kotlin code - so I don't have clue).


In my new working code the WriteData and ReadData use a service and characteristic I worked out from nRFConnect - and by sheer luck I made the correct guess. I know from the (Arduino) serial monitor that the BLE is getting the ? and sending the count value to its serial port when invoked to do so by the phone user.

I was expecting the B4X Sub Manager_DataAvailable (ServiceId As String, Characteristics As Map) to fire but the log doesn't show that.

In your BLEExample.b4a:
Sub Manager_DataAvailable (ServiceId As String, Characteristics As Map)
    pbReadData.Hide
    Log (">>>>>>>>> ServiceID = " & ServiceId) ' never shows in the log
    clv.Add(CreateServiceItem(ServiceId), "")
    For Each id As String In Characteristics.Keys
        clv.Add(CreateCharacteristicItem(id, Characteristics.Get(id)), "")
    Next
End Sub

How do I get the value returned now that the data write and read are working? (I hope you're not going to say please start a new thread - its part of the same question as in #1 - to me. 🤞)

If I can get past this, have that count returned/accessible on the phone, then, It Might All be in the Bag barring building the front-end.

Thanks again, a normal, for your patience and responses......
 
Upvote 0
Top