2012-04-28 124 views
0

我正在开发一个具有蓝牙功能的android应用程序。但我有一个问题。我使用下面的代码Android蓝牙启用错误

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 

    if (mBluetoothAdapter == null) { 

    // Device does not support Bluetooth 

    tv.setText("Device is not there"); 

    setContentView(tv); 

    } 
    else 
    { 
     if (!mBluetoothAdapter.isEnabled()) { 
      Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
      startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); 
     } 
    tv.setText("Device is there"); 
    setContentView(tv); 
    } 

我得到错误的

if (!mBluetoothAdapter.isEnabled()) { 

Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 

startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); 
     } 

每当我试图运行的代码,我的应用程序崩溃通过展示与“强制关闭”按钮的消息框。错误在哪里,我还没搞清楚。请帮助。 Regards

回答

1

您是否在清单中拥有正确的权限? 您需要指定您的应用程序将使用蓝牙与:在你的清单文件

<uses-permission android:name="android.permission.BLUETOOTH" /> 

。 如果使用Eclipse,清单文件将被命名为AndroidManifest.xml,并且在您的项目文件夹中可见。

+0

是的,这就是我犯的错误,只是在我的清单中添加上面的行,并解决问题。谢了,兄弟。 – 2012-04-28 22:16:48

+0

我得到的错误无法解析“REQUEST_ENABLE_BT”。什么导致了这个错误? – 20B2 2017-06-08 02:58:36

0

assalaamu alaykum akhi。 我对android相当陌生,但是......你有没有像这样的清单中的任何东西?

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
`boolean hasBluetooth = (mBluetoothAdapter == null);` 

if (hasBluetooth && !mBluetoothAdapter.isEnabled()) 
+0

Valaikum萨拉姆兄弟!我对android也很陌生,说实话,我不知道显示是什么。通过浏览网页,我知道它是您的项目中的一个xml文件,描述了您的应用程序将具有哪些功能的android操作系统。那么,如何修改我的menifest以实现蓝牙功能。谢谢。 – 2012-04-28 22:09:41

+0

对不起,刚刚访问过你的个人资料,你确实是姐姐,再次抱歉。但如果你知道,请解决我的问题。 – 2012-04-28 22:10:37

+0

修复了我的清单。非常感谢你的帮助。 – 2012-04-28 22:17:07