利用可能な場合、有効になっているか調べて、
有効になっていない場合有効にする。
import android.os.Bundle;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.util.Log;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Bluetoothアダプタが利用可能か調べる
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
Log.v("タグ", "Bluetoothアダプタが利用出来ません");
}else{
Log.v("タグ", "Bluetoothアダプタが利用出来ます");
//Bluetoothアダプタが有効か調べる
if (mBluetoothAdapter.isEnabled()) {
Log.v("タグ", "Bluetoothアダプタが有効です");
}else {
Log.v("タグ", "Bluetoothアダプタを有効にします");
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
}
}
}
}
0 件のコメント:
コメントを投稿