查找配对的Android蓝牙设备是否在范围内的正确方法? [英] Proper way to find if a paired Android Bluetooth device is in range?

查看:379
本文介绍了查找配对的Android蓝牙设备是否在范围内的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个将用作蓝牙客户端的应用程序.我要尝试确定的是找出我支持的特定设备是否在范围内而不尝试一直在其上进行BluetoothDevice.connect()且失败的最佳方法.如果不在范围内.在这里,我们假设设备已经配对.

I'm suppose to write an application that will act as the bluetooth client. What I'm trying to do is figure out is what would be the best way to figure out if the specific device that I'm supporting is in range without attempting to do a BluetoothDevice.connect() on it all of the time and failing if it's not in range. Here we're assuming that the device as already been paired.

恐怕尝试一直连接到特定设备直到它处于范围之内都是不明智的做法.在我看来,这对电池不利.

I'm afraid that it would be bad practice to attempt to connect to a specific device all of the time until it's in range. Seems to me that it would be bad for the battery.

有人会知道应该使用什么方法或概念来完成我想做的事情吗?

Would anyone know of any methods or concepts that should be used to accomplish what I'm trying to do?

谢谢!

推荐答案

好吧,据我所知,是要获取与之配对的设备的名称,如果它在范围内?

Ok, so as I understand from your question is you want to get name of device you paired with, and if its in range??

这是解决方案:-

  1. 创建一个名为DeviceDetails的类:

DeviceDetails类{ 公共字符串名称; 公共字符串地址; 公共BluetoothDevice remoteDevice; }

class DeviceDetails{ public String name; public String address; public BluetoothDevice remoteDevice; }

  1. 您需要按此处进行连接和配对设备,完成并建立连接后,创建一个DeviceDetails对象.

  1. You need to connect and pair your devices as explained here, once this is done and connection is made, create an object of DeviceDetails.

DeviceDetails selectedDevice;,如果您有一个自定义适配器来显示设备列表,则将其位置从视图传递到selectedDevice引用.示例:-selectedDevice = adapter.getItem(pos);

DeviceDetails selectedDevice; and if you have a custom adapter to show list of devices pass on the position of from the view to selectedDevice reference. Example :- selectedDevice = adapter.getItem(pos);

现在,您具有已选择要配对的SelectedDevice对象,因此可以将其地址和名称保存在

Now you have the SelectedDevice object which you have selected to pair, thus you can save its address and name in

首选项.

Example:- 
savePairedDeviceInfo(selectedDevice.name, selectedDevice.address);
    public void savePairedDeviceInfo(String name, String addr)
        {
            if(name != null && !name.equalsIgnoreCase(""))
                editor.putString(PNAME_OF_DEVICE, name);

            if(addr != null && !addr.equalsIgnoreCase(""))
                editor.putString(MAC_ADDRESS_OF_DEVICE, addr);

            editor.commit();
        }

  • 现在,下次您要检查是否已完成配对设置时,可以通过从首选项中获取值来检查设备的名称和地址.使用检查(如果有条件)返回真,表明配对的旧设备相同或其他.

  • Now, next time when ever you want to check if setup of pairing is done or not check for name and address of the device by getting values from preferences. Use a check(if cond.) to return true that older device that was paired was same or some other.

    如果已配对的设备在范围内,则您将获得相同的值,否则它将尝试与其他设备配对.

    If paired device is in range you would get the same value else it would try to pair with some other device.

    如果您不了解我的解释,请告诉我.

    Let me know, if you understood from my explanation or not.

    这篇关于查找配对的Android蓝牙设备是否在范围内的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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