Android-bluetooth
Android-Bluetooth
多くの方法の中で、Bluetoothは2つの異なるデバイス間でデータを送受信する方法です。 Androidプラットフォームには、デバイスが他のBluetoothデバイスとワイヤレスでデータを交換できるようにするBluetoothフレームワークのサポートが含まれています。
Androidは、これらのさまざまな操作を実行するBluetooth APIを提供します。
- 他のBluetoothデバイスをスキャンする
- ペアリングされたデバイスのリストを取得する *サービス検出を介して他のデバイスに接続する
Androidは、Bluetoothと通信するBluetoothAdapterクラスを提供します。 静的メソッドgetDefaultAdapter()を呼び出して、この呼び出しのオブジェクトを作成します。 その構文は次のとおりです。
private BluetoothAdapter BA;
BA = BluetoothAdapter.getDefaultAdapter();
デバイスのBluetoothを有効にするには、次のBluetooth定数ACTION_REQUEST_ENABLEでインテントを呼び出します。 その構文は次のとおりです。
Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnOn, 0);
この定数とは別に、異なるタスクをサポートするAPIが提供する他の定数があります。 それらは以下にリストされています。
Sr.No | Constant & description |
---|---|
1 |
この定数は、Bluetoothの検出をオンにするために使用されます |
2 |
ACTION_STATE_CHANGED この定数は、Bluetoothの状態が変更されたことを通知します |
3 |
ACTION_FOUND この定数は、検出された各デバイスに関する情報を受信するために使用されます |
Bluetoothを有効にすると、getBondedDevices()メソッドを呼び出して、ペアリングされたデバイスのリストを取得できます。 Bluetoothデバイスのセットを返します。 その構文は次のとおりです。
private Set<BluetoothDevice>pairedDevices;
pairedDevices = BA.getBondedDevices();
parried Devicesの他に、Blueetoothをさらに制御できるAPIの他のメソッドがあります。 それらは以下にリストされています。
Sr.No | Method & description |
---|---|
1 |
enable() このメソッドは、有効になっていない場合にアダプターを有効にします |
2 |
isEnabled() アダプタが有効な場合、このメソッドはtrueを返します |
3 |
disable() このメソッドはアダプターを無効にします |
4 |
getName() このメソッドは、Bluetoothアダプターの名前を返します |
5 |
setName(String name) このメソッドは、Bluetooth名を変更します |
6 |
getState() このメソッドは、Bluetoothアダプターの現在の状態を返します。 |
7 |
startDiscovery() この方法は、Bluetoothの検出プロセスを120秒間開始します。 |
例
この例では、Bluetoothを操作し、Bluetoothによってペアのデバイスのリストを表示するBluetoothAdapterクラスのデモを提供します。
この例を試すには、実際のデバイスでこれを実行する必要があります。
Steps | Description |
---|---|
1 | You will use Android studio to create an Android application a package com.example.sairamkrishna.myapplication. |
2 | Modify src/MainActivity.java file to add the code |
3 | Modify layout XML file res/layout/activity_main.xml add any GUI component if required. |
4 | Modify AndroidManifest.xml to add necessary permissions. |
5 | Run the application and choose a running android device and install the application on it and verify the results. |
*src/MainActivity.java* のコンテンツは次のとおりです。
package com.example.sairamkrishna.myapplication;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Set;
public class MainActivity extends Activity {
Button b1,b2,b3,b4;
private BluetoothAdapter BA;
private Set<BluetoothDevice>pairedDevices;
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.button);
b2=(Button)findViewById(R.id.button2);
b3=(Button)findViewById(R.id.button3);
b4=(Button)findViewById(R.id.button4);
BA = BluetoothAdapter.getDefaultAdapter();
lv = (ListView)findViewById(R.id.listView);
}
public void on(View v){
if (!BA.isEnabled()) {
Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnOn, 0);
Toast.makeText(getApplicationContext(), "Turned on",Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Already on", Toast.LENGTH_LONG).show();
}
}
public void off(View v){
BA.disable();
Toast.makeText(getApplicationContext(), "Turned off" ,Toast.LENGTH_LONG).show();
}
public void visible(View v){
Intent getVisible = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(getVisible, 0);
}
public void list(View v){
pairedDevices = BA.getBondedDevices();
ArrayList list = new ArrayList();
for(BluetoothDevice bt : pairedDevices) list.add(bt.getName());
Toast.makeText(getApplicationContext(), "Showing Paired Devices",Toast.LENGTH_SHORT).show();
final ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, list);
lv.setAdapter(adapter);
}
}
*activity_main.xml* のコンテンツは次のとおりです。
'_ここでabcはfinddevguidesのロゴについて示しています。_
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:transitionGroup="true">
<TextView android:text="Bluetooth Example"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textview"
android:textSize="35dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tutorials point"
android:id="@+id/textView"
android:layout_below="@+id/textview"
android:layout_centerHorizontal="true"
android:textColor="#ff7aff24"
android:textSize="35dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@drawable/abc"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:theme="@style/Base.TextAppearance.AppCompat"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Turn On"
android:id="@+id/button"
android:layout_below="@+id/imageView"
android:layout_toStartOf="@+id/imageView"
android:layout_toLeftOf="@+id/imageView"
android:clickable="true"
android:onClick="on"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get visible"
android:onClick="visible"
android:id="@+id/button2"
android:layout_alignBottom="@+id/button"
android:layout_centerHorizontal="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="List devices"
android:onClick="list"
android:id="@+id/button3"
android:layout_below="@+id/imageView"
android:layout_toRightOf="@+id/imageView"
android:layout_toEndOf="@+id/imageView"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="turn off"
android:onClick="off"
android:id="@+id/button4"
android:layout_below="@+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_alignParentBottom="true"
android:layout_alignLeft="@+id/button"
android:layout_alignStart="@+id/button"
android:layout_below="@+id/textView2"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Paired devices:"
android:id="@+id/textView2"
android:textColor="#ff34ff06"
android:textSize="25dp"
android:layout_below="@+id/button4"
android:layout_alignLeft="@+id/listView"
android:layout_alignStart="@+id/listView"/>
</RelativeLayout>
*Strings.xml* のコンテンツは次のとおりです。
<resources>
<string name="app_name">My Application</string>
</resources>
*AndroidManifest.xml* のコンテンツは次のとおりです。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sairamkrishna.myapplication" >
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
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>
アプリケーションを実行してみましょう。 実際のAndroidモバイルデバイスをコンピューターに接続していると思います。 Androidスタジオからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[画像を実行:/android/images/eclipse_run.jpg [Eclipse Run Icon]アイコンをクリックします。Bluetoothがオンにならない場合は、 Bluetoothを有効にする許可を求めます。
[表示]ボタンを選択して、表示をオンにします。 次の画面が表示され、検出を120秒間オンにする許可を求められます。
[デバイスのリスト]オプションを選択します。 リストビューにペアのデバイスがリストされます。 私の場合、ペアリングされたデバイスは1つだけです。 以下に示します。
次に、[オフにする]ボタンを選択して、Bluetoothをオフにします。 Bluetoothをオフにすると、Bluetoothが正常にオフになったことを示す次のメッセージが表示されます。