質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Q&A

0回答

482閲覧

androidアプリで、起動時に権限のダイアログを出したい

shimishimi

総合スコア0

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

0グッド

0クリップ

投稿2023/03/19 23:41

編集2023/03/20 02:23

実現したいこと

ここに実現したいことを箇条書きで書いてください。

  • androidstudio(Java)で、Arduino Uno WiFi Rev2とBLE通信し、
    アプリ上のボタンでArduinoのLEDを操作するアプリにしたい。

前提

ここに質問の内容を詳しく書いてください。
BLE通信するにあたり、位置情報と付近のデバイスへのアクセスを許可しないといけないため、起動時に許可を求めるダイアログを出そうとしているが、位置情報は出るものの、付近のデバイスへのアクセスの方が出てこないです。
もちろんアプリ設定を開いて直接許可することもできますが、アプリ起動時にダイアログが出るようにしたいです。
下記にも掲載させていただいております。
https://ja.stackoverflow.com/questions/94251/android%e3%82%a2%e3%83%97%e3%83%aa%e3%81%a7-%e8%b5%b7%e5%8b%95%e6%99%82%e3%81%ab%e6%a8%a9%e9%99%90%e3%81%ae%e3%83%80%e3%82%a4%e3%82%a2%e3%83%ad%e3%82%b0%e3%82%92%e5%87%ba%e3%81%97%e3%81%9f%e3%81%84

発生している問題・エラーメッセージ

付近のデバイスへのアクセス権限許可ダイアログが出てこない。

該当のソースコード

Java(androidstudio)

1package com.example.bletest2; 2 3import androidx.appcompat.app.AppCompatActivity; 4import androidx.core.app.ActivityCompat; 5import androidx.core.content.ContextCompat; 6 7import android.Manifest; 8import android.app.AlertDialog; 9import android.bluetooth.BluetoothAdapter; 10import android.bluetooth.BluetoothDevice; 11import android.bluetooth.BluetoothGatt; 12import android.bluetooth.BluetoothGattCallback; 13import android.bluetooth.BluetoothGattCharacteristic; 14import android.bluetooth.BluetoothGattService; 15import android.bluetooth.BluetoothManager; 16import android.bluetooth.BluetoothProfile; 17import android.bluetooth.BluetoothSocket; 18import android.content.Context; 19import android.content.DialogInterface; 20import android.content.pm.PackageManager; 21import android.os.Bundle; 22import android.util.Log; 23import android.view.View; 24import android.widget.Button; 25import android.widget.Toast; 26 27import java.io.IOException; 28import java.io.InputStream; 29import java.io.OutputStream; 30import java.util.Set; 31import java.util.UUID; 32 33public class MainActivity extends AppCompatActivity { 34 private BluetoothAdapter mBluetoothAdapter; 35 private BluetoothGatt mBluetoothGatt; 36 private BluetoothGattCharacteristic mSwitchCharacteristic; 37 private Button mHiButton; 38 private Button mLoButton; 39 40 private OutputStream outputStream; 41 private InputStream inputStream; 42 43 44 45 private static final String LED_SERVICE_UUID = "19B10010-E8F2-537E-4F6C-D104768A1214"; 46 private static final String SWITCH_CHARACTERISTIC_UUID = "19B10011-E8F2-537E-4F6C-D104768A1214"; 47 48 private static final int PERMISSION_REQUEST_LOCATION = 0; 49 private static final int PERMISSION_REQUEST_BLUETOOTH = 1; 50 51 private static final int REQUEST_PERMISSIONS = 2; 52 53 54 55 @Override 56 protected void onCreate(Bundle savedInstanceState) { 57 super.onCreate(savedInstanceState); 58 setContentView(R.layout.activity_main); 59 60 61 // 権限の確認 62 if (checkLocationPermission()) { 63 System.out.println("通ったよ1"); 64 if (checkBluetoothPermission()) { 65 System.out.println("通ったよ2"); 66 connectToDevice(); 67 } 68 } 69 70 mHiButton = findViewById(R.id.hi_button); 71 mHiButton.setOnClickListener(new View.OnClickListener() { 72 @Override 73 public void onClick(View v) { 74 if (mSwitchCharacteristic != null) { 75 mSwitchCharacteristic.setValue(1, BluetoothGattCharacteristic.FORMAT_UINT8, 0); 76 if (checkBluetoothPermission()) { 77 mBluetoothGatt.writeCharacteristic(mSwitchCharacteristic); 78 } 79 } 80 } 81 }); 82 83 mLoButton = findViewById(R.id.lo_button); 84 mLoButton.setOnClickListener(new View.OnClickListener() { 85 @Override 86 public void onClick(View v) { 87 if (mSwitchCharacteristic != null) { 88 mSwitchCharacteristic.setValue(0, BluetoothGattCharacteristic.FORMAT_UINT8, 0); 89 if (checkBluetoothPermission()) { 90 mBluetoothGatt.writeCharacteristic(mSwitchCharacteristic); 91 } 92 } 93 } 94 }); 95 96 97 } 98 99 private boolean checkBluetoothPermission() { 100 if (ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH) != PackageManager.PERMISSION_GRANTED 101 || ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_ADMIN) != PackageManager.PERMISSION_GRANTED 102 || !checkLocationPermission()) { 103 System.out.println("通ったよ7"); 104 105 ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.BLUETOOTH, Manifest.permission.BLUETOOTH_ADMIN, Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_BLUETOOTH); 106 return false; 107 } else { 108 return true; 109 } 110 } 111 112 113 private boolean checkLocationPermission() { 114 if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED 115 || ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 116 if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)) { 117 new AlertDialog.Builder(this) 118 .setTitle("Location Permission Required") 119 .setMessage("Location permission is required to scan for Bluetooth devices.") 120 .setPositiveButton("OK", new DialogInterface.OnClickListener() { 121 @Override 122 public void onClick(DialogInterface dialogInterface, int i) { 123 ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_LOCATION); 124 } 125 }) 126 .create() 127 .show(); 128 } else { 129 ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_LOCATION); 130 } 131 return false; 132 } else { 133 return true; 134 } 135 } 136 137 @Override 138 public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { 139 super.onRequestPermissionsResult(requestCode, permissions, grantResults); 140 if (requestCode == REQUEST_PERMISSIONS) { 141 if (grantResults[0] == PackageManager.PERMISSION_GRANTED && 142 grantResults[1] == PackageManager.PERMISSION_GRANTED && 143 grantResults[2] == PackageManager.PERMISSION_GRANTED) { 144 connectToDevice(); 145 } else { 146 Toast.makeText(this, "Bluetoothの権限が必要です。", Toast.LENGTH_SHORT).show(); 147 } 148 } 149 } 150 151 private void connectToDevice() { 152 BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 153 BluetoothDevice device = null; 154 if (checkLocationPermission()) { 155 Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices(); 156 if (pairedDevices.size() > 0) { 157 for (BluetoothDevice bluetoothDevice : pairedDevices) { 158 if (bluetoothDevice.getName().equals("LED")) { 159 device = bluetoothDevice; 160 break; 161 } 162 } 163 } 164 } 165 166 BluetoothSocket bluetoothSocket = null; 167 try { 168 bluetoothSocket = device.createRfcommSocketToServiceRecord(UUID.fromString("○○○○○○○○○○○○○○")); 169 bluetoothSocket.connect(); 170 outputStream = bluetoothSocket.getOutputStream(); 171 inputStream = bluetoothSocket.getInputStream(); 172 } catch (IOException e) { 173 e.printStackTrace(); 174 } 175 } 176 177 178 179 180 private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() { 181 @Override 182 public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { 183 if (newState == BluetoothProfile.STATE_CONNECTED) { 184 if (checkBluetoothPermission()) { 185 mBluetoothGatt.discoverServices(); 186 } 187 } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { 188 mBluetoothGatt.close(); 189 } 190 } 191 192 @Override 193 public void onServicesDiscovered(BluetoothGatt gatt, int status) { 194 if (status == BluetoothGatt.GATT_SUCCESS) { 195 BluetoothGattService service = gatt.getService(UUID.fromString(LED_SERVICE_UUID)); 196 if (service != null) { 197 mSwitchCharacteristic = service.getCharacteristic(UUID.fromString(SWITCH_CHARACTERISTIC_UUID)); 198 } 199 } 200 } 201 }; 202 203 @Override 204 protected void onResume() { 205 super.onResume(); 206 } 207 208 @Override 209 protected void onPause() { 210 super.onPause(); 211 } 212} 213

試したこと

checkBluetoothPermission()、checkLocationPermission()を色々いじりましたがうまくいきませんでした

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

shimishimi

2023/03/20 02:23

すみませんでした。修正させていただきました。
jimbe

2023/03/20 03:55

参考にされたネットの情報等ありましたら、 URL を教えて頂けますでしょうか。
shimishimi

2023/03/20 04:18

ズバリ書かれているものがなく(正確には書かれていても古い)、色々なサイトから必要なところを抜き取ってきた感じですので、このURLというものが無いです・・・
1T2R3M4

2023/03/20 04:20

>質問内容にマルチポストをする理由を書き 上記の部分は理解できなかったのでしょうか。
jimbe

2023/03/20 04:59 編集

>BLE通信するにあたり、位置情報と付近のデバイスへのアクセスを許可しないといけないため、起動時に許可を求めるダイアログを出そうとしている の「BLE通信するにあたり、位置情報と付近のデバイスへのアクセスを許可しないといけない」や「起動時に許可を求めるダイアログ(を出さないといけない?)」というのは、どこからでしょうか。 また、お使いのアンドロイドのバージョン、アプリの API バージョン等は幾つでしょうか。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問