我不知道这是怎么回事,但是我的应用程序正在搜索附近的蓝牙设备,但是当我尝试连接到其中一个蓝牙设备时,该应用程序崩溃了。

请看一看

这是我的 SearchBTDevice.java 。这是调用ConnectBTDevice Activity 的 Activity

package vertex2016.mvjce.edu.bluealert;

import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGattDescriptor;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.bluetooth.BluetoothAdapter;
import android.provider.Settings;
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.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.Set;

public class SearchBTDevice extends AppCompatActivity {

    public BluetoothAdapter BlueAdapter = BluetoothAdapter.getDefaultAdapter();
    public ArrayAdapter PairedArrayAdapter;
    public ArrayAdapter BTArrayAdapter;
    BluetoothDevice btd;

    public ListView devicesFound;


    private final BroadcastReceiver BTReceiver= new BroadcastReceiver(){

       public void onReceive(Context context, Intent intent)
       {
           String action = intent.getAction();

           if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                   btd = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

                   BTArrayAdapter.add(btd.getName() + "\t" + btd.getAddress() + "\n");

               }
           }

    };

    IntentFilter filter1 = new IntentFilter(BluetoothDevice.ACTION_FOUND);




    @Override
    protected void onResume() {
        super.onResume();
        this.registerReceiver(BTReceiver,filter1);



    }

    @Override
    protected void onPause() {
        super.onPause();
        BlueAdapter.cancelDiscovery();
        this.unregisterReceiver(BTReceiver);
        Toast.makeText(SearchBTDevice.this, "Discovery Stopped!!", Toast.LENGTH_SHORT).show();
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_btdevice);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);




       searchBTDevices();


    }


    public void searchBTDevices()
    {
        if(!BlueAdapter.startDiscovery())
            Toast.makeText(SearchBTDevice.this, "Failed to Start Discovery", Toast.LENGTH_SHORT).show();
        else
            Toast.makeText(SearchBTDevice.this, "Discovery Startred", Toast.LENGTH_SHORT).show();
        BTArrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1);
        devicesFound = (ListView)findViewById(R.id.searchpagelistView);
        devicesFound.setAdapter(BTArrayAdapter);
        devicesFound.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent connectedBTintent = new Intent(SearchBTDevice.this, ConnectedBTDevice.class);
                connectedBTintent.putExtra("BluetoothDevice", btd);
                startActivity(connectedBTintent);

            }
        });

    }


}

这是我的,已更新,ConnectBTDevice.java ,它试​​图建立RFCOMM通道。但这是我的应用崩溃的地方。
    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;

                try {
                    tempSocket = btd.createRfcommSocketToServiceRecord(myUUID);
                } catch (IOException e) {
                    e.printStackTrace();
                }

                BluetoothAdapter.getDefaultAdapter().cancelDiscovery();

                try {
                    btSocket.connect();
                    arr.add("CONNECTED TO-->" + btd.getName());
                } catch (IOException e) {
                    e.printStackTrace();
                    try {
                        btSocket.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            }

        };
        myThread.start();
    }




    public void displayStuff()
    {
        lv = (ListView)findViewById(R.id.connectedBTlistView);
        lv.setAdapter(arr);
    }

}

这是我的更新的 logcat 显示的内容
03-22 01:40:47.116 28214-11199/vertex2016.mvjce.edu.bluealert E/AndroidRuntime: FATAL EXCEPTION: Thread-11810
                                                                                Process: vertex2016.mvjce.edu.bluealert, PID: 28214
                                                                                java.lang.NullPointerException: Attempt to invoke virtual method 'void android.bluetooth.BluetoothSocket.connect()' on a null object reference
                                                                                    at vertex2016.mvjce.edu.bluealert.ConnectedBTDevice$1.run(ConnectedBTDevice.java:61)

最佳答案

logcat中的例外是告诉您问题所在。您正在尝试在空对象引用上调用BluetoothSocket.connect()

您正在调用btSocket.connect(),但btSocket尚未初始化,因此为null。

tempSocket.connect()之前,先调用tempSocket或将btSocket分配给connect()

关于android - 尽管尝试了教程,但蓝牙设备未在Android应用上连接,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36098231/

10-14 09:05