我正在尝试开发这款发现的应用程序&连接到附近的蓝牙设备。我遇到了我的应用程序崩溃的NULLPOINTEREXCEPTION。弄清楚这是因为我的UUID没有被赋予任何价值。现在,我是Android开发和推广的新手。我不知道将哪个值分配给我的UUID,以免它崩溃。
以下是处理与其他蓝牙设备连接的 ConnectedBTDevice.java 。
package vertex2016.mvjce.edu.bluealert;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.io.IOException;
import java.util.UUID;
public class ConnectedBTDevice extends AppCompatActivity {
public BluetoothDevice btd;
public BluetoothSocket btSocket, tempSocket;
private UUID myUUID;
ArrayAdapter arr;
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_connected_btdevice);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
arr = new ArrayAdapter(this, android.R.layout.simple_list_item_2);
btd = getIntent().getParcelableExtra("BluetoothDevice");
connectBT();
displayStuff();
}
public void connectBT() {
Thread myThread = new Thread() {
public void run() {
tempSocket = null;
btSocket = null;
try {
tempSocket = btd.createRfcommSocketToServiceRecord(myUUID);
} catch (IOException e) {
e.printStackTrace();
}
BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
try {
tempSocket.connect();
arr.add("CONNECTED TO-->" + btd.getName());
} catch (IOException e) {
e.printStackTrace();
try {
tempSocket.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
};
myThread.start();
}
public void displayStuff()
{
lv = (ListView)findViewById(R.id.connectedBTlistView);
lv.setAdapter(arr);
}
}
这是 logcat ,显示错误
03-24 00:32:11.254 5772-5772/vertex2016.mvjce.edu.bluealert I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@17abdde2 time:66442377
03-24 00:32:11.594 5772-5772/vertex2016.mvjce.edu.bluealert E/ActivityThread: Performing stop of activity that is not resumed: {vertex2016.mvjce.edu.bluealert/vertex2016.mvjce.edu.bluealert.MainActivity}
java.lang.RuntimeException: Performing stop of activity that is not resumed: {vertex2016.mvjce.edu.bluealert/vertex2016.mvjce.edu.bluealert.MainActivity}
at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3377)
at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3458)
at android.app.ActivityThread.access$1200(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1350)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
03-24 00:32:16.677 5772-5772/vertex2016.mvjce.edu.bluealert I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@13b7f542 time:66447800
03-24 00:32:17.811 5772-8519/vertex2016.mvjce.edu.bluealert I/Timeline: Timeline: Activity_launch_request id:vertex2016.mvjce.edu.bluealert time:66448935
03-24 00:32:18.420 5772-5772/vertex2016.mvjce.edu.bluealert I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@313c32f2 time:66449543
03-24 00:32:24.308 5772-5772/vertex2016.mvjce.edu.bluealert I/Timeline: Timeline: Activity_launch_request id:vertex2016.mvjce.edu.bluealert time:66455431
03-24 00:32:24.593 5772-9062/vertex2016.mvjce.edu.bluealert W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
03-24 00:32:24.611 5772-9062/vertex2016.mvjce.edu.bluealert E/AndroidRuntime: FATAL EXCEPTION: Thread-39196
Process: vertex2016.mvjce.edu.bluealert, PID: 5772
java.lang.NullPointerException: Attempt to invoke virtual method 'long java.util.UUID.getMostSignificantBits()' on a null object reference
at android.os.ParcelUuid.writeToParcel(ParcelUuid.java:129)
at android.bluetooth.IBluetooth$Stub$Proxy.connectSocket(IBluetooth.java:1767)
at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:309)
at vertex2016.mvjce.edu.bluealert.ConnectedBTDevice$1.run(ConnectedBTDevice.java:63)
03-24 00:32:25.172 5772-5772/vertex2016.mvjce.edu.bluealert I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@13b7f542 time:66456295
03-24 00:32:25.945 5772-9062/vertex2016.mvjce.edu.bluealert I/Process: Sending signal. PID: 5772 SIG: 9
我的问题是:我分配给myUUID的值是什么,以便它连接到我想要的任何设备?对不起,如果这个问题听起来很愚蠢,但我真的想清楚地了解UUID扮演的角色和角色。它是如何使用的(如分配的值)。
我浏览了Android: How do bluetooth UUIDs work? 这样的帖子,但似乎没有回答主要问题。我分配给我的UUID有什么价值?
感谢您的时间!!
答案 0 :(得分:0)