简体   繁体   English

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

[英]Using fetchUuidsWithSdp() to prevent friendly name caching

DEFINITION: 定义:


My non-android device (NAD) is a Bluetooth device who loops its name from 60 to 0 and resets in an infinite fashion. 我的非Android设备(NAD)是一种蓝牙设备,其名称从60循环到0,并以无限方式重置。


OBJECTIVE: What I'm trying is to do is to have my android device as closely as possible detect that countdown and initiate an alarm as close to that of the NAD counter as possible. 目的:我要做的是让我的Android设备尽可能接近地检测到该倒数,并发出与NAD计数器尽可能接近的警报。

I'm doing this by getting the native BluetoothAdapter of my device to startDiscovery() manually by tying the function to onscreen buttons and keeping an eye on the toasts I set through my BroadcastReceiver, which updates onscreen Textviews Which enables me to monitor what my device is receiving in real-time 我这样做是通过将功能绑定到屏幕上的按钮,并密切注意通过我的BroadcastReceiver设置的吐司来使设备的本机BluetoothAdapter手动启动startDiscovery(),它会更新屏幕上的Textviews,从而使我能够监视设备上的内容正在实时接收


REQUIREMENT: System & resource efficiency is not a concern in this context. 要求:在这种情况下,系统和资源效率不是问题。


PROBLEM:(Keep an eye out for PART 1 and PART 2 in the code) I'm not sure how using fetchUuidsWithSdp() is helping me since the TextView it's updating remains empty and the Textview getting populated by the EXTRA_NAME extra from intent returning action ACTION_NAME_CHANGED is the cached, initial discovery name (ie. my application is not reading a name after initial discovery). 问题:(请注意代码中的第1部分和第2部分)我不确定使用fetchUuidsWithSdp()会如何帮助我,因为它正在更新的TextView仍然为空,并且EXTRA_NAME填充了Textview,这超出了意图返回操作ACTION_NAME_CHANGED是缓存的初始发现名称(即,我的应用程序在初始发现后未读取名称)。


my code can be found below 我的代码可以在下面找到

Sorry for any newbie mistakes,I'm trying my best :) 对不起,如果有任何新手错误,我正在努力:)


    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));
                    }
                }
            }

    };

I have worked on a bluetooth project and what I perceived was that the discovery process should be in an Intent which can be left registered in the background. 我曾在一个蓝牙项目中工作过,我发现发现过程应该是一个Intent,可以在后台进行注册。 And to discover the devices in range, you just need to invoke the BTDevice.startDiscovery() to search them. 要发现范围内的设备,您只需调用BTDevice.startDiscovery()即可搜索它们。 Generally the startDiscovery() drains battery if enabled continuously. 通常,如果连续启用,则startDiscovery()会耗尽电池电量。 If you want, I can edit this post to share a snipet that I used to scan for devices. 如果您愿意,我可以编辑此帖子以分享我用来扫描设备的摘要。 Hope this helps ! 希望这可以帮助 !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM