1

我试图实现 android 示例中给出的蓝牙聊天示例。我在运行 Android 4.1 版本的果冻豆设备上运行它。

应用程序只是强制关闭。启动器活动的一部分启动器活动代码是这样的。

当我评论“mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();”这一行时 该应用程序不会强制关闭。并且所有的吐司消息也会显示出来,这些消息不是未注释的行

    private BluetoothAdapter mBluetoothAdapter = null;
// Member object for the chat services
private BluetoothChatService mChatService = null;


@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

     Toast.makeText(getApplicationContext(),"BluetothChat oncreate()", Toast.LENGTH_LONG).show();
    if(D) Log.e(TAG, "+++ ON CREATE +++");

    // Set up the window layout
    setContentView(R.layout.main);

    // Get local Bluetooth adapter
  try {     

    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

   //}
    // If the adapter is null, then Bluetooth is not supported
    if (mBluetoothAdapter == null) {
        Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
        finish();
        return;
    }     
 } catch (Exception e) {
       Toast.makeText(this,e.toString(), Toast.LENGTH_LONG).show(); 

 }
}

@Override
public void onStart() {



    Toast.makeText(getApplicationContext(),"BluetothChat onstart()", Toast.LENGTH_LONG).show();
    super.onStart();
    if(D) Log.e(TAG, "++ ON START ++");

    // If BT is not on, request that it be enabled.
    // setupChat() will then be called during onActivityResult
    try {
        if (!mBluetoothAdapter.isEnabled()) {
            Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
            // Otherwise, setup the chat session
        } else {
            if (mChatService == null) {
                setupChat();
            }
        }
    } catch (Exception e) {
     Toast.makeText(this,e.toString(), Toast.LENGTH_LONG).show(); 
    }


}
4

0 回答 0