目的是使用蓝牙模块
1 准备工作
1.1 在app->src->main-> AndroidManifest.xml 的package下增加
<!-- permission add bluetooth-->
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<!-- permission add bluetooth-->
1.2app->build.graddle 的Android 里增加
视图绑定功能可按模块启用。要在某个模块中启用视图绑定,请将 viewBinding
元素添加到其 build.gradle
文件中,
viewBinding {
enabled = true
}
在 Activity 中使用视图绑定
如需设置绑定类的实例以供 Activity 使用,请在 Activity 的 onCreate()
方法中执行以下步骤:
- 调用生成的绑定类中包含的静态
inflate()
方法。此操作会创建该绑定类的实例以供 Activity 使用。 - 通过调用
getRoot()
方法或使用 Kotlin 属性语法获取对根视图的引用。 - 将根视图传递到
setContentView()
,使其成为屏幕上的活动视图。
private lateinit var binding: ResultProfileBinding // add
override fun onCreate(savedInstanceState: Bundle) {
super.onCreate(savedInstanceState)
binding = ResultProfileBinding.inflate(layoutInflater)// add
val view = binding.root //
setContentView(view) //
}
那么引用视图的方法
binding.name.text = viewModel.name
binding.button.setOnClickListener { viewModel.userClicked() }
详细的内容,可参考官网材料。
还可我参考前面的例子 对color.xml 和 theme.xml 增加颜色和主题内容。这里不细说。
1.3 Resource Manager -Drawable 里增加蓝牙图标
clipArts 搜索 blue 即可找到
2 app->main->src->layout->activity_main.xml 布局文件
增加 txtview 显示蓝牙状态,增加蓝牙操控按键。蓝牙图标可从上面的图标动态调整。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
tools:context=".MainActivity">
<!--text view :display whether the bluetooth is availble or not -->
<!-- image to show the icon of bluetooth -->
<!-- Turn on bluetooth -->
<TextView
android:id="@+id/bluetoothStatusTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text=""
android:hint="I am here"
android:textAlignment="center"
android:textColor="@color/blue_200"
android:textSize="20sp" />
<ImageView
android:id="@+id/bluetoothIv"
android:layout_width="100dp"
android:layout_height="100dp"
android:contentDescription="@string/bluetoothiv"
android:src="@drawable/ic_bluetooth_off" />
<Button
android:id="@+id/turnonBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/TextAppearance.AppCompat.Widget.Button.Colored"
android:text="@string/turn_on"
android:width="280dp"/>
<!-- Turn off bluetooth -->
<Button
android:id="@+id/turnOffBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/TextAppearance.AppCompat.Widget.Button.Colored"
android:text="@string/turn_off"
android:width="280dp"/>
<!-- Discovered bluetooth -->
<Button
android:id="@+id/discoveredBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/TextAppearance.AppCompat.Widget.Button.Colored"
android:text="@string/discovered"
android:width="280dp"/>
<!-- Get list of bluetooth -->
<Button
android:id="@+id/pairedBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/TextAppearance.AppCompat.Widget.Button.Colored"
android:text="@string/get_paired_device"
android:width="280dp"/>
<!-- Pared bluetooth -->
<TextView
android:id="@+id/pairedTv"
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="show paired bluetooth"
android:textColor="@color/blue_200"/>
<TextView
android:id="@+id/showActionTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:hint="show action"
android:textColor="@color/design_default_color_on_primary"/>
</LinearLayout>
3 mainActivity.kt Code
这里主要是进行交互操作
package com.example.bluetooth
import android.app.Activity
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothAdapter.*
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.renderscript.ScriptGroup
import android.widget.Toast
import com.example.bluetooth.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private val REQUEST_CODE_ENABLE_BT:Int=1
private val REQUEST_CODE_DISCOVERED_BT:Int=1
// blutetooth adapter
lateinit var bAdapter:BluetoothAdapter
lateinit var binding: ActivityMainBinding // lateinit can use variable with no init value
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
binding = ActivityMainBinding.inflate(layoutInflater) //初始化 binding 对象,您将使用该对象访问 activity_main.xml 布局中的 Views
setContentView(binding.root)//设置 activity 的内容视图,指定应用中视图层次结构的根 binding.root
// init bluetooth Adapter
bAdapter= BluetoothAdapter.getDefaultAdapter()
// check if the bluetooth is on
if (bAdapter.isEnabled) binding.bluetoothStatusTv.text="Bluetooth is available"
else binding.bluetoothStatusTv.text="Bluetooth is not available"
// set image according to bluetooth status
// if (bAdapter.isEnabled) binding.bluetoothIv.setImageResource(R.drawable.ic_bluetooth_on)
// else binding.bluetoothIv.setImageResource(R.drawable.ic_bluetooth_off)
// binding.showActionTv.text="I will try"
// turn on blue tooth
binding.turnonBtn.setOnClickListener {
if(bAdapter.isEnabled){
Toast.makeText(this, "Already on", Toast.LENGTH_SHORT).show()
binding.bluetoothIv.setImageResource(R.drawable.ic_bluetooth_on)
}else{
// Turn on bluetooth
var intent=Intent(ACTION_REQUEST_ENABLE)
startActivityForResult(intent,REQUEST_CODE_ENABLE_BT)
}
binding.showActionTv.text="Turn on button down"
}
// turn off bluetooth
binding.turnOffBtn.setOnClickListener {
if(!bAdapter.isEnabled){
Toast.makeText(this, "Already off", Toast.LENGTH_SHORT).show()
}else{
// Turn off
bAdapter.disable()
binding.bluetoothIv.setImageResource(R.drawable.ic_bluetooth_off)
Toast.makeText(this, "Bluetooth turn off", Toast.LENGTH_SHORT).show()
}
binding.bluetoothIv.setImageResource(R.drawable.ic_bluetooth_off)
binding.showActionTv.text="Turn off button down"
}
//discovered bluetooth
binding.discoveredBtn.setOnClickListener {
if (!bAdapter.isDiscovering){
Toast.makeText(this, "Making your Bluetooth discovered ", Toast.LENGTH_SHORT).show()
val intent=Intent(Intent(ACTION_REQUEST_DISCOVERABLE))
startActivityForResult(intent,REQUEST_CODE_DISCOVERED_BT)
}
binding.showActionTv.text="Discover button down"
}
// get paired
binding.pairedBtn.setOnClickListener {
if(bAdapter.isEnabled){
binding.pairedTv.text="paired Device"
val devices=bAdapter.bondedDevices
for(device in devices){
val devicename=device.name
val deviceAddress=device
binding.pairedTv.append("\n Device: $devicename,$device")
}
}else
Toast.makeText(this, "Turn on your bluetooth first ", Toast.LENGTH_SHORT).show()
binding.showActionTv.text="Paired button down"
}
fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
when(requestCode){
REQUEST_CODE_ENABLE_BT ->
if (requestCode==Activity.RESULT_OK) {
binding.bluetoothIv.setImageResource(R.drawable.ic_bluetooth_on)
Toast.makeText(this, "Bluetooth is on", Toast.LENGTH_SHORT).show()
}else Toast.makeText(this, "Could not open Bluetooth", Toast.LENGTH_SHORT).show()
}
super.onActivityResult(requestCode, resultCode, data)
}
}
}
4 Result
后面步骤 就是 蓝牙通信获得数据。
安装到我的老手机,效果如下。