0

私はAndroid Bluetoothチャットアプリケーションを構築していますが、その中でいくつかの問題に直面しています.私の問題は、範囲内で利用可能なBluetoothデバイスを検出できず、リストに表示できないことです。問題。助けてください。

私のコードは次のとおりです。

public class BluetoothSearchActivity extends Activity {

    ArrayAdapter<String> btArrayAdapter;
    BluetoothAdapter mBluetoothAdapter;
    TextView stateBluetooth;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ImageView BluetoothSearchImageView=new ImageView(this);
        BluetoothSearchImageView.setImageResource(R.drawable.inner1);

        setContentView(BluetoothSearchImageView);
        setContentView(R.layout.activity_bluetooth_search);

        mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();

        ListView listDevicesFound=(ListView) findViewById(R.id.myList);

        btArrayAdapter=new ArrayAdapter<String> (BluetoothSearchActivity.this,android.R.layout.simple_list_item_1);

        listDevicesFound.setAdapter(btArrayAdapter);

        registerReceiver(ActionFoundReceiver,new IntentFilter(BluetoothDevice.ACTION_FOUND));

        btArrayAdapter.clear();
        mBluetoothAdapter.startDiscovery();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(ActionFoundReceiver);
    }

    private final BroadcastReceiver ActionFoundReceiver=new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            String action=intent.getAction();
            if(BluetoothDevice.ACTION_FOUND.equals(action)) {
                BluetoothDevice device=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                btArrayAdapter.add(device.getName()+"\n"+device.getAddress());
                btArrayAdapter.notifyDataSetChanged();
            }
        }   
    };
4

1 に答える 1

0

他のデバイスを検出可能に設定しましたか? ほとんどのスマートフォンは、デフォルトではペアリングされたデバイスから見えないため、表示するには表示を有効にする必要があります。

APIリファレンスの例を使用しているので、うまくいくはずです。

以下があることを確認してください。

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

AndroidManifest.xml ファイル内。

それ以外に、オブジェクトにはいくつかの奇妙な命名規則があります。ActionFoundReceiver は actionFoundReceiver (クラスではなくオブジェクト) である必要がありますが、大したことではありません。

何らかのエラー メッセージが表示された場合は、それも投稿してください。

編集: デバイス名とアドレスを Log.d(tag, string) に書き出すことができるので、logcat デバッグで読み取ることができ、リストの表示に関する問題が解消されます。

編集: Log.d のコードを追加

import android.util.Log;

//to use Log.d(String, String)
Log.d("someTag", "your text here");

Log.d を使用して、コードが正しく実行されているかどうかを理解するのに役立つデバッグ情報を出力できます。Log.d("tag", device.getName()+" "+device.getAddress());リスナーからの応答が得られているかどうかを確認したい と思います。そして、リストなどに入れようとすることで発生する可能性のある問題を排除します.

-- トーマス・フローロ

于 2012-10-24T06:53:16.860 に答える