状況
非常に単純なタスクを 1 つ実行するアプリケーションを作成しようとしています。NFCタグが適切な AAR と URI で検出されると、アプリケーションが起動し、タグ内のデータを使用して、指定された Bluetooth ラジオ (NFC タグによって指定されます) に接続します。 )、データ ストリームの受信を待機します。データ ストリームは文字列に変換され、画面に表示されます。
現在アプリケーションを実行すると、3 つの実行時例外が発生してすぐに終了します。閉じるのではなく、受動的に待機させたいのですが、実行時例外がわかりません。
例外
- 活動を再開できません。NullPointerException
- パースタグ(); 例外行 127
- onResume(); 例外行 83
コード
public class BlueNF extends Activity {
private BluetoothDevice mBluetoothDevice = null;
private BluetoothAdapter mBluetoothAdapter = null;
private BluetoothSocket mBluetoothSocket = null;
private NfcAdapter mNfcAdapter = null;
private NdefMessage dashTag = null;
private static final int REQUEST_ENABLE_BT = 3;
private static final UUID SerialPortServiceClass_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
private InputStream iStream = null;
public static String iValue = null;
public final static String EXTRA_MESSAGE = "com.yogafarts.hackpsuproject.BlueNF.iValue";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Set the content view
setContentView(R.layout.activity_blue_nf);
//Check for NFC
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (mNfcAdapter == null) {
Toast.makeText(this, "NFC is not available", Toast.LENGTH_LONG).show();
finish();
return;
}
//Check for Bluetooth
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
finish();
return;
}
}
@Override
public void onStart() {
super.onStart();
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
public void onResume() {
super.onResume();
//IntentFilter ndefTag = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
//Get the current intent
Intent dashTagIntent = getIntent();
//Read extra intent data into tag object (Really just reading the NdefMessage into the dashTag object
dashTag = dashTagIntent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
//Create a byte array of bluetooth hardware ID using ParseTag
byte[] mBluetoothInfo = ParseTag(dashTag);
//Create a bluetooth adapter object
mBluetoothDevice = mBluetoothAdapter.getRemoteDevice(mBluetoothInfo);//Should be 0013EF00061A
try {
mBluetoothSocket = mBluetoothDevice.createRfcommSocketToServiceRecord(SerialPortServiceClass_UUID);
}
catch ( IOException e ) {
Log.e( "Bluetooth Socket", "Bluetooth not available, or insufficient permissions" );
}
//Cancel discovery because it's resource intensive (if it's active)
mBluetoothAdapter.cancelDiscovery();
//Connect to the device
try {
mBluetoothSocket.connect();
iStream = mBluetoothSocket.getInputStream();
//System.out.println(iStream);
//OutputStream oStream = mBluetoothSocket.getOutputStream();
}
catch ( IOException e ) {
try { mBluetoothSocket.close();
}
catch ( IOException e2 ) {
Log.e( "Bluetooth Socket", "IO Exception" );
}
}
iValue = iStream.toString();
viewMessage();
}
public byte[] ParseTag(NdefMessage ndef) {
NdefRecord[] mNdef = ndef.getRecords();
return mNdef[2].getPayload();
}
public void viewMessage() {
Intent intent = new Intent(this, DisplayMessage.class);
//TextView text = (TextView) findViewById(R.id.tv);
String message = iValue;//text.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.blue_n, menu);
return true;
}
}
そして、ストリームが読み取られた後にテキストを表示するアクティビティ...
public class DisplayMessage extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
TextView text = (TextView) findViewById(R.id.tv);
text.setText(com.yogafarts.hackpsuproject.BlueNF.iValue);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.display_message, menu);
return true;
}
}