2012-01-09 8 views
2


Androidプログラミングの新機能です。私は、Bluetoothディスカバリー機能を含むライブラリー(JARファイル)を作成したいと考えています。
ライブラリであるため、内部のメソッドはシーケンシャルメソッド(Bluetoothディスカバリを開始し、時間を待って結果を返す)でなければなりません。
以下のコードを作成しましたが、動作しません。 LogCatでは、私は意図を見ることができましたが、BroadcastReceiverは意図を把握できませんでした。
コードに何か問題がありますか?

Android Bluetooth Discoveryはシーケンシャル方式ですか?

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    // Get local Bluetooth adapter 
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 

    mSendButton = (Button) findViewById(R.id.button1); 
    mSendButton.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      doDiscovery(); 
     } 
    }); 
} 

private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     String action = intent.getAction(); 

     if (BluetoothDevice.ACTION_FOUND.equals(action)) { 
      // Get the BluetoothDevice object from the Intent 
      BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
      Log.e("device_name", device.getName()); 
      Log.e("device_add", device.getAddress()); 
      numberOfDevice++; 
      Found_Device = true; 
     } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { 
      if(numberOfDevice == 0){ 
       Log.e("DISCOVERY", "No Device found"); 
      } 
      Log.e("DISCOVERY", "Number of device :" + numberOfDevice); 
      Discovery_Finish = true; 
     } 
    } 
}; 

public void doDiscovery(){ 
    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); 
    filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); 
    registerReceiver(mReceiver, filter); 

    Discovery_Finish = false; 
    Found_Device = false; 
    long counter = 0; 

    if (mBluetoothAdapter.isDiscovering()) { 
     mBluetoothAdapter.cancelDiscovery(); 
    } 
    mBluetoothAdapter.startDiscovery(); 
    while((Found_Device == false) || (Discovery_Finish == false)){ 
     counter++; 
     if (counter >= 1000000){ 
      break; 
     } 
    } 
    // Add a code to check the number of device 

    unregisterReceiver(mReceiver); 
    mBluetoothAdapter.cancelDiscovery(); 
} 

BroadcastReceiverを使用せずに意思を確認する方法はありますか?
ご協力いただきありがとうございます。

答えて

0

他のデバイスの検出を開始するには不足していると思います。

0

あなたがこれを行うときには、あなたのIntentFilterを上書きされています

IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); 
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); 

あなたはこのようにそれを実行する必要があります。

IntentFilter filter = new IntentFilter(); 
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); 
filter.addAction(BluetoothDevice.ACTION_FOUND);