Skip to content

Commit

Permalink
v1.5 展示附近蓝牙设备列表:未完成
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleBing committed Feb 23, 2024
1 parent 2fab017 commit 3ef0357
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 39 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 = 7
versionCode = 18
versionName = "1.5"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package cn.kylebing.blackberry.q10_keyboard

import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothManager
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.PackageManager
import android.os.Bundle
import android.os.CountDownTimer
import android.view.View
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity


Expand All @@ -30,6 +34,10 @@ class MainActivity : AppCompatActivity() {

refreshBlueToothInfo() // 刷新蓝牙信息

/**
* 按钮事件
*/

// bluetooth on
findViewById<Button>(R.id.button_turn_on_bluetooth)
.setOnClickListener {
Expand All @@ -47,8 +55,57 @@ class MainActivity : AppCompatActivity() {
.setOnClickListener {
setBluetoothCanBeFound()
}

// bluetooth receive
findViewById<Button>(R.id.button_receive_connection)
.setOnClickListener {
receiveBTDeviceConnection()
}
}


override fun onDestroy() {
super.onDestroy()
// Don't forget to unregister the ACTION_FOUND receiver.
unregisterReceiver(receiver)
}


/**
* 查找附近蓝牙设备
*/

// 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)
when (intent.action) {
BluetoothDevice.ACTION_FOUND -> {
// Discovery has found a device. Get the BluetoothDevice
// object and its info from the Intent.
val device: BluetoothDevice? =
intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE)
val deviceName = device?.name
val deviceHardwareAddress = device?.address // MAC address
textViewOther.text = "${textViewOther.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()
}



/**
* 设置蓝牙可被发现
*/
Expand All @@ -59,6 +116,9 @@ class MainActivity : AppCompatActivity() {
putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 100)
}
startActivityForResult(discoverableIntent, requestCode)
Toast
.makeText(applicationContext, "已开启可被发现", Toast.LENGTH_SHORT)
.show()
}

/**
Expand Down Expand Up @@ -89,12 +149,15 @@ class MainActivity : AppCompatActivity() {
textViewOther.text = "配对的蓝牙设备数量为 ${mBtAdapter.bondedDevices.size}"
var deviceSetString = ""
mBtAdapter.bondedDevices.forEach { device ->
deviceSetString = deviceSetString + device.toString() + ","
deviceSetString = deviceSetString + device.toString() + ",\n"
}
textViewOther.text =
"配对的蓝牙设备数量为:${mBtAdapter.bondedDevices.size}\n 设备信息为:$deviceSetString"
"配对的蓝牙设备数量为:${mBtAdapter.bondedDevices.size}\n设备信息为:\n$deviceSetString"
}
}
Toast
.makeText(applicationContext, "已开启蓝牙", Toast.LENGTH_SHORT)
.show()
}

/**
Expand All @@ -116,5 +179,8 @@ class MainActivity : AppCompatActivity() {
"配对的蓝牙设备数量为:${mBtAdapter.bondedDevices.size}\n设备信息为:\n$deviceSetString"
}
}
Toast
.makeText(applicationContext, "已刷新", Toast.LENGTH_SHORT)
.show()
}
}
89 changes: 54 additions & 35 deletions app/src/main/res/layout/blue_tooth_feathers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,64 +37,83 @@
/>
</LinearLayout>

<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/textViewBlueTooth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:text="蓝牙权限"
android:textColor="@color/white"
android:textSize="18dp" />
<TextView
android:id="@+id/textViewBlueToothLE"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:text="低功耗蓝牙权限"
android:textColor="@color/white"
android:textSize="18dp" />
</LinearLayout>

<ScrollView
android:background="#111"
android:layout_marginTop="5dp"
android:layout_width="match_parent"
android:layout_height="200dp">
<LinearLayout
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:padding="5dp"
android:layout_height="150dp">
<TextView
android:id="@+id/textViewOther"
android:layout_width="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textViewBlueTooth"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:text="蓝牙权限"
android:textColor="@color/white"
android:textSize="18dp" />

<TextView
android:id="@+id/textViewBlueToothLE"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:text="低功耗蓝牙权限"
android:textColor="@color/white"
android:textSize="18dp" />

<TextView
android:id="@+id/textViewOther"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:text="---"
android:textColor="@color/white"
android:textSize="18dp" />
</LinearLayout>
android:layout_height="wrap_content"
android:text="---"
android:textColor="@color/white"
android:textSize="18dp" />
</ScrollView>


<LinearLayout
android:gravity="center"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_height="40dp"
>
<Button
android:id="@+id/button_turn_on_bluetooth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:text="开启蓝牙" />
<Button
android:id="@+id/button_refresh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="刷新蓝牙" />
android:layout_marginRight="5dp"
android:text="刷新" />

</LinearLayout>

<LinearLayout
android:gravity="center"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="40dp"
>
<Button
android:id="@+id/button_receive_connection"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:text="接收连接" />
<Button
android:id="@+id/button_be_found"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="可被发现" />
android:layout_marginRight="5dp"
android:text="可被别人发现" />

</LinearLayout>

Expand Down
2 changes: 2 additions & 0 deletions move_app_to_external_storage.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
mv /Users/kyle/Documents/Android/Q10Keyboard/app/release/app-release.apk /Volumes/BBQ10_64GB/apps && diskutil unmountDisk /dev/disk6

0 comments on commit 3ef0357

Please sign in to comment.