12

ゴール:範囲内のBTデバイスの名前とアドレスを検出し、その値をWebサービスに送信するAndroidアプリを作成します。 BTデバイスは、以前はホストデバイスに接続されていませんでした。私が歩いているうちにすべてをポーリングしたいだけです。ドキュメントの上に注いAndroidでのBluetoothデバイスの検出 - startDiscovery()

  1. を:私は何をやったか

  2. ホストデバイスのBTアダプタのローカルインスタンスを実装しました。
  3. 有効になっていない場合、BTを有効にするための通知を実装しました。
  4. ACTION_FOUNDsstartDiscovery()から解析されるように登録されたブロードキャストレシーバーとインテント。
  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); 
     } 
    } 
} 

}

+0

これは、あなたが得ているエラーを知るのを助けるでしょう。少なくとも、 'startDiscovery()'が間違って機能していると思うように導いているのでしょうか? –

答えて

15

何コンテキストこの方法は、適切に機能するために内に配置する必要がありません。

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

startDiscovery()方法のあなたの使い方は次のようになります。

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

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

    if (mBtAdapter.isDiscovering()) { 
        mBtAdapter.cancelDiscovery(); 
    } 
    
  3. すぐに発見モードをチェックする(そしておそらくキャンセル)の後に、呼び出すことによって、検出を開始

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

が...これが助けたなら、私を知ってみましょう、あなたはまだ問題がある場合は、あなたのlogcat出力および/またはあなたが取得しているすべてのエラーメッセージとあなたの答えを更新し、多分私は少しあなたを助けることができますもっと。

+0

それは助けました。また、ToastではなくArrayAdapterに結果を渡すことも間違いなく助けになりました。 – Lemminkainen

+0

には、Timerを置かずにBGサービスでスキャンを継続する方法があります。システムレベルのAPI。 – CoDe

+0

@ Shubh私はそうは思わない。この制限が設けられる理由は、バックグラウンドでスキャンを継続してアプリケーションが「Bluetooth」を悪用しないようにするためです。 –

関連する問題