My b4a program connects to a device that has a hardware and that hardware has bluetooth device. It turns appliances on/off via click of a button through bluetooth or sms, whichever the user selects. It's ok with bluetooth. I just want to make it error proof. What I did is my b4a program connected successfully to the hardware that has bluetooth device. After that, then I turned the hardware off. My variable Var.BT_Service_connected must then become false after I turned off the hardware but how come it is still evaluated as true in my code even though I turned off the hardware to which my bluetooth program is connected. What happened is that it still execute:
tmrSend.Enabled =True
ProgressDialogShow("Processing Request...")
Since the hardware is turned off, it makes my program crash and stop running. And how come the Try Catch failed to catch the error and allowed my program to crashed?
These are the code fragments:
If Var.sendMode =1 AND Var.BT_Service_connected = True Then
Try
tmrSend.Enabled =True
ProgressDialogShow("Processing Request...")
Catch
Msgbox("Error sending Bluetooth message!", "Error Sendindg")
ReverseButton
End Try
End If
This is my svc_bt_connected:
Sub Process_Globals
End Sub
Sub Service_Create
'Var.BT_Service_connected = False
End Sub
Sub Service_Start (StartingIntent As Intent)
If StartingIntent.Action = "android.bluetooth.device.action.ACL_CONNECTED" Then
Log("BT_Connect")
Var.BT_Service_connected = True
End If
Log(StartingIntent)
Log(StartingIntent.ExtrasToString)
End Sub
Sub Service_Destroy
End Sub
This is my svc_bt_disconnect:
Sub Process_Globals
End Sub
Sub Service_Create
End Sub
Sub Service_Start (StartingIntent As Intent)
If StartingIntent.Action = "android.bluetooth.device.action.ACL_DISCONNECTED" Then
Log("BT_Disconnect")
Var.BT_Service_connected = False
End If
Log(StartingIntent)
Log(StartingIntent.ExtrasToString)
End Sub
Sub Service_Destroy
End Sub
This is my Manifest:
AddManifestText(
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="14"/>
<supports-screens android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true"/>)
AddReceiverText(svc_bt_disconnect, <intent-filter> <action android:name="android.bluetooth.device.action.ACL_DISCONNECTED"/></intent-filter>)
AddReceiverText(svc_bt_connected, <intent-filter> <action android:name="android.bluetooth.device.action.ACL_CONNECTED"/></intent-filter>)
AddPermission(android.permission.BLUETOOTH)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
And this is my BTService:
Sub Process_Globals
Dim AStream As AsyncStreams
Dim MsgToSend As String
Dim MsgReceived As String
End Sub
Sub Service_Create
End Sub
Sub Service_Start (StartingIntent As Intent)
If AStream.IsInitialized Then AStream.Close
AStream.Initialize(Control.serial1.InputStream, Control.serial1.OutputStream, "AStream")
Log("Astream Initialized")
End Sub
Sub Service_Destroy
AStream.Close
End Sub
Sub SendMsgBluetooth
Log(MsgToSend)
If Var.BT_Service_connected = True Then
AStream.Write(MsgToSend.GetBytes("UTF8"))
End If
End Sub
Sub AStream_NewData(Buffer() As Byte)
Var.ErrorSend = False
Var.nagSuccess = True
Try
MsgReceived = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
If IsPaused(Control) = False Then
CallSub(Control, "NewMsgFrom")
End If
Catch
Msgbox("Error sending Bluetooth message!", "Error Sendindg")
End Try
End Sub
Sub AStream_Error
ToastMessageShow("Bluetooth Disconnected", True)
AStream.Close
AStream_Terminated
End Sub
Sub AStream_Terminated
Var.ErrorSend = True
Var.nagSuccess = False
'ToastMessageShow("Bluetooth Disconnected", True)
ProgressDialogHide
Msgbox("Error sending Bluetooth message!", "Error Sendindg")
CallSub(Control,"ReverseButton")
End Sub
tmrSend.Enabled =True
ProgressDialogShow("Processing Request...")
Since the hardware is turned off, it makes my program crash and stop running. And how come the Try Catch failed to catch the error and allowed my program to crashed?
These are the code fragments:
If Var.sendMode =1 AND Var.BT_Service_connected = True Then
Try
tmrSend.Enabled =True
ProgressDialogShow("Processing Request...")
Catch
Msgbox("Error sending Bluetooth message!", "Error Sendindg")
ReverseButton
End Try
End If
This is my svc_bt_connected:
Sub Process_Globals
End Sub
Sub Service_Create
'Var.BT_Service_connected = False
End Sub
Sub Service_Start (StartingIntent As Intent)
If StartingIntent.Action = "android.bluetooth.device.action.ACL_CONNECTED" Then
Log("BT_Connect")
Var.BT_Service_connected = True
End If
Log(StartingIntent)
Log(StartingIntent.ExtrasToString)
End Sub
Sub Service_Destroy
End Sub
This is my svc_bt_disconnect:
Sub Process_Globals
End Sub
Sub Service_Create
End Sub
Sub Service_Start (StartingIntent As Intent)
If StartingIntent.Action = "android.bluetooth.device.action.ACL_DISCONNECTED" Then
Log("BT_Disconnect")
Var.BT_Service_connected = False
End If
Log(StartingIntent)
Log(StartingIntent.ExtrasToString)
End Sub
Sub Service_Destroy
End Sub
This is my Manifest:
AddManifestText(
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="14"/>
<supports-screens android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true"/>)
AddReceiverText(svc_bt_disconnect, <intent-filter> <action android:name="android.bluetooth.device.action.ACL_DISCONNECTED"/></intent-filter>)
AddReceiverText(svc_bt_connected, <intent-filter> <action android:name="android.bluetooth.device.action.ACL_CONNECTED"/></intent-filter>)
AddPermission(android.permission.BLUETOOTH)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
And this is my BTService:
Sub Process_Globals
Dim AStream As AsyncStreams
Dim MsgToSend As String
Dim MsgReceived As String
End Sub
Sub Service_Create
End Sub
Sub Service_Start (StartingIntent As Intent)
If AStream.IsInitialized Then AStream.Close
AStream.Initialize(Control.serial1.InputStream, Control.serial1.OutputStream, "AStream")
Log("Astream Initialized")
End Sub
Sub Service_Destroy
AStream.Close
End Sub
Sub SendMsgBluetooth
Log(MsgToSend)
If Var.BT_Service_connected = True Then
AStream.Write(MsgToSend.GetBytes("UTF8"))
End If
End Sub
Sub AStream_NewData(Buffer() As Byte)
Var.ErrorSend = False
Var.nagSuccess = True
Try
MsgReceived = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
If IsPaused(Control) = False Then
CallSub(Control, "NewMsgFrom")
End If
Catch
Msgbox("Error sending Bluetooth message!", "Error Sendindg")
End Try
End Sub
Sub AStream_Error
ToastMessageShow("Bluetooth Disconnected", True)
AStream.Close
AStream_Terminated
End Sub
Sub AStream_Terminated
Var.ErrorSend = True
Var.nagSuccess = False
'ToastMessageShow("Bluetooth Disconnected", True)
ProgressDialogHide
Msgbox("Error sending Bluetooth message!", "Error Sendindg")
CallSub(Control,"ReverseButton")
End Sub
Last edited: