0

Android の Bluetooth ガイドに従って、整数を画面に表示するだけのシンプルなアプリを作成しようとしていますが、ガイドからコードを正しく動作させるのに問題があります。Bluetooth がオフの場合、アプリは Bluetooth をオンにする許可を要求し、オンにすると、「Hello world!」と表示されます。ただし、Bluetooth が既にオンになっていると、プログラムがクラッシュするだけです。

package com.example.bluetooth;

import java.util.Set;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.Toast;


public class MainActivity extends Activity {
private ArrayAdapter<String> mArrayAdapter; 


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (mBluetoothAdapter == null) {
        Toast.makeText(getApplicationContext(), "Cannot Find Bluetooth Adapter", Toast.LENGTH_SHORT).show();
    }

    if (!((BluetoothAdapter) mBluetoothAdapter).isEnabled()) {
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        int REQUEST_ENABLE_BT = 1;
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    }

    Set<BluetoothDevice> pairedDevices = ((BluetoothAdapter) mBluetoothAdapter).getBondedDevices();
    // If there are paired devices
    if (pairedDevices.size() > 0) {
        // Loop through paired devices
        for (BluetoothDevice device : pairedDevices) {
            // Add the name and address to an array adapter to show in a ListView
            mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
        }
    }

// Create a BroadcastReceiver for ACTION_FOUND
final BroadcastReceiver mReceiver = new BroadcastReceiver() {           
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        // When discovery finds a device
        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            // Get the BluetoothDevice object from the Intent
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            // Add the name and address to an array adapter to show in a ListView
            mArrayAdapter.add(device.getName() + "\n" + device.getAddress());                                   
        }       
    }
};

// Register the BroadcastReceiver
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy
}//end of onCreate

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
}

私が受けたすべての助けに感謝します。これは、コードhttp://developer.android.com/guide/topics/connectivity/bluetooth.htmlを書くために私が従おうとしていたものです

質問の仕方が悪くて申し訳ありません。

4

1 に答える 1

1

OnDestroy() 関数をオーバーライドします。関数内に次のように記述します。

unregisterReceiver(mReceiver);
于 2013-09-17T21:59:38.980 に答える