4

Android アプリケーションを使用しているデバイスで Bluetooth が有効になっているかどうかを確認したいと考えています。.isEnabled メソッドを使用しました。しかし、エラーがあります。エラーが .isEnabled メソッドにあることが(行にコメントすることで)わかりました。これを理解するのを手伝ってもらえますか?

final BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();

submitButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        String status = "Bluetooth";

        if(bluetooth != null) {
            if (bluetooth.isEnabled()) {
                String mydeviceaddress = bluetooth.getAddress();
                String mydevicename = bluetooth.getName();
                status = ("Address "+ mydeviceaddress + " Name" + mydevicename);
                Toast.makeText(getApplicationContext(), "" + status + "", Toast.LENGTH_LONG).show();
            } else {
                status = ("Bluetooth not enabled");
                Toast.makeText(getApplicationContext(), "" + status + "", Toast.LENGTH_LONG).show();
            }
        } else {
            Toast.makeText(getApplicationContext(), "" + status + "", Toast.LENGTH_LONG).show();
        }
    }
}
4

4 に答える 4

20

これは私にとって最もうまくいきました:

/**
 * Check for Bluetooth.
 *
 * @return true if Bluetooth is available.
 */
public boolean isBluetoothAvailable() {
    final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    return bluetoothAdapter != null 
        && bluetoothAdapter.isEnabled() 
        && bluetoothAdapter.getState() == BluetoothAdapter.STATE_ON;
}
于 2014-01-09T01:25:07.843 に答える
6

これを試して。

BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
    // Device does not support Bluetooth
} else {
    if (!bluetoothAdapter.isEnabled()) {
        // Bluetooth is not enabled
    }
}

あなたのAndroidManifest.xml File追加で

<uses-permission android:name="android.permission.BLUETOOTH" />
于 2012-06-28T10:25:34.750 に答える
0

なぜだけではないのですか:

...
return mBluetoothAdapter != null && mBluetoothAdapter.isEnabled();
于 2014-09-02T14:34:43.203 に答える