下記のサイトのようなことをandroid端末とmacbookでしてみたいと思って下にあるようなソースコードを作ったのですが、androidで実行したところ
mBtDevice = mBluetoothAdapter.getRemoteDevice("MACアドレス");
のところで下にあるようなエラーメッセージがでて、アプリが終了してしまいます。
実機だと以下のメッセージがでます。
I/Process: Sending signal. PID: 13978 SIG: 9
Application terminated.
MACアドレスの部分はちゃんと自分のpcのを調べて入力しています。
どういった原因が考えられるのでしょうか??
よろしくお願いします。
http://blog.livedoor.jp/sce_info3-craft/archives/8337697.html
###発生している問題・エラーメッセージ
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.aaa.bt1, PID: 5222 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.aaa.bt1/com.example.aaa.bt1.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.bluetooth.BluetoothDevice android.bluetooth.BluetoothAdapter.getRemoteDevice(java.lang.String)' on a null object reference
###該当のソースコード
package com.example.aaa.bt1; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothSocket; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; 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 btn1; // 送信ボタン public MainActivity() { } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // ボタンのインスタンスを取得 btn1 = (Button) findViewById(R.id.button1); // 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(); } btn1.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub try { mOutput.write('a'); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); } @Override protected void onDestroy() { super.onDestroy(); // ソケットを閉じる try { mBtSocket.close(); } catch (IOException e) { e.printStackTrace(); } } }
###試したこと
課題に対してアプローチしたことを記載してください
###補足情報(言語/FW/ツール等のバージョンなど)
より詳細な情報
回答3件
あなたの回答
tips
プレビュー