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の状態をモニタ
【用語解説】
XML
HTMLと同じような言語ですが、
すべての型の複雑なデータ構造を
送信するための設計ができる
ファイル型式。
オブジェクト
ある手続きを持った
データアイテムで、
他の手続きと関連づけされるもの
metaタグ
ウェブページに与える
情報の宣言につかわれ
検索エンジン・ウェブアプリ
などにウェブページの内容を
伝える役目をする。
metaタグの内容は
ページ内に表示されない。
コンストラクタ
オブジェクト指向
プログラミングで使われ
新しいオブジェクトが
作られ時に呼び出される
メソッドのこと。
クラス
オブジェクト指向
プログラミングでの
オブジェクトの型
オブジェクト指向プログラミング
プログラミングの
方式のことで、
プログラマはデータ型を
定義できるだけでなく
自動的にデータ型と
メソッドを関連付け
できる
アクティビティ
Androidにおける
アクティビティは
Androidアプリの画面に
相当するもの
ボタンやウェブページ
など表示されている
すべてのもののこと。
view
Androidのアプリを開発する
上で使う部品のことで、
トリガボタン・イメージボタン
チェックボックスなどのこと。
UI
ユーザインターフェースのこと。
Bind(バインド)
シンボルとデータを
関連させるため
または、データの一片と
他のものを関連させること。
以下は例。
・変数に値を入れる。
(変数の初期化)
・特定のEthernetポートと
ネットワークプロトコルを
関連付けする。
・Javaの変数のセット上に
XML文書を配置すること。
または、ほかの
プログラミング言語でも。
|