Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
蓝牙android强制关闭_Android_Bluetooth - Fatal编程技术网

蓝牙android强制关闭

蓝牙android强制关闭,android,bluetooth,Android,Bluetooth,我正试图从android连接到笔记本电脑上的蓝牙服务器,但我的应用程序强制关闭。我也包括了蓝牙权限。有人能检查一下代码,告诉我我可能做错了什么吗 package com.android.example.blueoga; import java.io.IOException; import java.util.UUID; import android.os.Bundle; import android.app.Activity; import android

我正试图从android连接到笔记本电脑上的蓝牙服务器,但我的应用程序强制关闭。我也包括了蓝牙权限。有人能检查一下代码,告诉我我可能做错了什么吗

   package com.android.example.blueoga;

   import java.io.IOException;
   import java.util.UUID;

   import android.os.Bundle;
   import android.app.Activity;
   import android.bluetooth.BluetoothAdapter;
   import android.bluetooth.BluetoothDevice;
   import android.bluetooth.BluetoothSocket;
   import android.content.Intent;
   import android.view.Menu;
   import android.widget.TextView;

   public class BlueGOA extends Activity {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    TextView view=(TextView)findViewById(R.id.textView1);
    BluetoothSocket socket;
    socket=null;
    String address="64:27:37:D0:1F:48";
    UUID MY_UUID=UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
    int REQUEST_ENABLE_BT=1;
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (mBluetoothAdapter == null) {
        // Device does not support Bluetooth
        view.setText("Go Home Suckers");
    }
    else if(!mBluetoothAdapter.isEnabled()){
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);




    }
    BluetoothDevice device=mBluetoothAdapter.getRemoteDevice(address);
    try{
        socket=device.createRfcommSocketToServiceRecord(MY_UUID);
    }catch(IOException e){
        view.setText("problem in socket sucker");
    }
    try {
        socket.connect();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        view.setText("Problem in connecting sucker");
    }

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.blue_go, menu);
    return true;
    }

   }

我看到的第一件事是,即使套接字分配引发异常,您也在尝试使用socket.connect()

public class BlueGOA extends Activity {

    private final BluetoothSocket mmSocket;
    private final BluetoothDevice mmdevice;
    // Use a temporary object that is later assigned to mmSocket,
    // because mmSocket is final
    mmdevice = device;
    BluetoothSocket tmp = null;
    // Get a BluetoothSocket to connect with the given BluetoothDevice
    try {
         tmp = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
    } catch (IOException e) { }
    mmSocket = tmp;
}