我目前正在开发一个项目,我正在构建一个使用蓝牙向我的Arduino Uno发送命令的应用程序,除了实际发送数据外,一切都运行良好。
每当我尝试发送数据时,它会抛出一个空指针异常,而我所知道的这些异常是当某些内容未初始化或无法找到时抛出它们(如果我错了,请纠正我)。所以我认为问题是我的connectedThread没有正确初始化但我不知道该做什么以及做什么。
还有一些其他代码用于语音到文本,所以请忽略它。
我真的很感激任何帮助或建议。
非常感谢你!
我的Main_Activity.java:
UserControl
}
我的activity_main.xml:
ResourceDictionary
我的logcat:
package marggraffd.developer.com.robot;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.ActivityNotFoundException;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.speech.RecognizerIntent;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Locale;
import java.util.Set;
import java.util.UUID;
public class MainActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemClickListener {
public static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
private static final int SUCCESS_CONNECT = 0;
private static final int MESSAGE_READ = 1;
private static final String TAG = null;
Toolbar toolbar;
FloatingActionButton fab;
Button forward, reverse, left, right, stop;
private final int REQ_CODE_SPEECH_INPUT = 100;
TextView speech;
String text;
ArrayAdapter<String> listAdapter;
ListView listView;
ArrayList<String> pairedDevices;
ArrayList<BluetoothDevice> devices;
private BluetoothAdapter btAdapter;
Set<BluetoothDevice> devicesArray;
IntentFilter filter;
BroadcastReceiver receiver;
ConnectedThread connectedThread = null;
Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case SUCCESS_CONNECT:
ConnectedThread connectedThread = new ConnectedThread((BluetoothSocket) msg.obj);
Toast.makeText(getApplicationContext(), getResources().getString(R.string.bluetooth_success), Toast.LENGTH_LONG).show();
String s = "Successfully Connected";
connectedThread.write(s);
break;
case MESSAGE_READ:
byte[] readBuff = (byte[]) msg.obj;
String string = new String(readBuff);
Toast.makeText(getApplicationContext(), string, Toast.LENGTH_LONG).show();
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
init();
btAdapter = BluetoothAdapter.getDefaultAdapter();
if (btAdapter == null) {
Toast.makeText(getApplicationContext(), getResources().getString(R.string.error2), Toast.LENGTH_LONG).show();
} else {
if (!btAdapter.isEnabled()) {
turnOnBT();
}
getPairedDevices();
startDiscovery();
}
}
private void startDiscovery() {
btAdapter.cancelDiscovery();
btAdapter.startDiscovery();
}
private void turnOnBT() {
Intent intent = new Intent((BluetoothAdapter.ACTION_REQUEST_ENABLE));
startActivityForResult(intent, 1);
}
private void getPairedDevices() {
devicesArray = btAdapter.getBondedDevices();
if (devicesArray.size() > 0) {
for (BluetoothDevice device : devicesArray) {
pairedDevices.add(device.getName());
}
}
}
private void init() {
fab = (FloatingActionButton) findViewById(R.id.fab);
forward = (Button) findViewById(R.id.bForward);
reverse = (Button) findViewById(R.id.bReverse);
left = (Button) findViewById(R.id.bLeft);
right = (Button) findViewById(R.id.bRight);
stop = (Button) findViewById(R.id.bStop);
speech = (TextView) findViewById(R.id.textView3);
listView = (ListView) findViewById(R.id.listView);
listView.setOnItemClickListener(this);
listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, 0);
listView.setAdapter(listAdapter);
pairedDevices = new ArrayList<String>();
filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
devices = new ArrayList<BluetoothDevice>();
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
devices.add(device);
String s = "";
for (int a = 0; a < pairedDevices.size(); a++) {
if (device.getName().equals(pairedDevices.get(a))) {
s = "(Paired)";
break;
}
}
listAdapter.add(device.getName() + " " + s + " " + "\n" + device.getAddress());
} else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
} else if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
if (btAdapter.getState() == btAdapter.STATE_OFF) {
turnOnBT();
}
}
}
};
registerReceiver(receiver, filter);
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
registerReceiver(receiver, filter);
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(receiver, filter);
filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(receiver, filter);
fab.setOnClickListener(this);
stop.setOnClickListener(this);
buttons();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bStop:
connectedThread.write("e");
break;
case R.id.fab:
speech();
break;
}
}
private void speech() {
Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
i.putExtra(RecognizerIntent.EXTRA_PROMPT, getString(R.string.speech_prompt));
try {
startActivityForResult(i, REQ_CODE_SPEECH_INPUT);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(), getString(R.string.error), Toast.LENGTH_LONG).show();
startActivityForResult(i, REQ_CODE_SPEECH_INPUT);
}
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(receiver);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQ_CODE_SPEECH_INPUT && resultCode == RESULT_OK) {
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
speech.setText(result.get(0));
text = speech.getText().toString();
if (text == getString(R.string.forward)) {
Toast.makeText(getApplicationContext(), "Forward", Toast.LENGTH_LONG).show();
} else if (text == getString(R.string.reverse)) {
Toast.makeText(getApplicationContext(), "Reverse", Toast.LENGTH_LONG).show();
} else if (text == getString(R.string.left)) {
Toast.makeText(getApplicationContext(), "Left", Toast.LENGTH_LONG).show();
} else if (text == getString(R.string.right)) {
Toast.makeText(getApplicationContext(), "Right", Toast.LENGTH_LONG).show();
} else if (text == getString(R.string.stop)) {
Toast.makeText(getApplicationContext(), "Stop", Toast.LENGTH_LONG).show();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (btAdapter.isDiscovering()) {
btAdapter.cancelDiscovery();
}
if (listAdapter.getItem(position).contains("(Paired)")) {
BluetoothDevice selectedDevices = devices.get(position);
ConnectThread connect = new ConnectThread(selectedDevices);
connect.start();
} else {
Toast.makeText(getApplicationContext(), getResources().getString(R.string.bluetooth_unpaired), Toast.LENGTH_LONG).show();
}
}
private class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
public ConnectThread(BluetoothDevice device) {
BluetoothSocket tmp = null;
mmDevice = device;
try {
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
}
mmSocket = tmp;
}
public void run() {
btAdapter.cancelDiscovery();
try {
mmSocket.connect();
} catch (IOException connectException) {
try {
mmSocket.close();
} catch (IOException closeException) {
}
return;
}
mHandler.obtainMessage(SUCCESS_CONNECT, mmSocket).sendToTarget();
}
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
}
}
}
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket) {
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
byte[] buffer = new byte[9600];
int bytes;
while (true) {
try {
buffer = new byte[9600];
bytes = mmInStream.read(buffer);
mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
break;
}
}
}
public void write(String message) {
byte[] msgBuffer = message.getBytes();
try {
mmOutStream.write(msgBuffer);
} catch (IOException e) {
e.printStackTrace();
}
}
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
}
}
}
private void buttons() {
forward.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
try {
connectedThread.write("e");
} catch (Exception e) {
e.printStackTrace();
}
Toast.makeText(getApplicationContext(), "Forward", Toast.LENGTH_SHORT).show();
break;
case MotionEvent.ACTION_UP:
connectedThread.write("e");
Toast.makeText(getApplicationContext(), "Stop", Toast.LENGTH_SHORT).show();
break;
}
return false;
}
});
reverse.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
connectedThread.write("d");
Toast.makeText(getApplicationContext(), "Reverse", Toast.LENGTH_SHORT).show();
break;
case MotionEvent.ACTION_UP:
connectedThread.write("e");
Toast.makeText(getApplicationContext(), "Stop", Toast.LENGTH_SHORT).show();
break;
}
return false;
}
});
left.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
connectedThread.write("b");
Toast.makeText(getApplicationContext(), "Left", Toast.LENGTH_SHORT).show();
break;
case MotionEvent.ACTION_UP:
connectedThread.write("e");
Toast.makeText(getApplicationContext(), "Stop", Toast.LENGTH_SHORT).show();
break;
}
return false;
}
});
right.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
connectedThread.write("c");
Toast.makeText(getApplicationContext(), "Right", Toast.LENGTH_SHORT).show();
break;
case MotionEvent.ACTION_UP:
connectedThread.write("e");
Toast.makeText(getApplicationContext(), "Stop", Toast.LENGTH_SHORT).show();
break;
}
return false;
}
});
}
答案 0 :(得分:2)
非常感谢你的帮助。我已经解决了这个问题。我刚刚在ConnectThread类的run()末尾初始化了线程,现在它运行得很好。
如果其他人遇到同样的问题,这就必须修复:
private class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
public ConnectThread(BluetoothDevice device) {
BluetoothSocket tmp = null;
mmDevice = device;
try {
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
}
mmSocket = tmp;
}
public void run() {
btAdapter.cancelDiscovery();
try {
mmSocket.connect();
} catch (IOException connectException) {
try {
mmSocket.close();
} catch (IOException closeException) {
}
return;
}
mHandler.obtainMessage(SUCCESS_CONNECT, mmSocket).sendToTarget();
connectedThread = new ConnectedThread(mmSocket);
connectedThread.start();
}
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
}
}
}
答案 1 :(得分:0)
基于您LogCat
错误消息:
java.lang.NullPointerException
10-01 18:32:00.481 21197-21197/marggraffd.developer.com.robot W/System.err: at marggraffd.developer.com.robot.MainActivity$3.onTouch(MainActivity.java:429)
错误就在这一行:
connectedThread.write("e");
变量connectedThread
仍然具有null
的值,请检查您的代码是否正确初始化ConnectedThread
!
...
...
...
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
try {
connectedThread.write("e"); //*** connectedThread is NULL
} catch (Exception e) {
e.printStackTrace();
}
Toast.makeText(getApplicationContext(), "Forward", Toast.LENGTH_SHORT).show();
...
...
答案 2 :(得分:0)
您定义为类字段:
ConnectedThread connectedThread = null;
然后你有Handler
处理A CONNECT_SUCCESS
并为本地变量指定一个新的ConnectedThread
,不类字段:
ConnectedThread connectedThread = new ConnectedThread((BluetoothSocket) msg.obj);
我在代码中没有看到的是您实际创建ConnectedThread
并启动它的地方,更不用说将其分配给您的类字段,而您经常使用write
个字段
因此,在您的onTouch
方法中引发了NullPointerException
,因为没有connectedThread
。