bluetooth done

This commit is contained in:
skysamara 2022-08-30 00:02:15 +03:00
parent f947d619f7
commit 41b849c86a
6 changed files with 43 additions and 16 deletions

1
.idea/.name generated Normal file
View File

@ -0,0 +1 @@
airshot

2
.idea/gradle.xml generated
View File

@ -1,11 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="GradleMigrationSettings" migrationVersion="1" />
<component name="GradleSettings"> <component name="GradleSettings">
<option name="linkedExternalProjectsSettings"> <option name="linkedExternalProjectsSettings">
<GradleProjectSettings> <GradleProjectSettings>
<option name="testRunner" value="GRADLE" /> <option name="testRunner" value="GRADLE" />
<option name="distributionType" value="DEFAULT_WRAPPED" /> <option name="distributionType" value="DEFAULT_WRAPPED" />
<option name="externalProjectPath" value="$PROJECT_DIR$" /> <option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="gradleJvm" value="Embedded JDK" />
<option name="modules"> <option name="modules">
<set> <set>
<option value="$PROJECT_DIR$" /> <option value="$PROJECT_DIR$" />

4
.idea/misc.xml generated
View File

@ -3,11 +3,13 @@
<component name="DesignSurface"> <component name="DesignSurface">
<option name="filePathToZoomLevelMap"> <option name="filePathToZoomLevelMap">
<map> <map>
<entry key="..\:/_my/_Project/airshot_a/app/src/main/res/drawable-anydpi/ic_bluetooth_off.xml" value="0.143" />
<entry key="..\:/_my/_Project/airshot_a/app/src/main/res/drawable-anydpi/ic_bluetooth_on.xml" value="0.1" />
<entry key="..\:/_my/_project/android/airshot/app/src/main/res/layout/activity_main.xml" value="0.75" /> <entry key="..\:/_my/_project/android/airshot/app/src/main/res/layout/activity_main.xml" value="0.75" />
</map> </map>
</option> </option>
</component> </component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="Android Studio default JDK" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_11" project-jdk-name="11" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" /> <output url="file://$PROJECT_DIR$/build/classes" />
</component> </component>
<component name="ProjectType"> <component name="ProjectType">

View File

@ -15,6 +15,7 @@ import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() { class MainActivity : AppCompatActivity() {
private val REQUEST_CODE_ENABLE_BT:Int = 1 private val REQUEST_CODE_ENABLE_BT:Int = 1
private val REQUEST_CODE_DISCOVERBLE_BT:Int = 2
// bluetooth adapter // bluetooth adapter
lateinit var bAdapter:BluetoothAdapter lateinit var bAdapter:BluetoothAdapter
@ -42,33 +43,54 @@ class MainActivity : AppCompatActivity() {
} }
// Включаем bluetooth // Включаем bluetooth
// TODO Bluetooth включается, но сообщение и иконка неправильные
turnOnBtn.setOnClickListener { turnOnBtn.setOnClickListener {
if (bAdapter.isEnabled){ if (bAdapter.isEnabled){
// Уже включен // Уже включен
Toast.makeText(this, "Already on", Toast.LENGTH_LONG).show() Toast.makeText(this, "Already on", Toast.LENGTH_SHORT).show()
} }
else{ else{
// Включаем // Включаем
var intent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE) val intent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
startActivityForResult(intent, REQUEST_CODE_ENABLE_BT) startActivityForResult(intent, REQUEST_CODE_ENABLE_BT)
} }
} }
// Выключаем bluetooth // Выключаем bluetooth
turnOffBtn.setOnClickListener { turnOffBtn.setOnClickListener {
if (!bAdapter.isEnabled){
Toast.makeText(this, "Already off", Toast.LENGTH_SHORT).show()
}
else{
bAdapter.disable()
bluetoothIv.setImageResource(R.drawable.ic_bluetooth_off)
Toast.makeText(this, "Bluetooth turned off", Toast.LENGTH_SHORT).show()
}
} }
// Ищем bluetooth // Ищем bluetooth
discoverableBtn.setOnClickListener { discoverableBtn.setOnClickListener {
if (!bAdapter.isDiscovering){
Toast.makeText(this, "Making Your devices discoverable", Toast.LENGTH_SHORT).show()
val intent = Intent(Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE))
startActivityForResult(intent, REQUEST_CODE_DISCOVERBLE_BT)
}
}
// Список сопряженных устроств
pairedBtn.setOnClickListener{
if (bAdapter.isEnabled){
pairedTv.text = "Paired devices"
// получаем список сопряженных устройств
val devices = bAdapter.bondedDevices
for(device in devices){
val deviceName = device.name
val deviceAddress = device
pairedTv.append("\nDevice: $deviceName , $device")
}
}
else{
Toast.makeText(this, "Turn on bluetooth first", Toast.LENGTH_SHORT).show()
}
} }
// ****************************************** startShooting()
} }
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
@ -76,10 +98,10 @@ class MainActivity : AppCompatActivity() {
REQUEST_CODE_ENABLE_BT -> REQUEST_CODE_ENABLE_BT ->
if (requestCode == Activity.RESULT_OK){ if (requestCode == Activity.RESULT_OK){
bluetoothIv.setImageResource(R.drawable.ic_bluetooth_on) // 14:14 bluetoothIv.setImageResource(R.drawable.ic_bluetooth_on) // 14:14
Toast.makeText(this, "Bluetooth is on", Toast.LENGTH_LONG).show() Toast.makeText(this, "Bluetooth is on", Toast.LENGTH_SHORT).show()
} }
else{ else{
Toast.makeText(this, "Could not on bluetooth", Toast.LENGTH_LONG).show() Toast.makeText(this, "Could not on bluetooth", Toast.LENGTH_SHORT).show()
} }
} }

View File

@ -3,7 +3,7 @@
android:height="24dp" android:height="24dp"
android:viewportWidth="24" android:viewportWidth="24"
android:viewportHeight="24" android:viewportHeight="24"
android:tint="#0A3F52" android:tint="#FF00"
android:alpha="0.8"> android:alpha="0.8">
<path <path
android:fillColor="@android:color/white" android:fillColor="@android:color/white"

View File

@ -3,7 +3,7 @@
android:height="24dp" android:height="24dp"
android:viewportWidth="24" android:viewportWidth="24"
android:viewportHeight="24" android:viewportHeight="24"
android:tint="#0A3F52" android:tint="#00FF00"
android:alpha="0.8"> android:alpha="0.8">
<path <path
android:fillColor="@android:color/white" android:fillColor="@android:color/white"