Skip to content

Commit

Permalink
ensure that the BluetoothChatService is properly initialized and avai…
Browse files Browse the repository at this point in the history
…lable before attempting to use it in your FragmentViewModel
  • Loading branch information
Okuro3499 committed Jun 20, 2024
1 parent 70cc170 commit 894ed22
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ open class FragmentViewModel(application: Application) : AndroidViewModel(applic
/**
* To access bluetooth service for derived View Models
*/
protected lateinit var mChatService : BluetoothChatService
protected var mChatService: BluetoothChatService? = null
var lastCommand = ""
/**
* Monitors connection Status (has the specific connection state)
Expand Down Expand Up @@ -103,31 +103,34 @@ open class FragmentViewModel(application: Application) : AndroidViewModel(applic
if (_connectionStatus.value != Constants.STATE_CONNECTED) {
Toast.makeText(getApplication(), "Not Connected to Bluetooth", Toast.LENGTH_LONG).show()
}
else mChatService.write(toSend?.toByteArray())
else mChatService?.write(toSend?.toByteArray())
}

/**
* Load the bluetooth service and update the handler and connection status
*/
fun loadBT() {
mChatService = getApplication<MainApplication>().getCurrentBluetoothService()!!
mChatService.updateHandler(mHandler)
_connectionStatus.value = mChatService.state
val chatService = getApplication<MainApplication>().getCurrentBluetoothService()
if (chatService != null) {
mChatService = chatService
mChatService?.updateHandler(mHandler)
_connectionStatus.value = mChatService?.state
}
}

/**
* Update the current handler back to the scope of this ViewModel
*/
fun refreshHandler() {
mChatService.updateHandler(mHandler)
_connectionStatus.value = mChatService.state
mChatService?.updateHandler(mHandler)
_connectionStatus.value = mChatService?.state
}

/**
* Disconnects from the current connected device (NOTE: this does not stop the Android Service)
*/
fun disconnectBT() {
mChatService.stop()
mChatService?.stop()
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class HomeViewModel(application: Application) : FragmentViewModel(application) {
*/
fun connect(device: BluetoothDevice) {
this.device = device
mChatService.connect(device, true)
mChatService?.connect(device, true)
}

/**
Expand Down Expand Up @@ -102,7 +102,7 @@ class HomeViewModel(application: Application) : FragmentViewModel(application) {
}
s == RESULTS.VERSION && checkVersionSent -> checkVersion(output)
s == RESULTS.REMOTE_CHECK -> {
sendLog(mChatService.connectedDeviceName, output.trim().split(" "))
mChatService?.connectedDeviceName?.let { sendLog(it, output.trim().split(" ")) }
sendMessage(getString(R.string.TREEHOUSES_UPGRADE_CHECK))
}
else -> moreActions(output, s)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class NetworkViewModel(application: Application) : BaseNetworkViewModel(applicat
try {
sendMessage(getString(R.string.REBOOT))
Thread.sleep(1000)
if (mChatService.state != Constants.STATE_CONNECTED) {
if (mChatService?.state != Constants.STATE_CONNECTED) {
Toast.makeText(context, "Bluetooth Disconnected: Reboot in progress", Toast.LENGTH_LONG).show()
showHome.value = true
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class StatusViewModel(application: Application) : FragmentViewModel(application)
fun onLoad() {
loadBT()
fetchWifiCountry()
deviceName.value = mChatService.connectedDeviceName
deviceName.value = mChatService?.connectedDeviceName
countryDisplayTextEnabled.value = false
showUpgrade.value = false
refresh()
Expand Down

0 comments on commit 894ed22

Please sign in to comment.