0

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.

Ashvin solanki
  • 4,802
  • 3
  • 25
  • 65

2 Answers2

1

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

petzval
  • 441
  • 1
  • 2
  • 7
  • Thanks for your suggestion but it was not working, it was still showing that old name – Ashvin solanki May 26 '22 at 12:57
  • I can change the BLE device name while an Android scan is in progress and the above code sees the change immediately. – petzval May 26 '22 at 13:11
  • what if you are using a delayed scan ?. – Ashvin solanki May 26 '22 at 13:12
  • I'm not sure what a delayed scan is. I start a scan, it reads every advert packet (I have no filters in place), and this code extracts the name from the packet. If the BLE device changes the packet data, it will be detected. – petzval May 26 '22 at 13:20
  • I am using with filters and delayed scan, it will do same because delayed scan will give you the result that found in that duration or nothing else – Ashvin solanki May 26 '22 at 13:23
  • Two things are necessary. First, the BLE name change must call HCI_LE_Set_Advertising_Data (Bluetooth specification Vol 4 Part E 7.8.7). You can check this with the Android nRF Connect monitor app (on Google play) that will show a name change immediately. Secondly, all scan packets must get through to the callback. There is a filter that rejects duplicate packets, so only the first packet from a device is presented to the callback. This filter needs to be removed. – petzval May 26 '22 at 13:56
  • as you said, your solution works only if i don't use the delay in filters – Ashvin solanki May 27 '22 at 10:57
0

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.

Min Thura
  • 21
  • 2