3

私はAndroid用のプログラミングに非常に慣れていません。メインと btmanager の 2 つのクラスがあります。電話でアプリをテストしようとすると、procees が強制終了されたという情報しか得られません。私は何を間違っていますか?

コードの実装:

メインクラス:

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 に答える