Android Bluetooth蓝牙名称修改

一、蓝牙名称获取

AdapterService.java (packages\apps\bluetooth\src\com\android\bluetooth\btservice)

    static final int BT_PROPERTY_BDNAME = 0x01;
    static final int BT_PROPERTY_BDADDR = 0x02;
    public void onCreate() {
        getAdapterPropertyNative(AbstractionLayer.BT_PROPERTY_BDADDR);
        getAdapterPropertyNative(AbstractionLayer.BT_PROPERTY_BDNAME);
        	int ret = sBluetoothInterface->get_adapter_property((bt_property_type_t) type);
			    bluetoothInterface,
			    	get_adapter_property,
			    		 btif_get_adapter_property(type);
			    		 btif_transfer_context(execute_storage_request,
				                 BTIF_CORE_STORAGE_ADAPTER_READ,  (char*)&req, sizeof(btif_storage_req_t), NULL);  // call execute_storage_request
				            case BTIF_CORE_STORAGE_ADAPTER_READ:
				                btif_storage_get_adapter_property(&prop);
				                	btif_dm_get_adapter_property(property);
				                	case BT_PROPERTY_BDNAME:
								         bt_bdname_t *bd_name = (bt_bdname_t*)prop->val;
								         strcpy((char *)bd_name->name, btif_get_default_local_name()); // 获取名字
				                HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, status, 1, &prop); //回到到应用层

下面我们主要看下名字获取这部分代码:
btif_dm.c (external\bluetooth\bluedroid\btif\src)

static char btif_default_local_name[DEFAULT_LOCAL_NAME_MAX+1] = {'\0'};

static char* btif_get_default_local_name() {
    if (btif_default_local_name[0] == '\0')
    {
        int max_len = sizeof(btif_default_local_name) - 1;
        if (BTM_DEF_LOCAL_NAME[0] != '\0')
        {
            strncpy(btif_default_local_name, BTM_DEF_LOCAL_NAME, max_len);
        }
        else
        {
            char prop_model[PROPERTY_VALUE_MAX];
            property_get(PROPERTY_PRODUCT_MODEL, prop_model, "");
            strncpy(btif_default_local_name, prop_model, max_len);
        }
        btif_default_local_name[max_len] = '\0';
    }
    return btif_default_local_name;
}

二、蓝牙名称设置

HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, status, 1, &prop); 
	static bt_callbacks_t sBluetoothCallbacks
	    adapter_properties_callback,
	    	 callbackEnv->CallVoidMethod(sJniCallbacksObj,   method_adapterPropertyChangedCallback, types,                          props);
	    	 mAdapterProperties.adapterPropertyChangedCallback(types, val);
	    	 case AbstractionLayer.BT_PROPERTY_BDNAME:
	              mName = new String(val);
	              intent = new Intent(BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED);
	              intent.putExtra(BluetoothAdapter.EXTRA_LOCAL_NAME, mName);
	              intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
	              mService.sendBroadcastAsUser(intent, UserHandle.ALL, mService.BLUETOOTH_PERM);

BluetoothSettings.java (packages\apps\settings\src\com\android\settings\bluetooth)

  private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
            final int state =
                intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);

            if (action.equals(BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED)) {
                updateDeviceName(context);
            }

            if (state == BluetoothAdapter.STATE_ON) {
                mInitiateDiscoverable = true;
            }
        }

        private void updateDeviceName(Context context) {
            if (mLocalAdapter.isEnabled() && mMyDevicePreference != null) {
                mMyDevicePreference.setSummary(context.getResources().getString(
                            R.string.bluetooth_is_visible_message, mLocalAdapter.getName()));
            }
        }
    };

    public BluetoothSettings() {
        super(DISALLOW_CONFIG_BLUETOOTH);
        mIntentFilter = new IntentFilter(BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED);
    }
    public void onResume() {
        getActivity().registerReceiver(mReceiver, mIntentFilter);
    }

三、蓝牙名称修改

1) 修改btif_default_local_name初始值:

static char btif_default_local_name[DEFAULT_LOCAL_NAME_MAX+1] = "";

2) 修改BTM_DEF_LOCAL_NAME的值

// Bt_target.h (external\bluetooth\bluedroid\include)	
#ifdef HAS_BDROID_BUILDCFG
#include "bdroid_buildcfg.h"
#endif

#ifndef BTM_DEF_LOCAL_NAME
#define BTM_DEF_LOCAL_NAME      ""
#endif

在bt_target.h文件的包含了bdroid_buildcfg.h 头文件.

// android/device/generic/common/bluetooth/bdroid_buildcfg.h 
#ifndef _BDROID_BUILDCFG_H
#define _BDROID_BUILDCFG_H
#define BTM_DEF_LOCAL_NAME   "Android Bluedroid"
#endif

3) 修改ro.product.model 属性值
out/target/product/s700_cb7/system/build.prop

# begin build properties
# autogenerated by buildinfo.sh
ro.build.version.sdk=22
ro.build.version.codename=REL
ro.build.version.all_codenames=REL
ro.build.version.release=5.1.1
ro.build.date=2019年 07月 23日 星期二 00:15:32 CST
ro.build.date.utc=1563812132
ro.build.type=userdebug
ro.build.flavor=s700_cb7-userdebug
ro.product.model=S700
ro.product.board=s700_cb7
ro.board.platform=S700
# ro.build.product is obsolete; use ro.product.device
ro.build.product=s700_cb7
# end build properties

# from device/actions/s700_cb7/system.prop

查看 android/build/tools/buildinfo.sh

#!/bin/bash

echo "# begin build properties"
echo "# autogenerated by buildinfo.sh"

echo "ro.build.id=$BUILD_ID"
#echo "ro.build.display.id=$BUILD_DISPLAY_ID"
echo "ro.build.version.incremental=$BUILD_NUMBER"
echo "ro.build.version.sdk=$PLATFORM_SDK_VERSION"
echo "ro.build.version.codename=$PLATFORM_VERSION_CODENAME"
echo "ro.build.version.all_codenames=$PLATFORM_VERSION_ALL_CODENAMES"
echo "ro.build.version.release=$PLATFORM_VERSION"
echo "ro.build.date=`date`"
echo "ro.build.date.utc=`date +%s`"
echo "ro.build.type=$TARGET_BUILD_TYPE"
echo "ro.build.user=$USER"
echo "ro.build.host=`hostname`"
echo "ro.build.tags=$BUILD_VERSION_TAGS"
echo "ro.build.flavor=$TARGET_BUILD_FLAVOR"
echo "ro.product.model=$PRODUCT_MODEL"
#echo "ro.product.brand=$PRODUCT_BRAND"
#echo "ro.product.name=$PRODUCT_NAME"
#echo "ro.product.device=$TARGET_DEVICE"
echo "ro.product.board=$TARGET_BOOTLOADER_BOARD_NAME"
上一篇:SystemUI修改蓝牙状态以及图标,用系统蓝牙图标


下一篇:Bluetooth打开流程