java - 尝试关闭蓝牙套接字时变为空(错误:空对象引用)
问题描述
我的应用程序有一个 MainActivity.java,它初始化 2 个按钮(btnBTConnect 和 btnBTStop)并为它们调用 setOnClickListener 方法。
在 BluetoothActivity.java 中,我有调用方法的 onClick 方法。对于 btnBTConnect,它调用我的方法来查找蓝牙设备 (findBT),连接到它并接收 inputStream (openBT)。对于btnBTStop,它调用我的方法来关闭连接(closeBT)。
当我点击开始蓝牙按钮时,我没有问题。建立连接并按预期接收数据(BluetoothSocket 和 InputStream 不为空)。
但是,当我单击蓝牙停止按钮以停止数据并关闭 InputStream 和 Socket 时,我得到一个空对象引用错误。
有人可以帮忙吗?是因为我如何设置 onClick 吗?通过使用 switch 和 case,当它切换到蓝牙停止 case 时,我的 findBT 和 openBT 方法中的所有变量是否都被删除了?这就是为什么当我尝试关闭它们时我的蓝牙套接字和 InputStream 为空的原因吗?
我怎样才能解决这个问题?
谢谢
主要活动
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()));
}
蓝牙活动
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();
}
}
日志猫
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)
解决方案
您制作了两个不同的对象并将它们放入侦听器中。尝试:
BluetoothActivity listener = new BluetoothActivity(getApplicationContext());
btnBTConnect.setOnClickListener(listener);
btnBTStop.setOnClickListener(listener);
推荐阅读
- python - 如何使用 Yield 和 Range 从列表中获取位置和项目?
- python - 如何在单元测试中使用 argparse 参数调用函数?
- python - Python - 三角形和自恋数字函数
- angular - NGRX 多店功能交流
- reactjs - 我想使用 axios 在 React 上使用 id 删除前端的用户帐户
- javascript - 如何访问和循环 React 中的嵌套 JSON 项?
- android - 为什么我的
从导航菜单返回片段后字段为空? - checkbox - QToolbar中的复选框
- javascript - javascript中chainig函数时如何避免冗余?
- php - 显示自定义 Gutenberg 块的预览图像