使用fetchUuidsWithSdp()来防止友好名称缓存

时间:2015-03-16 20:42:24

标签: android bluetooth android-bluetooth

定义:


我的非Android设备(NAD)是一个蓝牙设备,它将其名称从60转为0并以无限方式重置。


目标: 我尝试做的是让我的Android设备尽可能地检测到倒计时并发出尽可能接近NAD计数器的警报。

我通过将设备的原生BluetoothAdapter设置为启动发现()手动将该功能连接到屏幕按钮并密切关注我通过BroadcastReceiver设置的祝酒来实现这一点,这会更新屏幕Textviews使我能够监控我的设备实时接收的内容


需要量: 系统&在这种情况下,资源效率不是一个问题。


问题:(密切注意代码中的第1部分和第2部分) 我不确定使用fetchUuidsWithSdp()是如何帮助我的,因为TextView它的更新仍然是空的,而且意外返回操作的EXTRA_NAME额外填充了Textview ACTION_NAME_CHANGED是缓存的初始发现名称(即。我的应用程序在初始发现后没有读取名称。)


我的代码可以在下面找到

对不起任何新手的错误,我都在努力:)


    public class BTBroadcastReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            //pulling the action name from the broadcasted intent
            String action = intent.getAction();
            if(BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)){
                sToaster("StartedD");//show toast that Discovery has started

            }
            else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
                sToaster("EndedD");//show toast signifying end of discovery
                /*
                if(notfound){
                    mBTAdapter.startDiscovery();
                }*/
            }
            else if(BluetoothDevice.ACTION_FOUND.equals(action)){
                //when a device is found
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                //make sure it's indeed my NAD device by checking MAC address
                if(device.getAddress().equals(MACAddress)){
                    if(notfound){
                        //show device name on screen
                        sToaster("FOUND DEvice");
                        notfound = false;
                        NAD = device;
                        NameShower.setText(device.getName());
                    }
                    else{
                        //do nothing if it's the second time the device is found
                    }
                }
            }
            else if(BluetoothDevice.ACTION_NAME_CHANGED.equals(action)){
                //name changed
                BluetoothDevice foundDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                //make sure it's indeed my NAD device
                if(foundDevice.equals(NAD)){
                    sToaster("Name Change!"); //show on screen that the name change intent has been caught

                    //PART1
                    //to prevent caching of the old device name StackOverflow article 
                    //advised using this function i don't totally understand yet
                    //NAD.fetchUuidsWithSdp();
                    //either commented or not commented the outcome is the same (no refresh of the name)



                    //PART2
                    //tried showing the new name two different ways below, neither of which are effective
                    //by inspecting the TextViews on-screen
                    NameShower.setText(foundDevice.getName());
                    EventView.setText(intent.getStringExtra(BluetoothDevice.EXTRA_NAME));
                    }
                }
            }

    };

1 个答案:

答案 0 :(得分:0)

我参与了一个蓝牙项目,我认为发现过程应该是一个可以在后台注册的Intent。要发现范围内的设备,您只需要调用BTDevice.startDiscovery()来搜索它们。 通常,如果连续启用,startDiscovery()会耗尽电池电量。 如果您愿意,我可以编辑此帖子以共享我用于扫描设备的snipet。 希望这有帮助!