Android Serial, BluetoothAdmin, & AsyncStreams Oh My ..

CharlesIPTI

Active Member
Licensed User
Longtime User
"Lions Tigers and Bears Oh My "

C'Mon it was funny ... :icon_clap: :confused:

I think I already know the answer but I NEED to be sure..
If Dim admin As BluetoothAdmin

Can I somehow place the callbacks and automations for admin in another code module..
Admin_DiscoveryFinished
Admin_StateChanged
Admin_DeviceFound

Also the Related object Serial. Serial1_Connected

I kind of want these tools and their subs out of my main activity
something like

Dim serial1 As ImASpecialModuleForSerialStuff.Serial
Dim admin As ImASpecialModuleFor_bt_Devices.BluetoothAdmin

But I'm guessing there's no way these "externalized" references can have their callbacks engaged correctly like this..

?? right ??

So I have to repeat this stuff
[ Admin_DiscoveryFinished
Admin_StateChanged
Admin_DeviceFound ]

in another Activity now

and its upsetting to have code duplication
Oh look.... I have to do Serial1_Connected again too Grrrrr :BangHead:
 

CharlesIPTI

Active Member
Licensed User
Longtime User
Clarity Test

So apparently in the Service Start I cant evaluate who's calling the service ( What Activity) so I'm forced to create ANOTHER Service to address this extra bit of functionality


B4X:
Sub Service_Start (StartingIntent As Intent)
    If AsyncScanSrv.IsInitialized Then AsyncScanSrv.Close  ' In case the connection was broken and restarted
    AsyncScanSrv.Initialize(postMain.scanSerial.InputStream, postMain.scanSerial.OutputStream, "AsyncScanSrv")            
End Sub

Is there some way I can get
B4X:
 varActivityName =  StartingIntent."What Activity called me"

so I can evaluate it to select the correct initialize string, for specific Activities??
B4X:
If varActivityName = ActivityBlack Then
    AsyncScanSrv.Initialize(ActivityBlack.scanSerial.InputStream, _
      ActivityBlack.scanSerial.OutputStream, "AsyncScanSrv")   
Else If  varActivityName = ActivityGreen Then
    AsyncScanSrv.Initialize(ActivityGreen .scanSerial.InputStream, _
      ActivityGreen.scanSerial.OutputStream, "AsyncScanSrv")
End If
 
Upvote 0

CharlesIPTI

Active Member
Licensed User
Longtime User
Indirection

Presently these are duplicated in the respective activities

  • Admin_DiscoveryFinished
  • Admin_StateChanged
  • Admin_DeviceFound
  • Serial_Connected

BlueToothAdmin & Serial connection methods

Can these be placed in a module and the module called by the respective
activities to avoid duplication of these methods in the respective activities?

I don't immediately see how the transfer of the serial connection to the independent module would be accomplished.---please help ??
:sign0085:
I think I understand the Bluetooth concern as per your answer, but again for placing it into this "independent" module, referencable by many activities,, I'm not quite "Seeing" the structure just yet.

Appreciate any and all insight

thank you
 
Upvote 0

CharlesIPTI

Active Member
Licensed User
Longtime User
More Please

Hi Erel, thanks for the swift response.




So this "custom class" will have
Admin_DiscoveryFinished
Admin_StateChanged
Admin_DeviceFound
Serial_Connected

it will function as basically my Universal --Blue Tooth and serial- class. ?
And, any activity can call the Service that "Hosts" these utilities ???

Is this correct ....? :(
 
Upvote 0

CharlesIPTI

Active Member
Licensed User
Longtime User
Confusing

Yesterday 01:49 AM
Erel You should use a Service and a custom class to handle the Bluetooth tasks.
The code will be written in the class. The service will hold the class instance. This way the class instance will always be running (after the service was started).
-------------------------------------------------------------
Q: Charles And, any activity can call the Service that "Hosts" these utilities ???
A:Erel : Other modules should call the class not the service.
------------------------------------------------------------


if the service Holds an instance of the class I need to fire up an instance of the service which should auto init the bluetooth work (Class) & "allow barcode scans" but where's the serial object get created ..?? in the activity or in this hosted class?

What's going on in the Service_Start?
Should the Service_Start reference its hosted class instance??
the Serial_Connected goes .. in the hosted class ??
B4X:
Sub Service_Start (StartingIntent As Intent)
 If blueService.IsInitialized Then blueService.Close  ' In case the connection was broken and restarted
 blueService.Initialize(clsTestObj.scanSerial.InputStream, clsTestObj.scanSerial.OutputStream, "blueService")      
End Sub


Lastly whats happening in the serial_connected if success = true

B4X:
   Sub scanSerial_Connected (Success As Boolean)
      ProgressDialogHide
      Log("connected: " & Success)
      If Success = False Then
         Log(LastException.Message)
         ToastMessageShow("Error connecting: " & LastException.Message, True)
      Else
           '''''StartService(AsyncScanSrv)  
      End If
   End Sub
 

Attachments

  • BlueClassSerial.zip
    8.8 KB · Views: 366
Upvote 0

CharlesIPTI

Active Member
Licensed User
Longtime User
Slow

OK We can go slow then...

B4X:
Sub Activity_Create(FirstTime As Boolean)
StartService(blueService)  
Activity.LoadLayout("loMain")
End Sub


Sub Service_Create

clsTestObj.Admin.Initialize(EventName)
If blueService.IsInitialized Then blueService.Close  ' In case the connection was broken and restarted
 blueService.Initialize(clsTestObj.scanSerial.InputStream, clsTestObj.scanSerial.OutputStream, "blueService")      
 
End Sub

Im afraid I don't see this. What eventname of the Bluetooth hosted in the clsTestObj gets referenced in the intialize
 
Upvote 0

CharlesIPTI

Active Member
Licensed User
Longtime User
yea no

Perhaps the async stream, Bluetooth and serial are making this too complicated... lets try it with just a web service and the HTTPUtil modules..
please if that is possible. I will actually need to understand both but this may be easier to begin with.

GOAL that any activity can access the running service which in this case houses a class that has the specific web service methods/ subs but is actually using the HTTPUtil module's features to accomplish the work...

because I'm not seeing this yet... it wont even load up my test application to debug it..


Today 07:48 AM
Erel You should initialize Admin inside the class code (and handle the event inside).
The

CharlesIPTI-- respectfully, the Bluetooth class Named Admin is hosted by the "class" named clsTestObj.
  • Below,,,, you say init the bluetooth class in Service_Create but
  • above you say init "Admin" (the Bluetooth class again) in the class code

Today 12:38 AM
Erel You should start the service when your app starts and in Service_Create you should initialize the bluetooth class. You should not touch this service anymore. From now on only work with the bluetooth class.


In Service Create I cannot init the class nor its contained bluetooth class
clsTestObj.Admin.Initialize(EventName)
 

Attachments

  • wsCentralTest.zip
    10.5 KB · Views: 280
Upvote 0

CharlesIPTI

Active Member
Licensed User
Longtime User
switch UP

I revisited the original concern and experienced some success BUT now I do not know how to implement the serial object.

B4X:
Compiling code.                         0.03
Compiling layouts code.                 0.02
Generating R file.                      0.00
Compiling generated Java code.          Error
B4A line: 118
MsgReceived = BytesToString(Buffer, 0, Buffer.Length, \
javac 1.6.0_31
src\test\classes\clstest.java:203: cannot find symbol
symbol  : variable processBA
location: class test.classes.clstest
         processBA.setLastException(e); };
         ^
1 error


B4X:
LogCat connected to: 43C800242BF1157
--------- beginning of /dev/log/system


--------- beginning of /dev/log/main


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


** Activity (main) Resume **
** Service (blueservice) Create **
** Service (blueservice) Start **
DARININSPIRON:00:19:7E:DB:3B:83
IPTILT12:40:2C:F4:8E:6C:36
** Service (blueservice) Destroy **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Service (blueservice) Create **
** Service (blueservice) Start **
DARININSPIRON:00:19:7E:DB:3B:83
4820I:00:22:58:39:06:46
IPTILT12:40:2C:F4:8E:6C:36
connected: true
** Service (blueservice) Destroy **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Service (blueservice) Create **
** Service (blueservice) Start **
IPTILT12:40:2C:F4:8E:6C:36
DARININSPIRON:00:19:7E:DB:3B:83
4820I:00:22:58:39:06:46
connected: true
** Service (blueservice) Destroy **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Service (blueservice) Create **
** Service (blueservice) Start **
4820I:00:22:58:39:06:46
IPTILT12:40:2C:F4:8E:6C:36
DARININSPIRON:00:19:7E:DB:3B:83
connected: true
** Service (blueservice) Destroy **


Previously this method started the service
from the activity but the StartService line is now commented out in the "Class" Object..

B4X:
   ' Note scanSerial is Serial Connection While AsyncScanSrv is Service to invoke
   Sub scanSerial_Connected (Success As Boolean)
      ProgressDialogHide
      Log("connected: " & Success)
      If Success = False Then
         Log(LastException.Message)
         ToastMessageShow("Error connecting: " & LastException.Message, True)
      Else
           StartService(AsyncScanSrv)  
      End If
   End Sub

The error line 115 is the MsgReceived UTF 8 I have tried both types

B4X:
   Sub AsyncScanSrv_NewData(Buffer() As Byte)    
   
      Try   
            'MsgReceived = BytesToString(Buffer, 0, Buffer.Length, "ISO-8859-1")
            MsgReceived = BytesToString(Buffer, 0, Buffer.Length, "UTF-8")            
'            MsgReceived = MsgReceived.Trim
      Catch                         
            ''Msgbox("ERROR: " & LastException.Message ,"ERROR Scan")         
            'Msgbox("plpppl" , "ZZZZZZZZZZZZZ")
      End Try
      
      If IsPaused(Main) = False Then 
         CallSub(Main, "NewMsgFromAsyncScanSrv")
      End If
      
End Sub
 

Attachments

  • BlueTest_Serial.zip
    9 KB · Views: 349
Upvote 0

CharlesIPTI

Active Member
Licensed User
Longtime User
Progress -ish - esque - kinda

OK OK ok ok I figured some of this out,,
In the activity you start the service instance.
also in the activity you call a method on AN INSTANCE of the class.

The service -- in my test its service_start, but if I recall correctly it could be done in service_create, you initialize an instance of the class.

The activity calls a method of its instance of the class for scanning whatever...
So the class has the serial the bluetooth and the asyncstreams all as class globals (Thats MUCH MUCH Better than process globals anywhere ).

Im doing nothing in the class' initialize sub, but all the bluetooth methods ( OK Subs) the serialize connect and the new data for the asyncStream are all in this class.. the only problem I'm having now is if you place error validation around the new data code you get that error from previous post
"cannot find symbol symbol : variable processBA"

As you clearly see this is test but if you use the version with try catch around it you get the error. I even put a fresh set of try catch around the new "BytesToString" still it fails ....

B4X:
   Sub AsyncScanSrv_NewData(Buffer() As Byte)    
      
      Dim msg As String
       
'      Try   
''            MsgReceived = BytesToString(Buffer, 0, Buffer.Length, "ISO-8859-1")
'            MsgReceived = BytesToString(Buffer, 0, Buffer.Length, "UTF-8")            
'''            MsgReceived = MsgReceived.Trim
'      Catch                               
'            ToastMessageShow("Error connecting: "  &"ERROR, True)
'      End Try
      

       msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
         MsgReceived = msg.Trim
   
               ' LogError

      
      
      
      If IsPaused(Main) = False Then 
         CallSub(Main, "NewMsgFromAsyncScanSrv")
      End If
      
End Sub


So will this concept also work for HttpUtil like I described previously..??
http://www.b4x.com/forum/basic4andr...othadmin-asyncstreams-oh-my-2.html#post108159
all Activities call into a "PROXY" Class I have made with all the web methods I mean subs. And this class is hosted by a service. And This class uses the HttpUtilities to do its work. ??? How do I push the HttpResults out to a class devoted completely to housing my XML Sax parsers? Does this SaxParser class have to be referenced by the service hosting the webservice proxy class ??
 
Last edited:
Upvote 0

CharlesIPTI

Active Member
Licensed User
Longtime User
new problem SOLVED

I attempted to add a second activity to test using the now centralized features hosted by the service for scanning bluetooth serial asyncstreams ...

like I posted a moment ago how do I determine who called the class's scan method so that I know whom to return the value to in new data?

This Hack below will work but I have to delineate more specifically in the listed subs for each activity.

When there are multiple scan types originating from a single activity, I have created a simple scan type to identify the source of the scan for this situation, so I set that on scan and iterate it in the callback for that activity.


B4X:
  If IsPaused(Main) = False Then 
        CallSub(Main, "NewMsgFromAsyncScanSrv")
    End If


  If IsPaused(BetaActivity) = False Then 
        CallSub(BetaActivity, "NewMsgFromAsyncScanSrv_Too")
    End If


So this will work but how to I do this dance for Web Service calls.. AKA HttpUtil
 
Last edited:
Upvote 0

CharlesIPTI

Active Member
Licensed User
Longtime User
Access Object tree

You should start the service when your app starts and in Service_Create you should initialize the bluetooth class. You should not touch this service anymore. From now on only work with the bluetooth class.

OK But how is the class accessed programmatically?

with the servicename.Class
as it is a property of the service

or..

Does the service make it publicly available by instantiating a process global instance of the class ??

I'm confused :(:D
 
Upvote 0
Top