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

7

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

  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个回答

4
这是我如何在一个Activity中搜索蓝牙设备并在ListView中显示它们的名称和mac地址。除了在ListView中显示设备外,您可以对发现的BluetoothDevice对象进行实际上的任何操作。 FindBluetoothActivity.java
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);
            } 
        }
    };
}

布局 .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>

Android Manifest.xml 文件:

<?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>

附加信息:

  • 确保您的设备已启用蓝牙
  • 在Manifest.xml文件中添加权限android.permission.BLUETOOTHandroid.permission.BLUETOOTH_ADMIN
  • 当销毁Activity时,请务必注销广播接收器
  • 请记住,处于范围内的蓝牙设备需要被发现才能被您的应用程序找到。仅启用它们上的蓝牙可能不足够。通常情况下,用户需要启用某种“可发现”模式,才能使设备被其他设备发现。
  • 请注意,通过蓝牙发现设备的范围通常在室内约为10米,在室外约为50米

但是如果你看到我的代码...它和你的一样...我看不出有什么区别,为什么我的代码不能正常工作 - JNI_OnLoad
你的代码具体哪里出了问题?onReceive(...) 方法被调用了吗? - Philipp Jahoda
你确定一定还有更多的设备等待被发现吗?它们是否处于可以被发现的蓝牙模式中?你考虑过像我在代码中所做的那样将每个找到的设备添加到列表中吗? - Philipp Jahoda
好的。最后一次尝试:您是否考虑将此操作从IntentFilter中移除:filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); - Philipp Jahoda
是的,我刚试了一下不起作用....但奇怪的是它通常只在Nexus 5上挂起,有任何输入吗? - JNI_OnLoad
显示剩余5条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接