因为课堂需要需要研究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
<<: Day 22 HTML5 <HTML5 input类型、表单标签>
延续昨天的内容,今天我们要完成写入&读取的功能 将信件内容写入到 Google Sheet ...
「喂?」山姆拿起对讲机,试着联络村子里的长老。 「我在这座黑森林里,找到很多水晶,这样不知道可以卖...
Amazon Relational Database Service (RDS) 是AWS中为「结构...
micro:bit 系列完赛罗~ 心得和感想在 Scratch 的主题最後一篇「Day46 Scra...
其实大家可能都有自己的API架构方式,不过我这边就是分享我目前在Web端撰写API时,在架构上和开发...