ウェブアプリケーション,インジェクション,コマンドインジェクション

Androidアプリ開発 電話機能偏

Bluetoothを使えるようにする

Enabling Bluetooth

もし開発者がBluetoothアプリケーションを書いているのなら、
デバイスのBluetoothラジオをプログラム的に
利用できるようになります。
デバイスにあるBluetoothラジオを使いだす前に、
Bluetoothがデバイスで使えるかどうかをチェックすることを
おすすめします。
ここでは、
●Bluetoothラジオがデバイスで使えるどうかをチェック。
●もし入手可能ならBluetoothラジオを使える様にする。
●デバイスのBluetoothラジオの状態をモニタする。
の3つをどのようにするかを示します。

Checking for Bluetooth Availability

デバイスのBluetoothが入手できるかチェックするためには、
いろんな型のBluetoothAdapterを作ります。
BluetoothAdapterクラスをインスタンス化するためには、
BluetoothAdapter.getDefaultAdapter()メソッドを呼び出します。
もしBluetoothがデバイスで入手できないのなら、
その時はBluetoothAdapter.getDefaultAdapter()メソッドは
nullを返します。コードを以下のように示します。

package net.learn2develop.bluetooth;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends Activity{
  BluetoothAdapter bluetoothAdapter;

  //---check if bluetooth is available on the device---
  private boolean BluetoothAvailable(){
   if(bluetoothAdapter == null)
     return false;
   else
     return true;
   }

  /**Called when the activity is first created */
  @Override
  public void onCreate(Bundle savedInstanceState){
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);

   bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
   Toast.makeText(this, "Bluetooth available: " + BluetoothAvailable(),
     Toast.LENGTH_LONG).show();
  }
}

Enabing Bluetooth

もしBluetoothが手に入るならば、
次は使えるがどうかをチェックすることになるでしょう。
そのチェックはBluetoothAdapterオブジェクトの
isEnabled()メソッドを使ってします。
もし使えないようならば、Intentオブジェクトを使うこともできます。

package ner.learn2develop.bluetooth;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends Activity{
  BluetoothAdapter bluetoothAdapter;
  static final int REQUEST_ENABLE_BT = 0;

  //---check if bluetooth is available on the device---
  private boolean BlietoothAvailable(){
   if(bluetoothAdapter == null)
      return false;
   else
      return true;
  }

  //---enable bluetooth on the device---
  private void EnableBluetooth(){
   if(BluetoothAvailabla() && !bluetoothAdapter.isEnabled()){
     Intent i = new Intent(
       BluetoothAdapter.ACTION_REQUEST_ENABLE);
     startActivityForResult(i, REQUEST_ENABLE_BT);
   }
  }

  public void onActivityResult(int requestCode,
         int resultCode, Intent data){
   if(requestCode == REQUEST_ENABLE_BT){
    if(resultCode == RESULT_OK){
     Toast.makeText(this, "Bluetooth turned on",
         Toast.LENGTH_SHORT).show();
    }
   }
  }

  /**Called when the activity is first created */
  @Override
  public void onCreate(Bundle savedInstanceState){
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);

   bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
   Toast.makeText(this, "Bluetooth available: "+ BluetoothAvailable(),
      Toast.LENGTH_LONG).show();

   if (BluetoothAvaiable())
    EnableBluetooth();
  }
}

ユーザにBluetoothをオンにするように促すダイアログを表示するよう
OSに要求するため、Intentオブジェクトを使うべきなのに注目します。
ユーザにBluetooth機能をオンにするように促した後、
本当にユーザが機能をオンにしたかどうかを判断するためには、
startActivityForResult()メソッドを使って、
Intentオブジェクトを始めます。
onActivity()メソッドを再定義することで
Bluetoothが使える様になったかどうかを知ることができます。
ユーザにBluetoothできるようにするためには、
BLUETOOTH_ADMINとBLUETOOTHパーミッションを加える必要があります。

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

Monitoring the State of Bluetooth

もしデバイスのBluetoothの状態をモニタしたければ、
BroadcastReceiverクラスを使えます。
以下のコードはBluetoothラジオの状態の変化に注目するために、
どのようにBroadcastReceiverクラスと一緒に
IntentFilterオブジェクトを使うかを示します。

package ner.learn2develop.bluetooth;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends Activity{
  BluetoothAdapter bluetoothAdapter;
  static final int REQUEST_ENABLE_BT = 0;

  //---check if bluetooth is availlable on the device---
  private boolean BluetoothAvailable(){
   if (bluetoothAdapter == null)
     return false;
   else
     return true;
  }

  //---enable bluetooth on the device---
  private void EnableBluetooth(){
   if(BluetoothAvailable() && !bluetoothAdapter.isEnabled()){
    BluetoothAdapter.ACTION_REQUEST_ENABLE);
   startActivityForResult(i, REQUEST_ENABLE_BT);
   }
  }

  public void onActivityResult(int requestCode, int resultCode, Intent data){
   if (requestCode, Intent data){
    if (resultCode == RESULT_OK){
     Toast.makeTest(this, "Bluetooth turned on",
         Toast.LENGTH_SHORT).show();
    }
   }
  }

  /**Called when the activity is first created */
  @Override
  public void onCreate(Bundle savedInstanceState){
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);

   bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
   Toast.makeText(this, "Bluetooth available: " + BluetoothAvailable(),
        Toast.LENGTH_LONG).show();

   if (BluetoothAvailable())
    EnableBluetooth();
   MyBTBroadcastReceiver mReceiver = new MyBTBroadcastReceiver();

   IntentFilater intentFilter = new
      IntentFilter("android.bluetooth.adapter.action.STATE_CHANGED");
   registerReceiver(mReceiver, intentFilter);
  }

  public class MyBTBroadcastReceiver extends BroadcastReceiver{
   @Override
   public void onReceive(Context context, Intent intent){
    int state = intent.getExtras().getInt(BluetoothAdapter.EXTRA_STATE);
    switch(state){
    BluetoothAdapter.STATE_OFF:
     Toast.makeText(context, "off", Toast.LENGTH_SHORT).show();
     break;
    case BluetoothAdapter.STATE_TURNING_OFF:
     Toast.makeText(context, "Turning off",
        Toast.LENGTH_SHORT).show();
     break;
    case BluetoothAdapter.STATE_ON:
     Toast.makeText(context, "on", Toase.LENGTH_SHORT).show();
     break;
    case BluetoothAdapter.STATE?TURNING_ON:
     Toast.makeText(context, "Turning On",
        Toast.LENGTH_SHORT).show();
     break;
    }
   }
  }
}

IntentFilterオブジェクトは、
android.bluetooth.adapter.action.STATE_CHANGEDアクションを
監視します。
デバイスのBluetoothの状態が変化したときにはいつでも、
IntentFilterオブジェクトはBroadcastReceiverクラスを発動します。
BroadcastReceiverの中では、Bluetooth adapterの現在の状態を
引き出し、状態をチェックします。
もし、ユーザの知識に頼らずデバイスのBluetoothを
プログラム的に使える様にしたいなら、
BluetoothAdapterオブジェクトのenable()メソッドを
使うことができます。

//---disable Bluetooth---
bluetoothAdapter.enable();

ちょっと注釈

開発者はユーザのはっきりとした同意がない限り
Bluetoothをプログラム的に使える様にするべきではありません。


Bluetoothの機能を停止するためには、disable()メソッドを使います。

//---enable Bluetooth---
bluetoothAdapter.disable();
ホーム
便利堂ロゴ
ページ内メニュー

Bluetoothが入手可能かチェック
Bluetoothを使えるようにする
Bluetoothの状態をモニタ


inserted by FC2 system