19

目標:範囲内の BT デバイスの名前とアドレスを検出し、それらの値を Web サービスに送信する Android アプリを作成します。BT デバイスは、これまでホスト デバイスに結合されていませんでした。歩き回っているときにすべてをポーリングしたいだけです。

私がやったこと:

  1. ドキュメントを熟読。
  2. ホスト デバイスの BT アダプターのローカル インスタンスを実装しました。
  3. 有効になっていない場合に BT を有効にする通知を実装しました。
  4. startDiscovery()ACTION_FOUNDsの終了を解析するための登録されたブロードキャスト レシーバーとインテント。
  5. マニフェストにBLUETOOTHおよびBLUETOOTH_ADMINパーミッションを登録しました。

物事は(増分コンソールロギングでテストされたように)まで動作しstartDiscovery()ます。


フラストレーション:

  • startDiscovery() -- 間違ったコンテキストでこれを渡していると思われます。このメソッドを適切に機能させるには、どのコンテキスト内に配置する必要がありますか?

この方法をうまく機能させることができた場合は、あなたの知恵に感謝します。

更新- これは、私を悲しませているコードの簡素化されたバージョンです。この単純化は、私のエラーを要約しています。このコードは実行されますが、エラーやその他のエラーはスローcat.logされず、何も出力されません。

package aqu.bttest;

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.widget.Toast;

public class BT2Activity extends Activity {

private BluetoothAdapter mBTA;
private SingBroadcastReceiver mReceiver;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

  //register local BT adapter
    mBTA = BluetoothAdapter.getDefaultAdapter();
    //check to see if there is BT on the Android device at all
    if (mBTA == null){
        int duration = Toast.LENGTH_SHORT;
        Toast.makeText(this, "No Bluetooth on this handset", duration).show();
    }
    //let's make the user enable BT if it isn't already
    if (!mBTA.isEnabled()){
        Intent enableBT = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBT, 0xDEADBEEF);
    }
    //cancel any prior BT device discovery
    if (mBTA.isDiscovering()){
        mBTA.cancelDiscovery();
    }
    //re-start discovery
    mBTA.startDiscovery();

    //let's make a broadcast receiver to register our things
    mReceiver = new SingBroadcastReceiver();
    IntentFilter ifilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    this.registerReceiver(mReceiver, ifilter);
}

private class SingBroadcastReceiver extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction(); //may need to chain this to a recognizing function
        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 Toast
            String derp = device.getName() + " - " + device.getAddress();
            Toast.makeText(context, derp, Toast.LENGTH_LONG);
        }
    }
}

}

4

1 に答える 1

25

このメソッドが正しく機能するには、どのコンテキスト内に配置する必要がありますか。

簡単に言うと、startDiscovery()アプリケーションでローカルBluetoothデバイスを検出する場合に使用する必要があります...たとえば、ListActivity近くのBluetoothデバイスをスキャンして動的に追加するを実装する場合ListView(を参照DeviceListActivity)。

メソッドの使用startDiscovery()法は次のようになります。

  1. ローカルBluetoothアダプタを表すクラス変数を定義します。

    BluetoothAdapter mBtAdapter = BluetoothAdapter.getDefaultAdapter();
    
  2. デバイスがすでに「検出」しているかどうかを確認してください。そうである場合は、検出をキャンセルします。

    if (mBtAdapter.isDiscovering()) {
        mBtAdapter.cancelDiscovery();
    }
    
  3. ディスカバリーモードをチェック(および場合によってはキャンセル)した直後に、を呼び出してディスカバリーを開始します。

    mBtAdapter.startDiscovery();
    
  4. 誤ってデバイスを検出モードのままにしてしまうことについては、一般的に非常に注意してください。デバイス検出の実行は、Bluetoothアダプターにとって重い手順であり、そのリソースの多くを消費します。たとえば、接続を試みる前に、検出を確認/キャンセルする必要があります。ほとんどの場合、メソッドの検出もキャンセルする必要がありますonDestroy

これが役に立ったかどうか教えてください...それでも問題が解決しない場合は、logcatの出力や表示されるエラーメッセージで回答を更新してください。もう少しお手伝いできるかもしれません。

于 2012-05-12T01:49:40.800 に答える