0

Bluetooth機能を備えたAndroidアプリケーションを開発しています。しかし、私には1つの問題があります。私は次のコードを使用しています

 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);
        }

そのコードを実行しようとすると、[強制終了]ボタンのあるメッセージボックスが表示されてアプリケーションがクラッシュします。エラーはどこにありますか、私はまだそれを理解していません。親切に助けてください。よろしく

4

2 に答える 2

1

マニフェストに適切な権限がありますか? アプリが Bluetooth を使用することを指定する必要があります。

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

あなたのマニフェストファイルに。マニフェスト ファイルは AndroidManifest.xml という名前になり、Eclipse を使用している場合はプロジェクト フォルダーに表示されます。

于 2012-04-28T22:11:25.517 に答える
0

assalaamualaykumakhi。私はAndroidにかなり慣れていませんが、マニフェストにこのようなものはありますか?

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

if (hasBluetooth && !mBluetoothAdapter.isEnabled())
于 2012-04-28T21:58:54.530 に答える