[/
Sub Class_Globals
'Bluetooth
Public Serial As Serial
Public Admin As BluetoothAdmin
Public AStream As AsyncStreams
Public FoundDevices As List
Type NameAndMac (Name As String, Mac As String)
Dim nm As NameAndMac
Public ReadData As String
' Private rp As RuntimePermissions
Public BluetoothState, ConnectionState As Boolean
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
Try
Admin.Initialize("admin")
Serial.Initialize("serial")
' If AStream.IsInitialized Then AStream.Close
Admin.Enable
If Admin.IsEnabled = False Then
If Admin.Enable = False Then
ToastMessageShow("Error Enabling Bluetooth...", True)
End If
Else
BluetoothState = True
End If
Catch
Log(LastException)
End Try
End Sub
Private Sub Admin_StateChanged (NewState As Int, OldState As Int)
Log("state changed: " & NewState)
BluetoothState = NewState = Admin.STATE_ON
NotifyOfStateChanged
End Sub
Private Sub NotifyOfStateChanged
' For Each Target In Array(Main, ChatActivity)
' CallSub(Target, "UpdateState")
' Next
End Sub
Public Sub Connect As Boolean
Dim PairedDevices As Map
Dim A, A1 As String
PairedDevices = Serial.GetPairedDevices
Dim l As List
l.Initialize
For i = 0 To PairedDevices.Size - 1
l.Add(PairedDevices.GetKeyAt(i))
Next
Dim Res As Int
Res = InputList(l, "Select Instrument", -1) 'show list with paired devices
If Res <> DialogResponse.CANCEL Then
Serial.Connect(PairedDevices.Get(l.Get(Res))) 'convert the name to mac address
A = PairedDevices.Get(l.Get(Res))
A1 = PairedDevices.GetKeyAt(Res)
Instruments(A, A1)
Return True
Else
Return False
End If
End Sub
Private Sub Serial_Connected (Success As Boolean)
Log("connected: " & Success)
CallSub2(Main, "AfterConnect", Success) 'allow the activity to hide the progress dialog
ConnectionState = Success
If Success = False Then
Log(LastException.Message)
ToastMessageShow("Error connecting: " & LastException.Message, True)
ProgressDialogHide
Else
If AStream.IsInitialized Then AStream.Close
'prefix mode! Change to non-prefix mode if communicating with non-B4X device.
AStream.Initialize(Serial.InputStream, Serial.OutputStream, "astream")
' StartActivity(ChatActivity)
End If
NotifyOfStateChanged
End Sub
Sub Instruments(A As String, A1 As String)
If CGlobals.li = 0 Then
CGlobals.DF(24)=A1
CGlobals.DF(25)=1
CGlobals.DF(26)=A
Msgbox2Async("Your Level Has Been Connected To " & A1, "Connected", "OK", "", "", Null,False)
Else If CGlobals.Li=1 Then
'Total Station
CGlobals.DF(5)=A1
CGlobals.DF(6)=1
CGlobals.DF(7)=A
Msgbox2Async("Your TS Has Been Connected To " & A1, "Connected", "OK", "", "", Null,False)
Else If CGlobals.Li=2 Then
'GPS
If CGlobals.BaseChecked=1 Then
CGlobals.DF(33)=A1
CGlobals.DF(34)=1
CGlobals.DF(35)=A
Msgbox2Async("Your GPS Base Has Been Connected To " & A1, "Connected", "OK", "", "", Null,False)
Else If CGlobals.RoverChecked=1 Then
CGlobals.DF(52)=A1
CGlobals.DF(53)=1
CGlobals.DF(54)=A
Msgbox2Async("Your GPS Rover Has Been Connected To " & A1, "Connected", "OK", "", "", Null,False)
End If
End If
End Sub
Public Sub ConnectTo (Device As NameAndMac)
Try
Serial.Connect(Device.Mac)
Catch
Log(LastException)
End Try
End Sub
Public Sub Disconnect
If AStream.IsInitialized Then AStream.Close
Serial.Disconnect
End Sub
Public Sub Disable
Admin.Disable
End Sub
'Public Sub Enable
' Admin.Enable
'End Sub
Public Sub SearchForDevices As Boolean
Return Admin.StartDiscovery
End Sub
Public Sub Admin_DiscoveryFinished
CallSub(Parameter, "DiscoverFinished")
End Sub
Public Sub Admin_DeviceFound (Name As String, MacAddress As String)
If Name<>"" And MacAddress<>"" Then
Dim nm As NameAndMac
nm.Name = Name
nm.Mac = MacAddress
FoundDevices.Add(nm)
End If
End Sub
Public Sub AfterSuccessfulConnection
If AStream.IsInitialized Then AStream.Close
'prefix mode! Change to non-prefix mode if communicating with non-B4X device.
' AStream.Initialize(Serial.InputStream, Serial.OutputStream, "AStream")
End Sub
Public Sub StopReading(A As String)
Serial.StopListening
End Sub
Public Sub ReadDeviceDataAsync (Buffer() As Byte)
ReadData=BytesToString(Buffer, 0, Buffer.Length, "UTF8")
End Sub
Public Sub SendDataToDevice(A As String)
AStream.Write(A.GetBytes("utf8"))
End Sub
Public Sub CloseConnection
End Sub
Public Sub ClearOutputData
End Sub
Public Sub ClearBuffer
End Sub
Public Sub Pair
Serial.Listen
' Dim StartingIntent As String
'
' If StartingIntent.Action = "ACTION_PAIRING_REQUEST" Then
' Dim jo As JavaObject = StartingIntent
' Dim device As JavaObject = jo.RunMethod("getParcelableExtra", Array("android.bluetooth.device.extra.DEVICE"))
' device.RunMethod("setPairingConfirmation", Array(True))
' End If
End Sub
]