如何在范围内找到可用的蓝牙设备?

时间:2014-06-26 07:41:04

标签: android bluetooth broadcastreceiver android-bluetooth

我正在尝试查找范围内的所有可用蓝牙设备。但我只得到一个设备,我在运行方法的线程中使用它。我已经检查了很多关于此问题的链接,但无法解决此问题。 这是我的代码

  public void run() {


if(service != null) {
    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    service.registerReceiver(this.bReceiver, filter);
        bluetooth.startDiscovery();
}

}
class BluetoothReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {

    Set<BluetoothDevice> pairedDevices = bluetooth.getBondedDevices();

    String action = intent.getAction();
    if (pairedDevices.size() > 0) {
            for (BluetoothDevice device : pairedDevices) {
                int rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE);
       Log.d(TAG, device.getName());
    }
    }

    if(BluetoothDevice.ACTION_FOUND.equals(action)) {
    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
    String uuid = intent.getStringExtra(BluetoothDevice.EXTRA_UUID);
    int rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE);
    Log.d(TAG, device.getName());
    }
    }

}

另外我想要找到每个设备的rssi值,但请忽略语法

1 个答案:

答案 0 :(得分:2)

这就是我<{1}}中搜索蓝牙设备的方式,并在Activity中显示其姓名和mac地址。除了在ListView中显示设备外,您几乎可以使用发现的ListView对象执行任何操作。

<强> FindBluetoothActivity.java

BluetoothDevice

布局 .xml文件:

public class FindBluetoothActivity extends Activity {

    private BluetoothAdapter mBtAdapter;
    private ListView mLvDevices;
    private ArrayList<String> mDeviceList = new ArrayList<String>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_bluetooth);

        mLvDevices = (ListView) findViewById(R.id.lvDevices);

        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);        
        registerReceiver(mBtReceiver, filter); 

        // Getting the Bluetooth adapter
        mBtAdapter = BluetoothAdapter.getDefaultAdapter();
        if(mBtAdapter != null) {
            mBtAdapter.startDiscovery();
            Toast.makeText(this, "Starting discovery...", Toast.LENGTH_SHORT).show();
        }
        else {
            Toast.makeText(this, "Bluetooth disabled or not available.", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mBtAdapter != null) {
            mBtAdapter.cancelDiscovery();
        }
        unregisterReceiver(mBtReceiver);
    }

    private final BroadcastReceiver mBtReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                mDeviceList.add(device.getAddress() + ", " + device.getName()); // get mac address

                ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, mDeviceList);
                mLvDevices.setAdapter(adapter);
            } 
        }
    };
}

Android Manifest.xml 文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".FindBluetoothActivity" >

    <ListView
        android:id="@+id/lvDevices"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ListView>

</RelativeLayout>

其他信息:

  • 确保设备启用
  • 将权限<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.bluetoothexample" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="11" android:targetSdkVersion="18" /> <uses-permission android:name="android.permission.BLUETOOTH"/> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@android:style/Theme.Holo.Light" > <activity android:name="com.example.bluetoothexample.FindBluetoothActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> android.permission.BLUETOOTH添加到您的Manifest.xml文件
  • 在销毁android.permission.BLUETOOTH_ADMIN
  • 时,务必取消注册您的广播接收器
  • 请注意,您的应用程序可以找到范围内的蓝牙设备。仅在它们上启用蓝牙可能还不够。通常情况下,某种类型的可被发现的&#34;在其他设备可以发现设备之前,用户需要启用该模式。
  • 请注意,通过蓝牙可以发现设备的范围通常在室内 10m 附近 50m