###前提・実現したいこと
AndroidStudio2.21でBluetoothを用いて端末間のRSSI値(電波強度)を求めようとしています。
###発生している問題・エラーメッセージ
以下のソースコードで実行しているのですが、実行結果が添付ファイルのスクリーンショットのように下部のデバイス検索中以降が出ません。javaファイル内のBroadcastReceiver内のプログラムがスルーされている?ような感じでRSSI値が受け取れずにいます。
###該当のソースコード
①newbluetooth.java ソースコード
package jp.co.webtech.bluetooth; import java.util.Set; import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.widget.TextView; public class newbluetooth extends Activity { private BluetoothAdapter myBluetoothAdapter; private TextView textView; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_bluetooth01); textView = (TextView) findViewById(R.id.textBlue); // Bluetooth myBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (myBluetoothAdapter == null) { textView.append("Bluetoothをサポートしていません。\n"); } else { textView.append("Bluetoothをサポートしています。\n"); //Bluetoothが利用できるか確認する if (myBluetoothAdapter.isEnabled()) { textView.append("Bluetoothを利用できます。\n"); //自機ディバイスの調査 getLocalInformation(); //ペアリング済みのディバイスの調査 findPairedDevices(); //ディバイスを見つけて調査 discoverDevices(); }else{ textView.append("Bluetoothを利用できません。\n"); //Bluetoothを使えるようにする処理を書いたりする。 } } } // 自機ディバイスの調査メソッド private void getLocalInformation() { // TODO 自動生成されたメソッド・スタブ textView.append("自機Bluetoothディバイスの調査\n"); textView.append("デバイス名:" + myBluetoothAdapter.getName() + "\n"); textView.append("アドレス:" + myBluetoothAdapter.getAddress() + "\n"); textView.append("SCAN_MODE:"); switch (myBluetoothAdapter.getScanMode()) { case BluetoothAdapter.SCAN_MODE_CONNECTABLE: textView.append("SCAN_MODE_CONNECTABLE" + "\n"); break; case BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE: textView.append("SCAN_MODE_CONNECTABLE_DISCOVERABLE" + "\n"); break; case BluetoothAdapter.SCAN_MODE_NONE: textView.append("SCAN_MODE_NONE" + "\n"); break; } textView.append("STATE:"); switch (myBluetoothAdapter.getState()) { case BluetoothAdapter.STATE_OFF: textView.append("STATE_OFF\n"); break; case BluetoothAdapter.STATE_ON: textView.append("STATE_ON\n"); break; case BluetoothAdapter.STATE_TURNING_OFF: textView.append("STATE_TURNING_OFF\n"); break; case BluetoothAdapter.STATE_TURNING_ON: textView.append("STATE_TURNING_ON\n"); break; } } // ペアリング済みの調査 private void findPairedDevices() { textView.append("\n登録済みデバイス\n"); Set<BluetoothDevice> pairedDevices = myBluetoothAdapter.getBondedDevices(); if (pairedDevices.size() > 0) { for (BluetoothDevice device : pairedDevices) { textView.append("デバイス名:" + device.getName() + "\n"); textView.append("アドレス:" + device.getAddress() + "\n"); textView.append("クラス:" + device.getBluetoothClass() + "\n"); } } else { textView.append("登録されているBluetoothデバイスはありません。\n"); } } // 他のBluetoothデバイスを見つけるレシーバーを登録 private final BroadcastReceiver mReceiver = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { String action = intent.getAction(); // When discovery finds a device if (BluetoothDevice.ACTION_FOUND.equals(action)) { // Get the BluetoothDevice object from the Intent BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); textView.append("デバイス名: " + device.getName() + "\n"); textView.append("アドレス: " + device.getAddress() + "\n"); short rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE); textView.append("RSSI: " + rssi + "dBm" + "\n"); } } }; // ディバイスを見つける private void discoverDevices() { // TODO 自動生成されたメソッド・スタブ IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy myBluetoothAdapter.startDiscovery(); textView.append("\nデバイスを検索中\n"); } }
②AndroidManifest.xml ソースコード
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="jp.co.webtech.bluetooth" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" />> <uses-permission android:name="android.permission.BLUETOOTH"/> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".newbluetooth"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
###試したこと
Android端末のBluetoothをON、周辺機器のBluetoothをON
###補足情報(言語/FW/ツール等のバージョンなど)
環境:AndroidStudio2.2
使用Android端末:Nexus9(回線通ってないため、ネットなど閲覧するときはWi-Fiを使用)
Bluetoothの電波強度を図る際の相方のBluetooth機:Raspberry Pi3 MODEL B
参考サイト:http://yamato-iphone.blogspot.jp/2012/06/bluetooth_09.html
実行結果は参考サイトの実行結果の下部、デバイス検索中以降が出ません。
初心者ですのでお手柔らかにお願いいたします。