3

我对android编程很陌生。我有两个类:main 和 btmanager。当我尝试在手机上测试我的应用程序时,我得到的只是进程被杀死的信息。我究竟做错了什么 ?

代码实现:

主要课程:

public class MainActivity extends Activity {

BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();

Btmanager manager;


@Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (bluetooth == null)
    {
        Toast.makeText(this, "Bluetooth is not enabled on this device", Toast.LENGTH_LONG).show();
        System.exit(0);
    }


}

@Override
public void onStart()
{
    super.onStart();

    if (!bluetooth.isEnabled()) {
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, 2);
    }

    manager.run();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}




public void closeApp (View view)
{
    System.exit(0);
}
}

Btmanager 类:

public class Btmanager extends Thread {

private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
public static final UUID myUUID = UUID.fromString("0x1101");
BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();

public Btmanager(BluetoothDevice device) {
    // Use a temporary object that is later assigned to mmSocket,
    // because mmSocket is final
    BluetoothSocket tmp = null;
    mmDevice = device;

    // Get a BluetoothSocket to connect with the given BluetoothDevice
    try {
        // MY_UUID is the app's UUID string, also used by the server code
        tmp = device.createRfcommSocketToServiceRecord(myUUID);
    } catch (IOException e) { }
    mmSocket = tmp;
}

public void run() {
    // Cancel discovery because it will slow down the connection
    bluetooth.cancelDiscovery();

    try {
        // Connect the device through the socket. This will block
        // until it succeeds or throws an exception
        mmSocket.connect();
    } catch (IOException connectException) {
        // Unable to connect; close the socket and get out
        try {
            mmSocket.close();
        } catch (IOException closeException) { }
        return;
    }

}

/** Will cancel an in-progress connection, and close the socket */
public void cancel() {
    try {
        mmSocket.close();
    } catch (IOException e) { }
}
4

2 回答 2

2

我在您的代码中看到的 2 个问题:

  1. 您不实例化Btmanager对象,它仍然是null当您调用run. (将导致NullPointerException- 你的应用程序将崩溃)。
  2. 您调用run方法而start不是Btmanager. 如果您希望run方法中的代码在新线程中运行,您必须调用start. 调用run将导致它在同一个线程中运行。这会阻塞您的 UI 线程,如果阻塞时间过长,可能会导致您的应用程序崩溃。
于 2012-11-03T15:58:25.763 回答
0

出于测试目的,我不使用 BTmanager 类 - 在 onStart() 方法中,我添加了具有连接实现的新线程,但仍然没有任何结果 - 应用程序崩溃。

public void onStart()
{
    super.onStart();

    if (!bluetooth.isEnabled()) {
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, 2);
    }


    new Thread(new Runnable() 
    {
        public void run() 
        {
            try {
                //Create a Socket connection: need the server's UUID number of registered
                socket = device.createRfcommSocketToServiceRecord(myUUID);

                socket.connect();
                Log.d("EF-BTBee", "Connectted");
            }
            catch (IOException e)
            {
                Log.e("EF-BTBee", "Error : ", e);
            }
        }
    }).start();


}
于 2012-11-04T10:30:27.477 回答