【问题标题】:How to know what UUID my app needs?如何知道我的应用需要什么 UUID?
【发布时间】:2016-07-11 07:12:58
【问题描述】:

我正在尝试开发这个应用程序来发现并连接到附近的蓝牙设备。我遇到了一个 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 分配什么值?

感谢您的宝贵时间!!

【问题讨论】:

标签: android exception-handling uuid android-bluetooth crash


【解决方案1】:

您要连接的设备应该有一个 UUID,并且您需要发现它。

这是发现蓝牙设备和服务的example app(不是我的)的链接。

作者使用BluetoothDevice.getUuids()函数。

【讨论】:

    猜你喜欢
    • 2012-02-08
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 2012-05-14
    • 1970-01-01
    • 2010-12-13
    • 2011-11-27
    • 2010-09-16
    相关资源
    最近更新 更多