javaandroidbluetoothbluetooth-lowenergybluetoothlescanner

How to refresh Bluetooth device name [Scan Cache]?


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.


Solution

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