Skip to content

Commit

Permalink
v1.5 搜索附近蓝牙设备列表
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleBing committed Feb 24, 2024
1 parent 3ef0357 commit 2074709
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 24 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ android {
applicationId = "cn.kylebing.blackberry.q10_keyboard"
minSdk = 18
targetSdk = 34
versionCode = 18
versionCode = 19
versionName = "1.5"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
Expand Down
10 changes: 6 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
tools:ignore="CoarseFineLocation">

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> <!--蓝牙管理-->
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" /> <!--蓝牙连接-->
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" /> <!--蓝牙扫描-->

<!-- If your app targets Android 9 or lower, you can declare
ACCESS_COARSE_LOCATION instead. -->

<!-- If your app targets Android 9 or lower, you can declare ACCESS_COARSE_LOCATION instead. -->
<!-- 需要这个权限,不然 app 安装失败 -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"
tools:ignore="CoarseFineLocation" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ class MainActivity : AppCompatActivity() {


/**
* 查找附近蓝牙设备
* 开始发现设备
*/

// Create a BroadcastReceiver for ACTION_FOUND.
// 创建一个广播接收器: ACTION_FOUND
private val receiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val textViewOther: TextView = findViewById<TextView>(R.id.textViewOther)
val textViewInfo: TextView = findViewById<TextView>(R.id.text_view_info)
when (intent.action) {
BluetoothDevice.ACTION_FOUND -> {
// Discovery has found a device. Get the BluetoothDevice
Expand All @@ -88,20 +88,30 @@ class MainActivity : AppCompatActivity() {
intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE)
val deviceName = device?.name
val deviceHardwareAddress = device?.address // MAC address
textViewOther.text = "${textViewOther.text}\n新设备名: ${deviceName}, 地址: ${deviceHardwareAddress}"
textViewInfo.text = "${textViewInfo.text}\n新设备名: ${deviceName}, 地址: ${deviceHardwareAddress}"
}
}
}
}

private fun receiveBTDeviceConnection(){
// Register for broadcasts when a device is discovered.
// 发现设备后创建一个接收器
val filter = IntentFilter(BluetoothDevice.ACTION_FOUND)
registerReceiver(receiver, filter)
Toast
.makeText(applicationContext, "已开启接收附近蓝牙设备的连接", Toast.LENGTH_SHORT)
.show()
// blue tooth
val mBluetoothManager = getSystemService(BLUETOOTH_SERVICE) as BluetoothManager
val mBtAdapter = mBluetoothManager.adapter
if (mBtAdapter != null) {

// 发现设备后创建一个接收器
val filter = IntentFilter(BluetoothDevice.ACTION_FOUND)
registerReceiver(receiver, filter)

// 开始扫描
if (mBtAdapter.startDiscovery()){
Toast
.makeText(applicationContext, "开始发现设备", Toast.LENGTH_SHORT)
.show()
}
}

}


Expand All @@ -116,6 +126,7 @@ class MainActivity : AppCompatActivity() {
putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 100)
}
startActivityForResult(discoverableIntent, requestCode)

Toast
.makeText(applicationContext, "已开启可被发现", Toast.LENGTH_SHORT)
.show()
Expand All @@ -127,31 +138,31 @@ class MainActivity : AppCompatActivity() {
private fun openBluetooth() {
// blue tooth
val mBluetoothManager = getSystemService(BLUETOOTH_SERVICE) as BluetoothManager
val textViewOther: TextView = findViewById<TextView>(R.id.textViewOther)
val textViewInfo: TextView = findViewById<TextView>(R.id.text_view_info)
val mBtAdapter = mBluetoothManager.adapter
if (mBtAdapter != null) {

if (!mBtAdapter.isEnabled) {
val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
val REQUEST_ENABLE_BT = 1 // 请求码,将会在结果中返回请求码
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT)
textViewOther.text = "蓝牙未开启,已请求开启"
textViewInfo.text = "蓝牙未开启,已请求开启"
object : CountDownTimer(3000,1000){
override fun onTick(p0: Long) {
textViewOther.text = "${p0/1000}s 后刷新蓝牙列表"
textViewInfo.text = "${p0/1000}s 后刷新蓝牙列表"
}
override fun onFinish() {
textViewOther.text = "正在刷新蓝牙列表..."
textViewInfo.text = "正在刷新蓝牙列表..."
refreshBlueToothInfo()
}
}.start()
} else {
textViewOther.text = "配对的蓝牙设备数量为 ${mBtAdapter.bondedDevices.size}"
textViewInfo.text = "配对的蓝牙设备数量为 ${mBtAdapter.bondedDevices.size}"
var deviceSetString = ""
mBtAdapter.bondedDevices.forEach { device ->
deviceSetString = deviceSetString + device.toString() + ",\n"
}
textViewOther.text =
textViewInfo.text =
"配对的蓝牙设备数量为:${mBtAdapter.bondedDevices.size}\n设备信息为:\n$deviceSetString"
}
}
Expand All @@ -167,15 +178,15 @@ class MainActivity : AppCompatActivity() {
private fun refreshBlueToothInfo() {
// blue tooth
val mBluetoothManager = getSystemService(BLUETOOTH_SERVICE) as BluetoothManager
val textViewOther: TextView = findViewById<TextView>(R.id.textViewOther)
val textViewInfo: TextView = findViewById<TextView>(R.id.text_view_info)
val mBtAdapter = mBluetoothManager.adapter
if (mBtAdapter != null) {
if (mBtAdapter.isEnabled) {
var deviceSetString = ""
mBtAdapter.bondedDevices.forEach { device ->
deviceSetString = deviceSetString + device.toString() + ",\n"
}
textViewOther.text =
textViewInfo.text =
"配对的蓝牙设备数量为:${mBtAdapter.bondedDevices.size}\n设备信息为:\n$deviceSetString"
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/blue_tooth_feathers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
android:padding="5dp"
android:layout_height="150dp">
<TextView
android:id="@+id/textViewOther"
android:id="@+id/text_view_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="---"
Expand Down

0 comments on commit 2074709

Please sign in to comment.