2012-04-16 3 views
2

Я делаю проект bluetooth, в котором я хочу спрятать два устройства Bluetooth в программном обеспечении в android.I использую api для api в android.It находит устройства. Но не возвращает Action_found намерение. условие для action_found намерения, оно возвращает false. Но в logcat он показывает найденные адреса устройств. Вот пример кода, который я попробовал. Может ли кто-нибудь удалить эту ошибку?Пара двух устройств bluetooth programaticaly

package com.qburst.android.settingsmanager.view; 

import java.util.ArrayList; 
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.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.Toast; 

public class Bluetooth extends Activity { 
    protected static final int REQUEST_ENABLE_BT = 1; 
    private View mBluetooth; 
    private Button mOnButton; 
    private Button mActiveDevices; 
    private Button mDiscoverable; 
    public ArrayAdapter<String> mArrayAdapter; 
    public BluetoothAdapter mBluetoothAdapter; 
    ArrayList<String> devices = new ArrayList<String>(); 

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

    setContentView(R.layout.bluetooth); 
    mOnButton = (Button) findViewById(R.id.on_button); 
    mActiveDevices = (Button) findViewById(R.id.active_devices); 
    mDiscoverable = (Button) findViewById(R.id.discoverable_button); 
    mArrayAdapter = new ArrayAdapter<String>(this, R.layout.main); 
    IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); 
    registerReceiver(mReceiver, filter); 


    mOnButton.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
       mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
       if (mBluetoothAdapter == null) { 
        // Device does not support Bluetooth 
       } 
       if (!mBluetoothAdapter.isEnabled()) { 
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); 
       } 

     } 
    }); 


    mActiveDevices.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 

      Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); 
      System.out.println("checking paired devices"); 
      // If there are paired devices 
      if (pairedDevices.size() > 0) { 
       System.out.println(" paired devices"); 
       // 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()); 

       } 
      }else { 
       mBluetoothAdapter.startDiscovery(); 
      } 


     } 
    }); 

    mDiscoverable.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      Intent discoverableIntent = new 
        Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); 
        discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300); 
        startActivity(discoverableIntent); 

      } 



    }); 

    } 

    public void onActivityResult(int requestCode, int resultCode, Intent data) { 

      switch (requestCode) { 


      case REQUEST_ENABLE_BT: 
       // When the request to enable Bluetooth returns 
       if (resultCode == Activity.RESULT_OK) { 
        Toast.makeText(this, "Bluetooth enebled", Toast.LENGTH_SHORT).show(); 

       } else { 
        // User did not enable Bluetooth or an error occurred 

        Toast.makeText(this, "Bluetooth not enebled", Toast.LENGTH_SHORT).show(); 
        finish(); 
       } 
      } 
     } 


    private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 

      public void onReceive(Context context, Intent intent) { 
       String action = intent.getAction(); 
       BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
       devices.add(device.getName()); 
        devices.add(device.getAddress()); 
        System.out.println(devices); 
       // When discovery finds a device 
       Log.i("receiver","Inside receiver"); 

       if (BluetoothDevice.ACTION_FOUND.equals(action)) { 
        System.out.println("device found"); 
        Toast.makeText(getBaseContext(), "No device found",Toast.LENGTH_LONG); 
        // 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 
        System.out.println(device.getName()); 
        devices.add(device.getName()); 
        devices.add(device.getAddress()); 
        mArrayAdapter.add(device.getName() + "\n" + device.getAddress()); 
        System.out.println(devices); 
       } 


      } 
     }; 


     // Register the BroadcastReceiver 

     // Don't forget to unregister during onDestroy 
     @Override 
     protected void onDestroy() { 

      super.onDestroy(); 
     } 


} 

Заранее спасибо

ответ

1

Вы должны добавить правильное действие для фильтра mReceiver в! Вы должны были добавить: filter.addAction (BluetoothDevice.ACTION_FOUND);

BluetoothDevice.ACTION_FOUND не находится в фильтре, поэтому он никогда не принимается!

Так изменить эти строки:

IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); 
registerReceiver(mReceiver, filter); 

к этому:

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

Позвольте мне знать, если это сработало!

+0

спасибо, это сработало для меня –

Смежные вопросы