iT邦幫忙

2021 iThome 鐵人賽

DAY 23
0
自我挑戰組

Android kotlin &MVVM系列 第 23

Android學習筆記23

  • 分享至 

  • xImage
  •  

因為課堂需要需要研究android連線到arduino透過藍芽的方式,所以開始研究藍芽的使用方法
一開始先幫藍芽做連線確認

class MainActivity : AppCompatActivity() {
    companion object{
        fun EXTRA_ADDRESS() = "device_address"
    }
    private var myBlueTooth: BluetoothAdapter? = null
    private val pairedDevices: Set<*>? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding = DataBindingUtil.setContentView<ActivityMainBinding>(this,R.layout.activity_main)
        myBlueTooth = BluetoothAdapter.getDefaultAdapter()
        if (myBlueTooth == null){
            Toast.makeText(applicationContext, "Bluetooth Device Not Available", Toast.LENGTH_LONG).show()
        }
        else{
            if (myBlueTooth!!.isEnabled){

            }
            else{
                val intent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
                startActivityForResult(intent,1)
            }
        }
        binding.button.setOnClickListener { pairedDeviceList() }
    }

那這邊的companion object就是kotlin要宣告靜態變數的方式
接著是連線到藍芽的方式

private fun pairedDeviceList() {
        val binding = DataBindingUtil.setContentView<ActivityMainBinding>(this,R.layout.activity_main)
        val pairedDevices = myBlueTooth?.bondedDevices
        val list = ArrayList<Any>()

        if (pairedDevices!!.size > 0) {
            for (bt in pairedDevices) {
                list.add(bt.name + "\n" + bt.address) //Get the device's name and the address
            }
        } else {
            Toast.makeText(
                applicationContext,
                "No Paired Bluetooth Devices Found.",
                Toast.LENGTH_LONG
            ).show()
        }

        val adapter: ArrayAdapter<*> = ArrayAdapter(this, android.R.layout.simple_list_item_1, list)
        binding.listView.adapter = adapter
        val myListClickListener =
            OnItemClickListener { av, v, arg2, arg3 -> // Get the device MAC address, the last 17 chars in the View
                val info = (v as TextView).text.toString()
                val address = info.substring(info.length - 17)
                // Make an intent to start next activity.
                val i = Intent(this, LedControl::class.java)
                //Change the activity.
                i.putExtra(
                    EXTRA_ADDRESS(),
                    address
                ) //this will be received at ledControl (class) Activity
                startActivity(i)
            }
        binding.listView.onItemClickListener = myListClickListener //Method called when the device from the list is clicked
    }
}private fun pairedDeviceList() {
        val binding = DataBindingUtil.setContentView<ActivityMainBinding>(this,R.layout.activity_main)
        val pairedDevices = myBlueTooth?.bondedDevices
        val list = ArrayList<Any>()

        if (pairedDevices!!.size > 0) {
            for (bt in pairedDevices) {
                list.add(bt.name + "\n" + bt.address) //Get the device's name and the address
            }
        } else {
            Toast.makeText(
                applicationContext,
                "No Paired Bluetooth Devices Found.",
                Toast.LENGTH_LONG
            ).show()
        }

        val adapter: ArrayAdapter<*> = ArrayAdapter(this, android.R.layout.simple_list_item_1, list)
        binding.listView.adapter = adapter
        val myListClickListener =
            OnItemClickListener { av, v, arg2, arg3 -> // Get the device MAC address, the last 17 chars in the View
                val info = (v as TextView).text.toString()
                val address = info.substring(info.length - 17)
                // Make an intent to start next activity.
                val i = Intent(this, LedControl::class.java)
                //Change the activity.
                i.putExtra(
                    EXTRA_ADDRESS(),
                    address
                ) //this will be received at ledControl (class) Activity
                startActivity(i)
            }
        binding.listView.onItemClickListener = myListClickListener //Method called when the device from the list is clicked
    }
}

這邊我有用databinding,但是還沒連結到viewmodel等功能做齊全後會設法移到viewmodel


上一篇
Android學習筆記22
下一篇
Android學習筆記24
系列文
Android kotlin &MVVM30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言