2013-02-20 152 views
0

我试图建立我的手机和蓝牙设备之间的蓝牙连接,但应用程序不断崩溃。通过评论,我发现错误在openBT()函数中。任何人都可以帮我吗?尝试连接到蓝牙时,应用程序崩溃android

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) 
+0

您是否拥有蓝牙许可? – Blackbelt 2013-02-20 13:05:36

+1

可以粘贴logcat日志 – RDX 2013-02-20 13:07:27

+0

是的,我确实设置了蓝牙许可。 – 2013-02-20 13:10:12

回答

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; 
     } 
    } 
} 
+0

我的设备不是空的但崩溃, – 2015-10-28 10:02:40

相关问题