I have recently followed a Tutorial on Youtube by Philip Lackner(https://www.youtube.com/watch?v=A41hkHoYu4M) and built a basic Bluetooth app.I am looking to further the app
I am currently looking into using Intent filters to transfer information in a dual pane layout. The Intents seem to work but Im not sure, but I have noticed a few things that I would like help with.
Firstly I have a dual pane layout, I would like to send a Bluetooth device name through the fragment via the intent, but when I pull across the intent to the other side it generates a null.
I have tried all sorts of null checks, alterations and improvisations (I have got some very unique results) but I still have not got the Bluetooth name Im after and Im really wondering what I’m doing wrong.
Secondly, I have put a Log Statement in the intent PutExtra statement, the log is never sent to logcat or it seems even looked at, I believe this could be the problem but not sure how to solve it
I am using Hilt and Dagger, could this be part of the problem?
Here is my Main Activity
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if (DEVICE_ID != null && !DEVICE_ID.isEmpty() && !DEVICE_ID.equals("null"))
intent.let {
// val devices = it.getStringExtra(DEVICES)
// findViewById<TextView>(R.id.tt_conf_label)
// .text = getString(R.string.tt_confirmation_label, devices)
val transfer = intent.extras?.getString(DEVICE_ID)
findViewById<TextView>(R.id.tt_conf_label)
.text = getString(R.string.tt_confirmation_label, transfer)
Log.d("test3 MA", "DEVICEID:"+ DEVICE_ID)
}
}
}
import android.annotation.SuppressLint
import android.content.ContentValues.TAG
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.core.content.ContextCompat.startActivity
import androidx.fragment.app.Fragment
import com.example.fragmentintro.StyleFragment
import com.example.fragmentintro.exchange.BluetoothDevice
import com.example.fragmentintro.presentation.BluetoothUiState
import com.example.fragmentintro.presentation.MainActivity
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import com.example.fragmentintro.R
const val DEVICE_ID = "DEVICE_ID"
@Composable
fun DeviceScreen(
state: BluetoothUiState,
onStartScan: () -> Unit,
onStopScan: () -> Unit
) {
Column(
modifier = Modifier
.fillMaxSize()
) {
BluetoothDeviceList(
pairedDevices = state.pairedDevices,
scannedDevices = state.scannedDevices,
onClick = {},
modifier = Modifier
.fillMaxWidth()
.weight(1f)
)
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceAround
) {
Button(onClick = onStartScan) {
Text(text = "Start scan")
}
Button(onClick = onStopScan) {
Text(text = "Stop scan")
}
}
}
}
@Composable
fun BluetoothDeviceList(
pairedDevices: List<BluetoothDevice>,
scannedDevices: List<BluetoothDevice>,
onClick: (BluetoothDevice) -> Unit,
modifier: Modifier = Modifier
) {
LazyColumn(
modifier = modifier
) {
item {
Text(
text = "Paired Devices",
fontWeight = FontWeight.Bold,
fontSize = 24.sp,
modifier = Modifier.padding(16.dp)
)
}
items(pairedDevices) { device ->
Text(
text = device.name ?: "(No name)",
modifier = Modifier
.fillMaxWidth()
.clickable { onClick(device) }
.padding(16.dp)
)
}
item {
Text(
text = "Scanned Devices",
fontWeight = FontWeight.Bold,
fontSize = 24.sp,
modifier = Modifier.padding(16.dp)
)
}
items(scannedDevices) { device ->
Text(
text = device.name ?: "(No name)",
modifier = Modifier
.fillMaxWidth()
.clickable { onClick(device) }
.padding(16.dp))
class DeviceActivity : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_style, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Intent(this, StyleFragment::class.java)
.also { styleFragment ->
styleFragment.putExtra(DEVICE_ID, device.name)
startActivity(styleFragment)
}
val conf = findViewById<Button>(R.id.tt_cont_button)
conf.setOnClickListener() {
Log.d("test3 DS", "DEVICEID:" + DEVICE_ID)
}
}
}
}
}
}
}
}
package com.example.fragmentintro.presentation.components
import android.annotation.SuppressLint
import android.content.ContentValues.TAG
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.core.content.ContextCompat.startActivity
import androidx.fragment.app.Fragment
import com.example.fragmentintro.StyleFragment
import com.example.fragmentintro.exchange.BluetoothDevice
import com.example.fragmentintro.presentation.BluetoothUiState
import com.example.fragmentintro.presentation.MainActivity
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import com.example.fragmentintro.R
const val DEVICE_ID = "DEVICE_ID"
@Composable
fun DeviceScreen(
state: BluetoothUiState,
onStartScan: () -> Unit,
onStopScan: () -> Unit
) {
Column(
modifier = Modifier
.fillMaxSize()
) {
BluetoothDeviceList(
pairedDevices = state.pairedDevices,
scannedDevices = state.scannedDevices,
onClick = {},
modifier = Modifier
.fillMaxWidth()
.weight(1f)
)
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceAround
) {
Button(onClick = onStartScan) {
Text(text = "Start scan")
}
Button(onClick = onStopScan) {
Text(text = "Stop scan")
}
}
}
}
@Composable
fun BluetoothDeviceList(
pairedDevices: List<BluetoothDevice>,
scannedDevices: List<BluetoothDevice>,
onClick: (BluetoothDevice) -> Unit,
modifier: Modifier = Modifier
) {
LazyColumn(
modifier = modifier
) {
item {
Text(
text = "Paired Devices",
fontWeight = FontWeight.Bold,
fontSize = 24.sp,
modifier = Modifier.padding(16.dp)
)
}
items(pairedDevices) { device ->
Text(
text = device.name ?: "(No name)",
modifier = Modifier
.fillMaxWidth()
.clickable { onClick(device) }
.padding(16.dp)
)
}
item {
Text(
text = "Scanned Devices",
fontWeight = FontWeight.Bold,
fontSize = 24.sp,
modifier = Modifier.padding(16.dp)
)
}
items(scannedDevices) { device ->
Text(
text = device.name ?: "(No name)",
modifier = Modifier
.fillMaxWidth()
.clickable { onClick(device) }
.padding(16.dp))
class DeviceActivity : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_style, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
class DeviceScreen: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Intent(this, MainActivity::class.java)
.also { styleFragment ->
styleFragment.putExtra(DEVICE_ID, device.name)
startActivity(styleFragment)
}
val conf = findViewById<Button>(R.id.tt_cont_button)
conf.setOnClickListener() {
Log.d("test3 DS", "DEVICEID:" + DEVICE_ID)
}
}
}
}
}
}
}
}
```