カテゴリー
SugiBlog Webデザイナー・プログラマーのためのお役立ちTips

Bluetoothを扱う Android

AndroidでBluetoothを使ってみます。

Bluetoothアダプタークラスをインポート

import android.bluetooth.BluetoothAdapter;

アダプターとリクエストコードの定義

BluetoothAdapter mBluetoothAdapter = null;
final int REQUEST_ENABLE_BT = 1; //任意のコード

アクティビティのonCreateでアダプターをセットします。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

	mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
}

続きを読む…»

7,148 views

SDカードにフォルダーを作成する mkDir

SDカード内にフォルダーを作成します。
再帰的に作成するにはmkDirsメソッドを使用します。
※パーミッションの記述は割愛します。

String PATH = Environment.getExternalStorageDirectory().toString() + "/DCIM/Example";

File f = new File(PATH);

if(!f.exists()) {
    try {
        f.mkdir();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
6,215 views

JSONデータを扱う [Android]

以下のようなJSONデータがあったとします。

{"json":[{"id": "00001","category": "カテゴリー1","title": "テスト1"},{"id": "00002","category": "カテゴリー2","title": "テスト2"}]}
String json_data = "{\"json\":[{\"id\": \"00001\",\"category\": \"カテゴリー1\",\"title\": \"テスト1\"},{\"id\": \"00002\",\"category\": \"カテゴリー2\",\"title\": \"テスト2\"}]}";

JSONArray jArray;
StringBuilder sb = new StringBuilder();

try {
    jArray = new JSONObject( json_data ).getJSONArray( "json" );

    for (int i = 0; i < jArray.length(); i++) {
        JSONObject jsonObj = jArray.getJSONObject(i);
        sb.append(jsonObj.getString("title"));
    }

    Toast.makeText(this, sb.toString(), Toast.LENGTH_LONG).show();

} catch (JSONException e) {
    e.printStackTrace();
} catch (Exception e) {
    e.printStackTrace();
}

コード上でデータを追加する場合(例外処理は上記と同様にしてださい)

JSONObject nJArray = new JSONObject();
nJArray.put("id", "00003");
nJArray.put("category", "カテゴリ3");
nJArray.put("title", "テスト3");
jArray.put(nJArray);
10,320 views

assetsフォルダーにあるテキストファイルを読み込む

assetsフォルダーに格納したテキストファイルを読み込みます。

InputStream inputStream = null;
BufferedReader reader = null;
try {
    inputStream = getResources().getAssets().open("ファイル名");
    reader = new BufferedReader(
              new InputStreamReader(inputStream));
    String line;
    StringBuilder sb = new StringBuilder();
    while ((line = reader.readLine()) != null) {
        Log.v("reader", line);
        if(line != ""){
            sb.append(line);
        }
    }
    String str = sb.toString();
    Toast.makeText(this, str, Toast.LENGTH_LONG).show();

} catch (IOException e) {
    e.printStackTrace();
} catch (SQLException e) {
    e.printStackTrace();
} catch (Exception e) {
    e.printStackTrace();
} finally {
    try {
        reader.close();
        inputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}
7,082 views

New I/Oの高速ファイル入出力

FileクラスにはCopyなどというメソッドがないようなので、調べていたらNew I/Oというものを発見したのでメモ。

java.nio.channels.FileChannelを使用して、簡単にコピーできます。

File inputFile  = new File("入力元ファイルのパス");
File outputFile = new File("出力先ファイルのパス");

try {
    FileChannel inputChannel  = new FileInputStream(inputFile).getChannel();
    FileChannel outputChannel = new FileOutputStream(outputFile).getChannel();

    // インプットチャネルの出力をアウトプットチャネルに接続
    inputChannel.transferTo(0, inputChannel.size(), outputChannel);

    inputChannel.close();
    outputChannel.close();
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
2,712 views