当尝试连接到蓝牙android时,应用程序崩溃

时间:2013-02-20 13:04:17

标签: java android eclipse bluetooth

我正在尝试在手机和蓝牙设备之间建立蓝牙连接,但应用程序一直在崩溃。通过评论,我发现错误在openBT()函数中。有人可以帮帮我吗?

 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.util.Set;
 import java.util.UUID;
 import android.app.Activity;
 import android.bluetooth.BluetoothAdapter;
 import android.bluetooth.BluetoothDevice;
 import android.bluetooth.BluetoothSocket;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.Button;
 import android.widget.TextView;

 public class PageOne extends Activity {

TextView myLabel;
TextView deviceFound;
Button openButton,closeButton;
BluetoothAdapter mBluetoothAdapter;
BluetoothDevice mmDevice;
BluetoothSocket mmSocket;
OutputStream mmOutputStream;
InputStream mmInputStream;


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.pageone);
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    setUp();
    openButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            if (openButton.getText().equals("Enable")) {
                findBT();
            }

            if(openButton.getText().equals("Start Connection")){
                System.out.println("here");

                try{
                    openBT();
                }catch (IOException e){e.printStackTrace();};
            }
        }
    });

    closeButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            closeBT();
            System.out.println("here too");
        }
    });

}
private void setUp() {
    openButton = (Button) findViewById(R.id.button1);
    myLabel = (TextView) findViewById(R.id.textView1);
    closeButton = (Button) findViewById(R.id.button2);
    deviceFound = (TextView) findViewById(R.id.textView2);
    setButtonText();



    BroadcastReceiver receiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            setButtonText();

        }
    };

    IntentFilter filter = new IntentFilter (BluetoothAdapter.ACTION_STATE_CHANGED);
    registerReceiver (receiver, filter);
}   

private void setButtonText() {

    closeButton.setText("Disable bluetooth");
    if (mBluetoothAdapter.isEnabled()) {
        openButton.setText("Start Connection");
        myLabel.setText("Bluetooth is enabled");
    } else {
        openButton.setText("Enable");
        myLabel.setText("Bluetooth is disabled");
    }

}

private void findBT(){

    if(mBluetoothAdapter == null)
    {
        myLabel.setText("No bluetooth adapter available");
    }

    if (!mBluetoothAdapter.isEnabled()) {
        Intent discoverableIntent = new
        Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
        discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
        startActivity(discoverableIntent);

    } 

    Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
    if(pairedDevices.size() > 0)
    {
        for(BluetoothDevice device : pairedDevices)
        {
            if(device.getName().equals("Hauwa")) 
            {
                mmDevice = device;
                break;
            }
        }
    }
    deviceFound.setText("Bluetooth Device Found");


}

private void closeBT(){
    if (mBluetoothAdapter.isEnabled()) {
        mBluetoothAdapter.disable();
        deviceFound.setText("bvnbvnvb");
    }
}

void openBT() throws IOException{

    final UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); 
    mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);        
    mmSocket.connect();
    mmOutputStream = mmSocket.getOutputStream();
    mmInputStream = mmSocket.getInputStream();


}



 }

这是logcat错误

     01-07 02:55:23.189: W/dalvikvm(11382): threadid=1: thread exiting with uncaught exception (group=0x401f0560)
     01-07 02:55:23.189: E/AndroidRuntime(11382): FATAL EXCEPTION: main
     01-07 02:55:23.189: E/AndroidRuntime(11382): java.lang.NullPointerException
     01-07 02:55:23.189: E/AndroidRuntime(11382):   at com.example.BluetoothExample.PageOne.openBT(PageOne.java:147)
     01-07 02:55:23.189: E/AndroidRuntime(11382):   at       com.example.BluetoothExample.PageOne$1.onClick(PageOne.java:51)
     01-07 02:55:23.189: E/AndroidRuntime(11382):   at android.view.View.performClick(View.java:2579)
     01-07 02:55:23.189: E/AndroidRuntime(11382):   at         android.view.View$PerformClick.run(View.java:9246)
     01-07 02:55:23.189: E/AndroidRuntime(11382):   at android.os.Handler.handleCallback(Handler.java:587)
     01-07 02:55:23.189: E/AndroidRuntime(11382):   at android.os.Handler.dispatchMessage(Handler.java:92)
     01-07 02:55:23.189: E/AndroidRuntime(11382):   at android.os.Looper.loop(Looper.java:130)
     01-07 02:55:23.189: E/AndroidRuntime(11382):   at android.app.ActivityThread.main(ActivityThread.java:3735)
     01-07 02:55:23.189: E/AndroidRuntime(11382):   at java.lang.reflect.Method.invokeNative(Native Method)
     01-07 02:55:23.189: E/AndroidRuntime(11382):   at java.lang.reflect.Method.invoke(Method.java:507)
     01-07 02:55:23.189: E/AndroidRuntime(11382):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
     01-07 02:55:23.189: E/AndroidRuntime(11382):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:662)
     01-07 02:55:23.189: E/AndroidRuntime(11382):   at dalvik.system.NativeStart.main(Native Method)
     01-07 02:55:23.199: E/AndroidRuntime(11382): [Blue Error Handler] Make Debugging Report file for main
     01-07 02:55:23.199: E/AndroidRuntime(11382): java.lang.NullPointerException
     01-07 02:55:23.199: E/AndroidRuntime(11382):   at com.example.BluetoothExample.PageOne.openBT(PageOne.java:147)
     01-07 02:55:23.199: E/AndroidRuntime(11382):   at       com.example.BluetoothExample.PageOne$1.onClick(PageOne.java:51)
     01-07 02:55:23.199: E/AndroidRuntime(11382):   at android.view.View.performClick(View.java:2579)
     01-07 02:55:23.199: E/AndroidRuntime(11382):   at android.view.View$PerformClick.run(View.java:9246)
     01-07 02:55:23.199: E/AndroidRuntime(11382):   at android.os.Handler.handleCallback(Handler.java:587)
     01-07 02:55:23.199: E/AndroidRuntime(11382):   at android.os.Handler.dispatchMessage(Handler.java:92)
     01-07 02:55:23.199: E/AndroidRuntime(11382):   at android.os.Looper.loop(Looper.java:130)
     01-07 02:55:23.199: E/AndroidRuntime(11382):   at android.app.ActivityThread.main(ActivityThread.java:3735)
     01-07 02:55:23.199: E/AndroidRuntime(11382):   at java.lang.reflect.Method.invokeNative(Native Method)
     01-07 02:55:23.199: E/AndroidRuntime(11382):   at java.lang.reflect.Method.invoke(Method.java:507)
     01-07 02:55:23.199: E/AndroidRuntime(11382):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
     01-07 02:55:23.199: E/AndroidRuntime(11382):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:662)
     01-07 02:55:23.199: E/AndroidRuntime(11382):   at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

你确定mmDevice不是空的吗?您确定在findBT()中设置了mmDevice吗?

Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if(pairedDevices.size() > 0)
{
    for(BluetoothDevice device : pairedDevices)
    {
        if(device.getName().equals("Hauwa")) 
        {
            mmDevice = device;
            break;
        }
    }
}

答案 1 :(得分:0)

<div class="container header-buffer">...</div>

您需要防止设备名称为空。即使您自己的设备名称不为null,代码也可以读取附近的其他BLE设备为null。您还需要防止子字符串和equals函数溢出