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

Androidアプリ開発 ネットワークプログラミング

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を使えるデバイスを
探すコードになります。
このページのつづき、他のデバイスを見つける
に飛びます。

ホーム
便利堂ロゴ
inserted by FC2 system