How to refresh Bluetooth device name [Scan Cache]?

889 Views Asked by At

I am working on an application where I am connecting with the BLE device and sending commands. One of that commands we have a command for changing the Bluetooth device name.

Communication is working fine, but the problem is when we send the command for changing the name it was working, BLE confirms the input and sends us the output, but when we disconnect and run LE Scan it was showing the same name as the previous, it should show the new name of the device.

If I want to get the latest name of the device I need to open the Bluetooth page manually in the device and scan over there in the scan result it was showing the latest name, when I open the app again which is in the background and its scanning under LE scan function with 10-sec delay, it was showing the new name in the list.

How can I ask Bluetooth manager or system to refresh the cache or refresh data for that Bluetooth device ?.

I don't know it was right to create ticket, but i have created ticket in google issue tracker : https://issuetracker.google.com/issues/233924346

Thanks.

2

There are 2 best solutions below

7
On BEST ANSWER

I had the same problem and solved it by reading the new name from the raw scan data. In this way you never have to use device.getName() which returns the old name from the cache. This is Android Java code for the scan callback function.

  private ScanCallback newscancallback()
    {
    ScanCallback scb;
  
    // Device scan callback.
    scb = new ScanCallback()
      {
      @Override
      public void onScanResult(int callbackType, ScanResult result)
        {
        super.onScanResult(callbackType, result);
        int n,k,len,getout;
        BluetoothDevice dev;
        byte[] rec;
        StringBuilder nameb;
        String name;
        
        
        dev = result.getDevice();
               
        // do not use dev.getName() which returns cached name
        // read current name from raw scan record instead 

        name = null;
        rec = result.getScanRecord().getBytes();
        len = rec.length;
        nameb = new StringBuilder();
        n = 0;
        getout = 0;
        // search scan record for name
        while(n < len-2 && rec[n] != 0 && getout == 0)
          {
          // rec[n] is length of next item
          // rec[n+1] is item type - look for 8 or 9=name
          // rec[n+2].. is the name, length rec[n]-1
          if(rec[n] > 1 && (rec[n+1] == 8 || rec[n+1] == 9)
            {  // found name
            for(k = 0 ; k < rec[n]-1 ; ++k)
              nameb.append((char)rec[n+2+k]);
            name = nameb.toString();
            getout = 1;
            }
          else  // go to next item
            n += rec[n] + 1;
          }
               
        // name is now null or the new name from the scan record

        }
    
      @Override
      public void onScanFailed(int errcode)
        {
        }
    
      @Override
      public void onBatchScanResults(List<ScanResult> result)
        {
        }
      };
    return (scb);
    }

1
On

As you can see the latest name in the Bluetooth settings of the mobile device, I believe there is no issue with the Bluetooth manager of the system. The issue will be in the scanning function of the code as it is not actually refreshing the scan list yet and it might saved the last known BLE list somewhere in the cache. If you are using third-party library, you might need to check their documentation or codes about how the scan function actually works. There may be like force-refresh option or something in the library. As far as I know, to save the device's battery, there is a delay to actually refresh the scan list.