Android Question about bluetooth spp serial quesion.

ship

Member
Licensed User
Longtime User
hi.i have a question....Astream_newdata() function not work!!!!
what's happen??

my code is:
#Region Module Attributes
#FullScreen: False
#IncludeTitle: True
#ApplicationLabel: BCDtest
#VersionCode: 1
#VersionName:
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region

'Activity module
Sub Process_Globals
Dim serial1 As Serial
Dim foundDevices As List
'Dim Connected As Boolean
'Dim Textreader1 As TextReader
Dim AStream As AsyncStreams
Private bc As ByteConverter
Dim cc As String
'Dim Timer1 As Timer
End Sub

Sub Globals
Dim btnSearchForDevices As Button
Dim btnExit As Button
Dim txtInput As EditText
Dim txtLog As EditText
Dim btnSend As Button
Dim btnKEY As Button
Dim btnID As Button
Dim btnFiger As Button
Dim btnIC As Button
Dim btnMSR As Button
Dim button1 As Button
Dim panel1 As Panel
End Sub

'Sub Timer1_Tick
' If Connected Then
' If Textreader1.Ready Then 'check if there is any data waiting to be read
' Msgbox("have_data!","")
' Msgbox(Textreader1.ReadAll,"")
' Log("cc:" & Textreader1.ReadLine)
' txtLog.Text = txtLog.Text & Textreader1.ReadLine & CRLF
' txtLog.SelectionStart = txtLog.Text.Length
' End If
' End If
'End Sub

Sub AStream_Error
ToastMessageShow("连接中断.", True)
btnSend.Enabled = False
txtInput.Enabled = False
'Textreader1.close
End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout ("1")
If FirstTime Then
serial1.Initialize ("serial1")
'Timer1.Initialize("Timer1", 500)
End If
txtLog.Width = 100%x
button1.Text="KEY1"
panel1.Visible =False
Timer1.Enabled =False
End Sub

Sub AStream_NewData (Buffer() As Byte)
LogMessage("You", BytesToString(Buffer, 0, Buffer.Length, "UTF8"))
End Sub

Sub Activity_Resume
If serial1.IsEnabled =False Then
Msgbox("请打开蓝牙!","")
'Timer1.Enabled =False
Else
serial1.Listen
End If
End Sub

Sub Activity_Pause (UserClosed As Boolean)
If UserClosed Then
AStream.close
Textreader1.close
End If
End Sub

Sub btnExit_Click
AStream.close
serial1.Disconnect
ExitApplication
End Sub

Sub btnSearchForDevices_Click
Dim PairedDevices As Map
Dim i As Long
PairedDevices=serial1.GetPairedDevices
foundDevices.Initialize
For i=0 To PairedDevices.Size-1
foundDevices.Add(PairedDevices.GetKeyAt(i))
Next
Dim res As Int
res=InputList(foundDevices,"选择设备:",-1)
If res<>DialogResponse.CANCEL Then
serial1.Connect(PairedDevices.Get(foundDevices.get(res)))
End If
End Sub

Sub Serial1_Connected (Success As Boolean)
ProgressDialogHide
Log("connected: " & Success)
If Success = False Then
Log(LastException.Message)
ToastMessageShow("Error connecting: " & LastException.Message, True)
panel1.Visible =False
Else
If AStream.IsInitialized = False Then
'AStream.InitializePrefix(serial1.InputStream, True, serial1.OutputStream, "AStream")
AStream.InitializePrefix(serial1.InputStream, False, serial1.OutputStream, "AStream")
End If
panel1.Visible =True
'Textreader1.Initialize(serial1.InputStream)
'Timer1.Enabled =True
Connected=True
'AStream.Initialize (serial1.OutputStream)
End If
End Sub

Sub txtInput_EnterPressed
If btnSend.Enabled = True Then btnSend_Click
End Sub
Sub btnSend_Click
'AStream.Write(txtInput.Text.GetBytes("UTF8"))
AStream.Write(bc.HexToBytes(SubRepalce(txtInput.Text," ","")))
txtInput.SelectAll
txtInput.RequestFocus
LogMessage("Me", txtInput.Text)
End Sub

Sub btnKEY_Click
cc="1a 25 12 41"
AStream.Write(bc.HexToBytes(SubRepalce(cc," ","")))
End Sub

Sub btnID_Click
cc="1a 25 12 42"
AStream.Write(bc.HexToBytes(SubRepalce(cc," ","")))
End Sub

Sub btnFiger_Click
cc="1a 25 12 43"
AStream.Write(bc.HexToBytes(SubRepalce(cc," ","")))
End Sub

Sub btnIC_Click
cc="1a 25 12 44"
AStream.Write(bc.HexToBytes(SubRepalce(cc," ","")))
End Sub
Sub btnMSR_Click
cc="1a 25 12 45"
AStream.Write(bc.HexToBytes(SubRepalce(cc," ","")))
End Sub

Sub button1_click
'Dim buffer() As Byte
'buffer = txtInput.Text.GetBytes("UTF8")'将编辑框里的内容分到数组上
cc="1b 53 46 00 01 0f 0d 0a"
AStream.Write(bc.HexToBytes(SubRepalce(cc," ","")))'写出内容
End Sub

Sub LogMessage(From As String, Msg As String)
txtLog.Text = txtLog.Text & From & ": " & Msg & CRLF
txtLog.SelectionStart = txtLog.Text.Length
End Sub

Sub Byte2Hex(b As Int) As String
Dim hex As String = "0123456789ABCDEF"
Dim l,h As Int
l=Bit.And(b,0xf)
h=Bit.And(Bit.ShiftRight(b,4),0xf) ' <- safety fix
Return (hex.CharAt(h) & hex.CharAt(l))
End Sub

Sub SubRepalce(chain As String, search As String, replaced As String)
If chain.IndexOf(search) = 0 Then Return chain
Dim gauche, droite As String
Do While chain.IndexOf(search)>0
chain = chain.substring2(0,chain.IndexOf(search)) & replaced & _
chain.SubString(chain.IndexOf(search)+search.Length)
Loop
Return chain
End Sub
 

ship

Member
Licensed User
Longtime User
B4X:
#Region Module Attributes
#FullScreen: False
#IncludeTitle: True
#ApplicationLabel: BCDtest
#VersionCode: 1
#VersionName:
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region

'Activity module
Sub Process_Globals
Dim serial1 As Serial
Dim foundDevices As List
'Dim Connected As Boolean
'Dim Textreader1 As TextReader
Dim AStream As AsyncStreams
Private bc As ByteConverter
Dim cc As String
'Dim Timer1 As Timer
End Sub

Sub Globals
Dim btnSearchForDevices As Button
Dim btnExit As Button
Dim txtInput As EditText
Dim txtLog As EditText
Dim btnSend As Button
Dim btnKEY As Button
Dim btnID As Button
Dim btnFiger As Button
Dim btnIC As Button
Dim btnMSR As Button
Dim button1 As Button
Dim panel1 As Panel
End Sub

'Sub Timer1_Tick
' If Connected Then
' If Textreader1.Ready Then 'check if there is any data waiting to be read
' Msgbox("have_data!","")
' Msgbox(Textreader1.ReadAll,"")
' Log("cc:" & Textreader1.ReadLine)
' txtLog.Text = txtLog.Text & Textreader1.ReadLine & CRLF
' txtLog.SelectionStart = txtLog.Text.Length
' End If
' End If
'End Sub

Sub AStream_Error
ToastMessageShow("连接中断.", True)
btnSend.Enabled = False
txtInput.Enabled = False
'Textreader1.close
End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout ("1")
If FirstTime Then
serial1.Initialize ("serial1")
'Timer1.Initialize("Timer1", 500)
End If
txtLog.Width = 100%x
button1.Text="KEY1"
panel1.Visible =False
Timer1.Enabled =False
End Sub

Sub AStream_NewData (Buffer() As Byte)
LogMessage("You", BytesToString(Buffer, 0, Buffer.Length, "UTF8"))
End Sub

Sub Activity_Resume
If serial1.IsEnabled =False Then
Msgbox("请打开蓝牙!","")
'Timer1.Enabled =False
Else
serial1.Listen
End If
End Sub

Sub Activity_Pause (UserClosed As Boolean)
If UserClosed Then
AStream.close
Textreader1.close
End If
End Sub

Sub btnExit_Click
AStream.close
serial1.Disconnect
ExitApplication
End Sub

Sub btnSearchForDevices_Click
Dim PairedDevices As Map
Dim i As Long
PairedDevices=serial1.GetPairedDevices
foundDevices.Initialize
For i=0 To PairedDevices.Size-1
foundDevices.Add(PairedDevices.GetKeyAt(i))
Next
Dim res As Int
res=InputList(foundDevices,"选择设备:",-1)
If res<>DialogResponse.CANCEL Then
serial1.Connect(PairedDevices.Get(foundDevices.get(res)))
End If
End Sub

Sub Serial1_Connected (Success As Boolean)
ProgressDialogHide
Log("connected: " & Success)
If Success = False Then
Log(LastException.Message)
ToastMessageShow("Error connecting: " & LastException.Message, True)
panel1.Visible =False
Else
If AStream.IsInitialized = False Then
'AStream.InitializePrefix(serial1.InputStream, True, serial1.OutputStream, "AStream")
AStream.InitializePrefix(serial1.InputStream, False, serial1.OutputStream, "AStream")
End If
panel1.Visible =True
'Textreader1.Initialize(serial1.InputStream)
'Timer1.Enabled =True
Connected=True
'AStream.Initialize (serial1.OutputStream)
End If
End Sub

Sub txtInput_EnterPressed
If btnSend.Enabled = True Then btnSend_Click
End Sub
Sub btnSend_Click
'AStream.Write(txtInput.Text.GetBytes("UTF8"))
AStream.Write(bc.HexToBytes(SubRepalce(txtInput.Text," ","")))
txtInput.SelectAll
txtInput.RequestFocus
LogMessage("Me", txtInput.Text)
End Sub

Sub btnKEY_Click
cc="1a 25 12 41"
AStream.Write(bc.HexToBytes(SubRepalce(cc," ","")))
End Sub

Sub btnID_Click
cc="1a 25 12 42"
AStream.Write(bc.HexToBytes(SubRepalce(cc," ","")))
End Sub

Sub btnFiger_Click
cc="1a 25 12 43"
AStream.Write(bc.HexToBytes(SubRepalce(cc," ","")))
End Sub

Sub btnIC_Click
cc="1a 25 12 44"
AStream.Write(bc.HexToBytes(SubRepalce(cc," ","")))
End Sub
Sub btnMSR_Click
cc="1a 25 12 45"
AStream.Write(bc.HexToBytes(SubRepalce(cc," ","")))
End Sub

Sub button1_click
'Dim buffer() As Byte
'buffer = txtInput.Text.GetBytes("UTF8")'将编辑框里的内容分到数组上
cc="1b 53 46 00 01 0f 0d 0a"
AStream.Write(bc.HexToBytes(SubRepalce(cc," ","")))'写出内容
End Sub

Sub LogMessage(From As String, Msg As String)
txtLog.Text = txtLog.Text & From & ": " & Msg & CRLF
txtLog.SelectionStart = txtLog.Text.Length
End Sub

Sub Byte2Hex(b As Int) As String
Dim hex As String = "0123456789ABCDEF"
Dim l,h As Int
l=Bit.And(b,0xf)
h=Bit.And(Bit.ShiftRight(b,4),0xf) ' <- safety fix
Return (hex.CharAt(h) & hex.CharAt(l))
End Sub

Sub SubRepalce(chain As String, search As String, replaced As String)
If chain.IndexOf(search) = 0 Then Return chain
Dim gauche, droite As String
Do While chain.IndexOf(search)>0
chain = chain.substring2(0,chain.IndexOf(search)) & replaced & _
chain.SubString(chain.IndexOf(search)+search.Length)
Loop
Return chain
End Sub
#Region Module Attributes
#FullScreen: False
#IncludeTitle: True
#ApplicationLabel: BCDtest
#VersionCode: 1
#VersionName:
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region

'Activity module
Sub Process_Globals
Dim serial1 As Serial
Dim foundDevices As List
'Dim Connected As Boolean
'Dim Textreader1 As TextReader
Dim AStream As AsyncStreams
Private bc As ByteConverter
Dim cc As String
'Dim Timer1 As Timer
End Sub

Sub Globals
Dim btnSearchForDevices As Button
Dim btnExit As Button
Dim txtInput As EditText
Dim txtLog As EditText
Dim btnSend As Button
Dim btnKEY As Button
Dim btnID As Button
Dim btnFiger As Button
Dim btnIC As Button
Dim btnMSR As Button
Dim button1 As Button
Dim panel1 As Panel
End Sub

'Sub Timer1_Tick
' If Connected Then
' If Textreader1.Ready Then 'check if there is any data waiting to be read
' Msgbox("have_data!","")
' Msgbox(Textreader1.ReadAll,"")
' Log("cc:" & Textreader1.ReadLine)
' txtLog.Text = txtLog.Text & Textreader1.ReadLine & CRLF
' txtLog.SelectionStart = txtLog.Text.Length
' End If
' End If
'End Sub

Sub AStream_Error
ToastMessageShow("连接中断.", True)
btnSend.Enabled = False
txtInput.Enabled = False
'Textreader1.close
End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout ("1")
If FirstTime Then
serial1.Initialize ("serial1")
'Timer1.Initialize("Timer1", 500)
End If
txtLog.Width = 100%x
button1.Text="KEY1"
panel1.Visible =False
Timer1.Enabled =False
End Sub

Sub AStream_NewData (Buffer() As Byte)
LogMessage("You", BytesToString(Buffer, 0, Buffer.Length, "UTF8"))
End Sub

Sub Activity_Resume
If serial1.IsEnabled =False Then
Msgbox("请打开蓝牙!","")
'Timer1.Enabled =False
Else
serial1.Listen
End If
End Sub

Sub Activity_Pause (UserClosed As Boolean)
If UserClosed Then
AStream.close
Textreader1.close
End If
End Sub

Sub btnExit_Click
AStream.close
serial1.Disconnect
ExitApplication
End Sub

Sub btnSearchForDevices_Click
Dim PairedDevices As Map
Dim i As Long
PairedDevices=serial1.GetPairedDevices
foundDevices.Initialize
For i=0 To PairedDevices.Size-1
foundDevices.Add(PairedDevices.GetKeyAt(i))
Next
Dim res As Int
res=InputList(foundDevices,"选择设备:",-1)
If res<>DialogResponse.CANCEL Then
serial1.Connect(PairedDevices.Get(foundDevices.get(res)))
End If
End Sub

Sub Serial1_Connected (Success As Boolean)
ProgressDialogHide
Log("connected: " & Success)
If Success = False Then
Log(LastException.Message)
ToastMessageShow("Error connecting: " & LastException.Message, True)
panel1.Visible =False
Else
If AStream.IsInitialized = False Then
'AStream.InitializePrefix(serial1.InputStream, True, serial1.OutputStream, "AStream")
AStream.InitializePrefix(serial1.InputStream, False, serial1.OutputStream, "AStream")
End If
panel1.Visible =True
'Textreader1.Initialize(serial1.InputStream)
'Timer1.Enabled =True
Connected=True
'AStream.Initialize (serial1.OutputStream)
End If
End Sub

Sub txtInput_EnterPressed
If btnSend.Enabled = True Then btnSend_Click
End Sub
Sub btnSend_Click
'AStream.Write(txtInput.Text.GetBytes("UTF8"))
AStream.Write(bc.HexToBytes(SubRepalce(txtInput.Text," ","")))
txtInput.SelectAll
txtInput.RequestFocus
LogMessage("Me", txtInput.Text)
End Sub

Sub btnKEY_Click
cc="1a 25 12 41"
AStream.Write(bc.HexToBytes(SubRepalce(cc," ","")))
End Sub

Sub btnID_Click
cc="1a 25 12 42"
AStream.Write(bc.HexToBytes(SubRepalce(cc," ","")))
End Sub

Sub btnFiger_Click
cc="1a 25 12 43"
AStream.Write(bc.HexToBytes(SubRepalce(cc," ","")))
End Sub

Sub btnIC_Click
cc="1a 25 12 44"
AStream.Write(bc.HexToBytes(SubRepalce(cc," ","")))
End Sub
Sub btnMSR_Click
cc="1a 25 12 45"
AStream.Write(bc.HexToBytes(SubRepalce(cc," ","")))
End Sub

Sub button1_click
'Dim buffer() As Byte
'buffer = txtInput.Text.GetBytes("UTF8")'将编辑框里的内容分到数组上
cc="1b 53 46 00 01 0f 0d 0a"
AStream.Write(bc.HexToBytes(SubRepalce(cc," ","")))'写出内容
End Sub

Sub LogMessage(From As String, Msg As String)
txtLog.Text = txtLog.Text & From & ": " & Msg & CRLF
txtLog.SelectionStart = txtLog.Text.Length
End Sub

Sub Byte2Hex(b As Int) As String
Dim hex As String = "0123456789ABCDEF"
Dim l,h As Int
l=Bit.And(b,0xf)
h=Bit.And(Bit.ShiftRight(b,4),0xf) ' <- safety fix
Return (hex.CharAt(h) & hex.CharAt(l))
End Sub

Sub SubRepalce(chain As String, search As String, replaced As String)
If chain.IndexOf(search) = 0 Then Return chain
Dim gauche, droite As String
Do While chain.IndexOf(search)>0
chain = chain.substring2(0,chain.IndexOf(search)) & replaced & _
chain.SubString(chain.IndexOf(search)+search.Length)
Loop
Return chain
End Sub
</code>
 
Upvote 0

ship

Member
Licensed User
Longtime User
Please use [code]code here...[/code] tags when posting code.

What are you connecting to?
hi.sir.i has copy my code to bbs by code spec.pls help me.thank you very much.
What are you connecting to?
hi.sir.my devices is ATM KEYboard.it's bluetooth serial. 1b 53 46 00 01 0f 0d 0a is keyboard rs232 Command.OK can get somedata.but use this code.i can't get it.
 
Upvote 0

ship

Member
Licensed User
Longtime User
See AsyncStreams tutorial. You can only use prefix mode if both sides of the connection implement the prefix mode.
thanks for your help ,but my question still ........i read https://www.b4x.com/android/forum/threads/asyncstreams-tutorial.7669/page-6,i don't find some infomation for me.prefix mode is not best way to my project .because ATM keyboard is not my product.
now.another APK can be send and recived data.so,this hardware is OK.
this my code error.this is true.
so,pls check my code....
thanks.........
 
Upvote 0

ship

Member
Licensed User
Longtime User
See AsyncStreams tutorial. You can only use prefix mode if both sides of the connection implement the prefix mode.
HI.EREL.
use AStream.Initialize not AStream.Initializeprefix,it's question Key point.....I am a stupid person........internet sample code. There is no feature to emphasize this...thanks a lot.sir.
 
Upvote 0
Top