実現したいこと
Android smart phoneでbluetoothが接続できるかどうか確認できることをandroid studioで
作ったアプリがあるのですけど、プログラムは上手く走るのですがボタンを押してもtoastが表示されなかったり、エラーメッセージが全くないのでどこを改善したらいいのかわかりません。
どうやったら正常に動くか教えて頂きたいです。
参考にした動画
https://youtu.be/zkA35fcNLH0
該当のソースコード
MainActivity.java
1package com.wabsarva.wings.android.bluetoothyoutube; 2 3import androidx.annotation.Nullable; 4import androidx.appcompat.app.AppCompatActivity; 5import androidx.core.app.ActivityCompat; 6 7import android.bluetooth.BluetoothAdapter; 8import android.content.Intent; 9import android.content.pm.PackageManager; 10import android.os.Bundle; 11import android.view.View; 12import android.widget.Button; 13import android.widget.Toast; 14 15public class MainActivity extends AppCompatActivity { 16 17 Button buttonON, buttonOFF; 18 BluetoothAdapter myBluetoothAdapter; 19 20 Intent btEnablingIntent; 21 int requestCodeForEnable; 22 23 @Override 24 protected void onCreate(Bundle savedInstanceState) { 25 super.onCreate(savedInstanceState); 26 setContentView(R.layout.activity_main); 27 28 buttonON = (Button) findViewById(R.id.btON); 29 buttonOFF = (Button) findViewById(R.id.btOFF); 30 myBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 31 btEnablingIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 32 requestCodeForEnable = 1; 33 34 bluetoothONMethod(); 35 bluetoothOFFMethod(); 36 } 37 38 private void bluetoothOFFMethod() { 39 buttonOFF.setOnClickListener(new View.OnClickListener() { 40 @Override 41 public void onClick(View v) { 42 if (myBluetoothAdapter.isEnabled()) 43 { 44 myBluetoothAdapter.disable(); 45 } 46 } 47 }); 48 } 49 50 51 @Override 52 protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { 53 super.onActivityResult(requestCode, resultCode, data); 54 if (requestCode == requestCodeForEnable) 55 { 56 if (resultCode == RESULT_OK) 57 { 58 Toast.makeText(getApplicationContext(),"Bluetooth is Enable", Toast.LENGTH_LONG).show(); 59 } else if (resultCode == RESULT_CANCELED) 60 { 61 Toast.makeText(getApplicationContext(),"Bluetooth Enabling Cancelled", Toast.LENGTH_LONG).show(); 62 } 63 } 64 } 65 66 private void bluetoothONMethod() { 67 buttonON.setOnClickListener(new View.OnClickListener() { 68 69 @Override 70 public void onClick(View v) { 71 if (myBluetoothAdapter == null) { 72 //Device does not support Bluetooth 73 Toast.makeText(getApplicationContext(), "Bluetooth does not support on this Device", Toast.LENGTH_LONG).show(); 74 } else { 75 if (!myBluetoothAdapter.isEnabled()) { 76 //Code for Bluetooth Enable 77 startActivityForResult(btEnablingIntent, requestCodeForEnable); 78 } 79 } 80 } 81 }); 82 } 83} 84
AndroidManifest.xml
1 2<?xml version="1.0" encoding="utf-8"?> 3<manifest xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:tools="http://schemas.android.com/tools"> 5 <uses-permission android:name="android.permission.BLUETOOTH"/> 6 <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> 7 8 <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" /> 9 10 <application 11 android:allowBackup="true" 12 android:dataExtractionRules="@xml/data_extraction_rules" 13 android:fullBackupContent="@xml/backup_rules" 14 android:icon="@mipmap/ic_launcher" 15 android:label="@string/app_name" 16 android:supportsRtl="true" 17 android:theme="@style/Theme.Bluetoothyoutube" 18 tools:targetApi="31"> 19 <activity 20 android:name=".MainActivity" 21 android:exported="true"> 22 <intent-filter> 23 <action android:name="android.intent.action.MAIN" /> 24 25 <category android:name="android.intent.category.LAUNCHER" /> 26 </intent-filter> 27 </activity> 28 </application> 29 30</manifest>
activity_main.xml
1 2<?xml version="1.0" encoding="utf-8"?> 3<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:app="http://schemas.android.com/apk/res-auto" 5 xmlns:tools="http://schemas.android.com/tools" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent" 8 tools:context=".MainActivity"> 9 10 <Button 11 android:id="@+id/btON" 12 android:layout_width="wrap_content" 13 android:layout_height="wrap_content" 14 android:layout_marginStart="8dp" 15 android:layout_marginTop="8dp" 16 android:layout_marginEnd="8dp" 17 android:text="Bluetooth ON" 18 app:layout_constraintEnd_toStartOf="@+id/btOFF" 19 app:layout_constraintStart_toStartOf="parent" 20 app:layout_constraintTop_toTopOf="parent" /> 21 22 <Button 23 android:id="@+id/btOFF" 24 android:layout_width="wrap_content" 25 android:layout_height="wrap_content" 26 android:layout_marginStart="8dp" 27 android:layout_marginTop="8dp" 28 android:layout_marginEnd="8dp" 29 android:text="Bluetooth OFF" 30 app:layout_constraintEnd_toEndOf="parent" 31 app:layout_constraintHorizontal_bias="0.916" 32 app:layout_constraintStart_toStartOf="parent" 33 app:layout_constraintTop_toTopOf="parent" /> 34</androidx.constraintlayout.widget.ConstraintLayout>

あなたの回答
tips
プレビュー