在Android中无法获得蓝牙适配器

时间:2012-11-12 07:04:58

标签: android bluetooth forceclose

我试图实现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(); 
    }


}

0 个答案:

没有答案