Bluetoothチャットアプリを作る
Creating a Bluetooth Chat Application
ここではクライアントと同じようにサーバとして動作する
Bluetoothを使ったAndroidアプリケーションを構築します。
このアプリケーションがサーバとして動くことで、
他のBluetoothデバイスが接続でき、接続したデバイスと
コミュニケーションがとれます。しかも同時に。
このアプリケーションにはBluetoothを介して他のデバイスに
接続する能力があります。
いまから作るアプリケーションは
●同じアプリケーションが走っている他のBluetoothデバイスを
見つけることができます。
●同じアプリケーションが走っている他のデバイスを見つけます。
●入ってくるBluetoothソケット接続と同期する
Bluetoothソケットサーバを走らせます。
●ソケットクライアントを使って他のBluetoothデバイスと
接続します。
ちょっと注釈
ここで作るアプリケーションをテストするためには、
Bluetoothが使えるデバイスが2台必要になります。
Creating the Project
新しいAndroidアプリケーションプロジェクトを作り、
UsingBluetoothと名づけます。
AndroidManifest.xmlファイルに必要になるパーミッションを加えます。
<uses-permission android:name="android.permission.BLUETOOTH />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
Creating the User Interface
activity_main.xmlファイルを設定して、以下の様に
res/layoutフォルダに配置します。
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true" >
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="MakeDiscoverable"
android.text="Make Discoverable" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="DiscoverDevices"
android:text="Discover Devices" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Devices discovered (tap to connect)" />
<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="120dp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Enter message here" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="SendMessage"
android:text="Send Message" />
<EditText
android:id="@+id/txtMessage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
androidhint="Message to send to the selected device />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Message Received" />
<TextView
android:id="@+id/txtData"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<ScrollView>
これでユーザインターフェースが出来上がります。
Adding New Classes
接続している他のBluetoothデバイスと同じように
同期するためのいくつかのスレッドを作る必要があります。
CommsThread.Java
ConnectToServerThread.Java
ServerThread.Java
このスレッドをnet.learn2develop.usingbluetoothオブジェクトの
下に作ります。
Importing the Packages Used
MainActivity.javaファイルに以下のパッケージをインポートします。
import java.io.IOException;
import java.util.ArrayList;
import android.app.ListActivity;
import android.bluetooth.BluetoothAdapter;
import android.blutooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;,br />
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
Declaring the Objects
MainActivity.javaファイルの中に以下のようなオブジェクトを
宣言しておきます。
public clas MainActivity extends ListActivity{
public final static String UUID = "3606f360-e4df-11e0-9572-0800200c9a66";
BluetoothAdapter bluetoothAdapter;
BroadcastReceiver discoverDeviceReceiver;
BroadcastReceiver discoveryFinishedReceiver;
//---store all the discovered devices---
ArrayList<BluetoothDevice>
ArraList<String> discoveredDevicesNames;
static TextView txtData;
EditText txtMessage;
//---thread for running the server socket---
serverThread serverThread;
//---thread for connecting to the client socket---
ConnectToServerThread connectToServerThread;
アクティビティがListViewを格納することで、
このアクティビティをListActivity基底クラスのサブクラスに
する必要があることに注目します。
onCreate()メソッドの中で以下のようなオブジェクトを初期化します。
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//---init the ArrayList objects and bluetooth adapter---
discoveredDevices = new ArrayList<BluetoothDevice>();
discoveredDevicesNames = new ArrayList<String>();
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
//---for displaying the messages received---
txtData = (TextView) findViewById(R.id.txtData);
txtMessage = (EditText) findViewById(R.id.txtMessage);
}
descoveredDevicesオブジェクトは発見したBluetoothデバイスの
リストを保存していきます。
それぞれのデバイスはBluetoothDeviceオブジェクトとして
表現されます。deicoveredDevicesNamesオブジェクトは
descoveredDevicesオブジェクトの中で一致しているデバイスの名前を
ユーザに分かりやすい形で保存します。
Making Your Device Discoverable
MainActivity.javaファイルに以下のようなメソッドを追加します。
//---make yourself discoverable---
public void MakeDiscoverable(View view){
Intent i = new Intent(
BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
i.putExtra(
BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity(i);
}
上のコードは300秒(5分間)Bluetoothを使えるデバイスを
探すコードになります。
このページのつづき、他のデバイスを見つける
に飛びます。
|
ページ内メニューです。
新しいプロジェクトを作ります
新しいクラスを加えます
オブジェクトを宣言します
他のデバイスを見つけられるようにします
【用語解説】
XML
HTMLと同じような言語ですが、
すべての型の複雑なデータ構造を
送信するための設計ができる
ファイル型式。
オブジェクト
ある手続きを持った
データアイテムで、
他の手続きと関連づけされるもの
metaタグ
ウェブページに与える
情報の宣言につかわれ
検索エンジン・ウェブアプリ
などにウェブページの内容を
伝える役目をする。
metaタグの内容は
ページ内に表示されない。
コンストラクタ
オブジェクト指向
プログラミングで使われ
新しいオブジェクトが
作られ時に呼び出される
メソッドのこと。
クラス
オブジェクト指向
プログラミングでの
オブジェクトの型
オブジェクト指向プログラミング
プログラミングの
方式のことで、
プログラマはデータ型を
定義できるだけでなく
自動的にデータ型と
メソッドを関連付け
できる
アクティビティ
Androidにおける
アクティビティは
Androidアプリの画面に
相当するもの
ボタンやウェブページ
など表示されている
すべてのもののこと。
view
Androidのアプリを開発する
上で使う部品のことで、
トリガボタン・イメージボタン
チェックボックスなどのこと。
UI
ユーザインターフェースのこと。
Bind(バインド)
シンボルとデータを
関連させるため
または、データの一片と
他のものを関連させること。
以下は例。
・変数に値を入れる。
(変数の初期化)
・特定のEthernetポートと
ネットワークプロトコルを
関連付けする。
・Javaの変数のセット上に
XML文書を配置すること。
または、ほかの
プログラミング言語でも。
|