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

今日长缨在手,何时缚住苍龙。这篇文章主要讲述当尝试连接到蓝牙android时,应用程序崩溃相关的知识,希望能为你提供帮助。
我正在尝试在我的手机和蓝牙设备之间建立蓝牙连接,但应用程序一直在崩溃。通过评论,我发现错误在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(); } }

【当尝试连接到蓝牙android时,应用程序崩溃】这是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):atcom.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):atandroid.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):atcom.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)

答案你确定你有mmDevice不是null吗?你确定你在findBT()中设置了mmDevice吗?
Set< BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); if(pairedDevices.size() > 0) { for(BluetoothDevice device : pairedDevices) { if(device.getName().equals("Hauwa")) { mmDevice = device; break; } } }

另一答案
Set< BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); String tmpS; String targetS = "Hauwa"; if(pairedDevices.size() > 0) { for(BluetoothDevice device : pairedDevices) { tmpS = device.getName() + ""; tmpS = tmpS.substring(0,targetS.length()); if(tmpS.equals(targetS)) { mmDevice = device; break; } }

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

    推荐阅读