Androidプログラマへの道 〜 Moonlight 明日香 〜 - 他のBluetoothデバイスから検出可能にする
他のBluetoothデバイスから検出可能に設定するには, インテント(Intent)を利用する.


      ↓


検出許可画面の呼び出し

  • Bluetooth02.java
    • ACTION_REQUEST_DISCOVERABLEを指定して, Intentのインスタンスを生成する.
    • Intent#putExtraメソッドで, 検出可能時間を設定する.
    • startActivityメソッドで, インテント呼び出しする.
package com.moonlight_aska.android.bluetooth02;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Bluetooth02 extends Activity implements View.OnClickListener {
  private Button mStartBtn;
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mStartBtn = (Button)findViewById(R.id.start_btn);
    mStartgBtn.setOnClickListener(this);
  }

  @Override
  public void onClick(View v) {
    // TODO Auto-generated method stub
    if (v == mStartBtn) {
      Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
      // 検出時間の設定(120秒)
      intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 120);
      startActivity(intent);
    }
  }
}
  • AndroidManifest.xml
    • Bluetoot設定の操作を行うには, "android.permission.BLUETOOTH_ADMIN"パーミションを設定する.
    • "android.permission.BLUETOOTH_ADMIN"パーミションを使用する場合は, "android.permission.BLUETOOTH"パーミションも設定する.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.moonlight_aska.android.bluetooth02"
  android:versionCode="1"
  android:versionName="1.0" >
  <uses-sdk android:minSdkVersion="8" />
  <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
  <uses-permission android:name="android.permission.BLUETOOTH"/>

  <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
      android:name=".Bluetooth02"
      android:label="@string/app_name" >
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
  </application>
</manifest>