我的应用程序有一个MainActivity.java,它初始化2个按钮(btnBTConnect和btnBTStop)并为它们调用setOnClickListener方法。
在BluetoothActivity.java中,我具有调用该方法的onClick方法。对于btnBTConnect,它调用我的方法来找到蓝牙设备(findBT),连接到它并接收inputStream(openBT)。对于btnBTStop,它调用我的方法来关闭连接(closeBT)。
当我单击“启动蓝牙”按钮时,没有问题。建立连接并按预期接收数据(BluetoothSocket和InputStream不为null)。
但是,当我单击蓝牙“停止”按钮以停止数据并关闭InputStream和Socket时,出现空对象引用错误。
有人可以帮忙吗?是因为我有onClick设置吗?通过使用switch和case,当它切换到蓝牙停止状态时,是否从findBT和openBT方法中删除了所有变量?这就是为什么当我尝试关闭它们时,我的Bluetooth套接字和InputStream为空吗?
我该如何解决?
谢谢
MainAcitivity
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
public Button btnBTConnect;
public Button btnBTStop;
BluetoothAdapter mBTAdap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBTAdap = BluetoothAdapter.getDefaultAdapter();
btnBTConnect = findViewById(R.id.btnBTConnect);
btnBTStop = findViewById(R.id.btnBTStop);
btnBTConnect.setOnClickListener(new BluetoothActivity(getApplicationContext()));
btnBTStop.setOnClickListener(new BluetoothActivity(getApplicationContext()));
}
BluetoothActivity
public class BluetoothActivity implements View.OnClickListener{
private static final String TAG = "BluetoothActivity";
Context mContext;
public BluetoothAdapter mBTAdap;
public BluetoothSocket mBTSock;
public BluetoothDevice mBTDev;
public InputStream mBTIP;
public Thread mBTThread;
byte[] mBuffer;
volatile boolean mStopThread;
public BluetoothActivity(Context myContext) {
this.mBTAdap = BluetoothAdapter.getDefaultAdapter();
this.mContext = myContext;
}
@Override
public void onClick(View mView) {
switch (mView.getId()){
case R.id.btnBTConnect:
try {
findBT();
openBT();
} catch (IOException e) {Log.e(TAG, "onClick: " + e.getMessage(), e);}
break;
case R.id.btnBTStop:
try {
closeBT();
} catch (IOException e) {Log.e(TAG, "onClick: " + e.getMessage(), e);}
break;
default:
break;
}
}
public void findBT() {
Set<BluetoothDevice> mBTPairedDevices = mBTAdap.getBondedDevices();
if (mBTPairedDevices.size() > 0) {
for (BluetoothDevice device : mBTPairedDevices) {
if (device.getName().equals("myDevice")) {
mBTDev = device;
toastMessage(mBTDev.getName() + " device found");
break;
} else {toastMessage("No device found");}
}
} else {toastMessage("No Devices paired");}
}
public void openBT() throws IOException
{
UUID mUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
try {
mBTSock = mBTDev.createRfcommSocketToServiceRecord(mUUID);
} catch (IOException e) {
Log.e(TAG, "openBT: " + e.getMessage(), e); toastMessage("Couldn't create RFComm socket");}
mBTSock.connect();
mBTIP = mBTSock.getInputStream();
listenBT();
}
public void listenBT(){
mStopThread = false;
mBuffer = new byte[6];
mBTThread = new Thread(new Runnable() {
@Override
public void run() {
while (!Thread.currentThread().isInterrupted() && !mStopThread) {
try {
bytes = mBTIP.read(mBuffer);
} catch (IOException e) {
mStopThread = true;
Log.e(TAG, "run: " + e.getMessage(), e);
}
}
}
}); mBTThread.start();
}
public void closeBT() throws IOException {
mStopThread = true;
mBTIP.close();
mBTSock.close();
toastMessage("BT Closed");
Log.d(TAG, "closeBT: BT Closed");
}
private void toastMessage(String message){
Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();
}
}
Logcat
02-06 19:54:41.256 3915-3915/com.example.mark.btconnflow E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mark.btconnflow, PID: 3915
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.bluetooth.BluetoothSocket.close()' on a null object reference
at com.example.mark.btconnflow.BluetoothActivity.closeBT(BluetoothActivity.java:202)
at com.example.mark.btconnflow.BluetoothActivity.onClick(BluetoothActivity.java:79)
at android.view.View.performClick(View.java:6261)
at android.widget.TextView.performClick(TextView.java:11159)
at android.view.View$PerformClick.run(View.java:23751)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
答案 0 :(得分:2)
您制作两个不同的对象,并将它们放在侦听器中。尝试:
BluetoothActivity listener = new BluetoothActivity(getApplicationContext());
btnBTConnect.setOnClickListener(listener);
btnBTStop.setOnClickListener(listener);