最終更新:
moonlight_aska 2012年06月20日(水) 21:48:37履歴
周辺のBluetoothデバイスのRSSI値を取得するには, 他のBluetoothデバイス発見時にBluetoothDevice#EXTRA_RSSIを使う.
注) ペアリングしたBluetoothデバイスのRSSI値取得はできないようである.

注) ペアリングしたBluetoothデバイスのRSSI値取得はできないようである.

- Bluetooth01.java
- 他のBluetoothデバイスのスキャンについては, Bluetoothデバイスを探すを参照.
- Intent#getParcelableExtraメソッドで, BluetoothDeviceオブジェクトを取得する.
- Intent#getShortExtraメソッドで, BluetoothDevice.EXTRA_RSSIを指定して, RSSIを取得する.
package com.moonlight_aska.android.bluetooth01;
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 Bluetooth01 extends Activity {
private BluetoothAdapter mBtAdapter;
private TextView mScanResult;
private String mResult = "";
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// RSSI値読み出し
int rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, Short.MIN_VALUE);
mResult += "Device : " + device.getName() + " RSSI=" + rssi + "\n";
mScanResult.setText(mResult);
}
}
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mScanResult = (TextView)findViewById(R.id.bt_text);
// インテントフィルタの作成
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
// ブロードキャストレシーバの登録
registerReceiver(mReceiver, filter);
// BluetoothAdapterのインスタンス取得
mBtAdapter = BluetoothAdapter.getDefaultAdapter();
// Bluetooth有効
if (!mBtAdapter.isEnabled()) {
mBtAdapter.enable();
}
// 周辺デバイスの検索開始
mBtAdapter.startDiscovery();
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
// 検索中止
if (mBtAdapter.isDiscovering()) {
mBtAdapter.cancelDiscovery();
}
unregisterReceiver(mReceiver);
}
}
このページへのコメント
返信ありがとうございます。
AndroidManifest.xmlにて、「permission.BLUETOOTH」「permission.BLUETOOTH_ADMIN」の設定が抜けていました。
Eclipse上でAndroidアプリケーションで実行したところ、同様のエラーが出ましたが、
実機では無事成功しました。
シミュレーターではBluetooth機能が備わっていないため、エラーがでるのでしょうか。
ご指摘ありがとうございました。大変助かりました。
askaです.
AndroidManifest.xmlでBluetoothのパーミションは設定されていますか?
まだでしたら, 「Bluetoothデバイスを探す」を参照して, AndroidManifest.xml内で, Bluetooth関連のパーミションを設定して, 再度試してみてください.
こちらのソースを引用させていただき、Eclipseを用いてAndroid2.2で実行したところ、
「The Application has stopped unexpectedly.」
となり、起動しませんでした。
実機(ZEN Touch2)においても同様の結果でした。
改善策、問題点等お気づきの点がございましたら
返信いただけると助かります。