如何发现,当过滤BluetoothDevice类 [英] How to filter BluetoothDevice when discovering

查看:107
本文介绍了如何发现,当过滤BluetoothDevice类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要当发现过滤BluetoothDevice类,并只保留它们开始用相同的软件听。例如,Android的样本BluetoothChat,它应该只显示这是由BluetoothChat开始得的蓝牙设备。

下面是我的code。

 公共类DeviceListActivity扩展活动
{
    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);        //设置UI这里
        ......        //注册的广播时,设备被发现
        IntentFilter的过滤器=新的IntentFilter(BluetoothDevice.ACTION_FOUND);
        this.registerReceiver(mReceiver,过滤器);        //注册的广播时,发现已完成
        过滤器=新的IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        this.registerReceiver(mReceiver,过滤器);        //获取本地蓝牙适配器
        mBtAdapter = BluetoothAdapter.getDefaultAdapter();        //请求从BluetoothAdapter发现
        mBtAdapter.startDiscovery();
    }    //侦听发现的设备和广播接收器
    //当发现完成改变标题
    私人最终的BroadcastReceiver mReceiver =新的广播接收器(){
        @覆盖
        公共无效的onReceive(上下文的背景下,意图意图){
            字符串行动= intent.getAction();            //当发现找到的设备
            如果(BluetoothDevice.ACTION_FOUND.equals(动作)){
                //从意向获取BluetoothDevice类对象
                BluetoothDevice类设备= intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);                // ***我的问题是在这里。我有一个BluetoothDevice类了,但我怎么能
                // ***检查是否是我的软件创建的?
                // ***如果我不能,我要所有的人都加入到我的列表中,当用户
                // ***选择它,如果它不是由我的软件创建的连接将失败。
                mNewDevicesArrayAdapter.add(device.getName()+\\ n+ device.getAddress());
            }
            //当发现完成后,更改活动标题
            }否则如果(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(动作)){
                //发现完成...
                //这里包code ...
                ......
            }
        }
    };
}


解决方案

您可以做一件事:

当你使用你的软件开始支持蓝牙,蓝牙的名称更改为一个特定的模式,这样就可以识别设备是由软件启动。

然后同时搜索蓝牙设备,请检查您来到翻过,而搜索蓝牙设备名称是否是您所定义的格局。

要更改设备名称:

更改Android的蓝牙设备名称

I want to filter BluetoothDevice when discovering and only keep which are started listening by same software. For example, the Android sample BluetoothChat, it should only show those bluetooth devices which are started by BluetoothChat too.

Here is my code.

public class DeviceListActivity extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Setup UI here
        ......

        // Register for broadcasts when a device is discovered
        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        this.registerReceiver(mReceiver, filter);

        // Register for broadcasts when discovery has finished
        filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        this.registerReceiver(mReceiver, filter);

        // Get the local Bluetooth adapter
        mBtAdapter = BluetoothAdapter.getDefaultAdapter();

        // Request discover from BluetoothAdapter
        mBtAdapter.startDiscovery();
    }

    // The BroadcastReceiver that listens for discovered devices and
    // changes the title when discovery is finished
    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            // When discovery finds a device
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                // Get the BluetoothDevice object from the Intent
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

                // *** My problem is here. I got a BluetoothDevice now, but how can I
                // *** check whether it is created by my software?
                // *** if I can't, I have to add all of them to my list, and when user
                // *** chooses it, connection will fail if it's not created by my software.
                mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
            }
            // When discovery is finished, change the Activity title
            } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
                // Discovery is finished...
                // wrap up code here...
                ......
            }
        }
    };
}

解决方案

You can do one thing :

When you start bluetooth by using your software, change the name of your bluetooth to a particular pattern so that you can identify the device is started by your software.

Then while searching for bluetooth device, check whether the bluetooth device name that you came accross while searching is of the pattern that you defined.

To change device name :

Change the Android bluetooth device name

这篇关于如何发现,当过滤BluetoothDevice类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆