以编程方式配对两个蓝牙设备

时间:2012-04-16 09:23:54

标签: android

我正在做一个蓝牙项目,我希望在Android中以编程方式配对两个蓝牙设备。我在Android中使用默认api。它正在寻找设备。但是不会返回Action_found的意图。在检查action_found intent的条件时,它返回false。但是在logcat中它显示了找到的设备地址。这是我试过的示例代码。

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 个答案:

答案 0 :(得分:2)

您应该向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);

让我知道它是否有效!