简体   繁体   中英

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.


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.

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


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).


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. And to discover the devices in range, you just need to invoke the BTDevice.startDiscovery() to search them. Generally the startDiscovery() drains battery if enabled continuously. If you want, I can edit this post to share a snipet that I used to scan for devices. Hope this helps !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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