BLE 设备用户定义名称存储在设备的广播包(Advertising Packet)中的 Local Name 字段中。可以使用以下代码示例来获取 BLE 设备的 Local Name:
private BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
// Parse the advertising packet
AdvertiseData data = AdvertiseData.parseFromBytes(scanRecord);
// Get the local name
String localName = data.getDeviceName();
// Do something with the local name
Log.d(TAG, "Found device with local name: " + localName);
}
};