最終更新:
moonlight_aska 2012年06月21日(木) 22:40:59履歴
Bluetoothの状態取得, 設定の変更を行うには, BluetoothAdapterクラスを利用する.

↓ ↑


↓ ↑

- Bluetooth01.java
- BluetoothAdapter#getDefaultAdapterメソッドで, BluetoothAdapterのインスタンスを取得する.
- BluetoothAdapter#isEnabledメソッドで, Bluetoothの状態を取得する.
- BluetoothAdapter#disableめそっぢで, BluetoothをOFFに設定する.
- BluetoothAdapter#enableメソッドで, BluetoothをONに設定する.
package com.moonlight_aska.android.bluetooth01;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
public class Bluetooth01 extends Activity implements View.OnClickListener {
private BluetoothAdapter mBtAdapter;
private Button mBtBtn;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mBtBtn = (Button)findViewById(R.id.bt_btn);
mBtBtn.setOnClickListener(this);
// BluetoothAdapterのインスタンス取得
mBtAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBtAdapter == null) {
mBtBtn.setText(R.string.nodevice_label);
}
else {
if (mBtAdapter.isEnabled()) {
mBtBtn.setText(R.string.off_label);
}
else {
mBtBtn.setText(R.string.on_label);
}
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == mBtBtn) {
if (mBtAdapter.isEnabled()) {
// BluetoothをOnからOffに
mBtAdapter.disable();
mBtBtn.setText(R.string.on_label);
}
else {
// BluetoothをOffからOnに
mBtAdapter.enable();
mBtBtn.setText(R.string.off_label);
}
}
}
}
- AndroidManifest.xml
- Bluetoot設定の操作を行うには, "android.permission.BLUETOOTH_ADMIN"パーミションを設定する.
- "android.permission.BLUETOOTH_ADMIN"パーミションを使用する場合は, "android.permission.BLUETOOTH"パーミションも設定する.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.moonlight_aska.android.bluetooth01"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".Bluetooth01"
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>
タグ
このページへのコメント
初めまして。
スマホのBluetoothがオフになってしまい、なんとかONにしたくて検索してるものなのですが、スマホ画面上がタッチ出来ません。
PC繋いでONにしたり、強制的にONにするアプリなどで出来ませんでしょうか…
ちなみにスマホにマウスを繋いでもポインターが出ないタイプになります。