【问题标题】:How to find available bluetooth devices in range?如何找到范围内可用的蓝牙设备?
【发布时间】:2014-06-26 07:41:03
【问题描述】:

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

  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值,但请忽略语法

【问题讨论】:

    标签: android bluetooth broadcastreceiver android-bluetooth


    【解决方案1】:

    这就是我在 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>
    

    附加信息:

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

    【讨论】:

    • 但是如果你看到我的代码......它和你的一样......我没有看到任何区别,那么为什么我的代码不起作用
    • 您的代码到底有什么问题?是否调用了 onReceive(...)?
    • 问题是有时应用程序在到达此广播接收器后挂起,不知道为什么,其他主要问题是我只有一个活动蓝牙设备,而不是我想要所有可用的活动蓝牙设备...跨度>
    • 您确定还有更多设备需要被发现吗?它们是否处于可以被发现的蓝牙模式?您是否考虑过像我在代码中那样将每个找到的设备添加到列表中?
    • 是的,我通过在此应用附近放置另外两台设备并开启蓝牙模式来检查它...不,我没有尝试过,但我认为 listview 没有太大区别
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-08
    • 2017-04-18
    • 2014-08-10
    • 1970-01-01
    • 2016-12-18
    • 1970-01-01
    • 2023-03-21
    相关资源
    最近更新 更多