###前提・実現したいこと
AndroidStudioでBluetoothのソケット通信アプリを作っています。
###発生している問題・エラーメッセージ
http://blog.livedoor.jp/sce_info3-craft/archives/8337697.html
上記を参照して作ってみたのですが、起動した瞬間にアプリが落ちてしまいます。
そもそもプログラミング初心者なので根本的なところから間違っているかもわかりません。
###該当のソースコード(MainActivity.java) import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothManager; import android.bluetooth.BluetoothSocket; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; import android.widget.ToggleButton; import java.io.IOException; import java.io.OutputStream; import java.util.UUID; public class MainActivity extends AppCompatActivity { private BluetoothAdapter mBluetoothAdapter; // BTアダプタ private BluetoothDevice mBtDevice; // BTデバイス private BluetoothSocket mBtSocket; // BTソケット private OutputStream mOutput; // 出力ストリーム private Button button; // 送信ボタン @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) { Toast.makeText(this, R.string.bt_not_supported, Toast.LENGTH_SHORT).show(); finish(); } else { Toast.makeText(this, R.string.bt_supported, Toast.LENGTH_SHORT).show(); finish(); } // ボタンのインスタンスを取得 button = (Button) findViewById(R.id.button); // BTの準備 -------------------------------------------------------------- // BTアダプタのインスタンスを取得 mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); // 相手先BTデバイスのインスタンスを取得 mBtDevice = mBluetoothAdapter.getRemoteDevice("接続先のMACアドレスを記入"); // BTソケットのインスタンスを取得 try { // 接続に使用するプロファイルを指定 mBtSocket = mBtDevice.createRfcommSocketToServiceRecord( UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")); } catch (IOException e) { e.printStackTrace(); } // ソケットを接続する try { mBtSocket.connect(); mOutput = mBtSocket.getOutputStream(); // 出力ストリームオブジェクトを得る } catch (IOException e) { e.printStackTrace(); } } public void btnOnClick(View v) { // TODO Auto-generated method stub try { mOutput.write('a'); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } protected void onDestroy() { super.onDestroy(); // ソケットを閉じる try { mBtSocket.close(); } catch (IOException e) { e.printStackTrace(); } } } ###activity_main.xml <?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:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="jp.ac.oita_u.i1311130.bletest.MainActivity"> <Button android:text="Test" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_centerHorizontal="true" android:id="@+id/button" android:onClick="btnOnClick"/> </RelativeLayout> ###補足情報(言語/FW/ツール等のバージョンなど) http://blog.livedoor.jp/sce_info3-craft/archives/8337697.html
回答2件
あなたの回答
tips
プレビュー