我正在尝试编写一个执行一项非常简单任务的应用程序:当使用适当的AAR和URI检测到NFC标记时,它会启动应用程序,使用标记中的数据连接到指定的蓝牙无线电(由NFC标签指定)并等待接受数据流,然后将其转换为字符串并显示在屏幕上。
当我运行应用程序时,它只是立即退出,有三个运行时异常。我希望它被动地等待而不是关闭,我不理解运行时异常。
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;
}
}