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=20190723日 星期二 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"
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现一个基于Android蓝牙的遥控器应用,需要完成以下几个步骤: 1. 在应用添加UI元素,如按钮、滑块等,用于控制遥控器的功能; 2. 在应用实现蓝牙连接功能,包括搜索设备、建立连接等; 3. 在应用实现蓝牙数据传输功能,包括向蓝牙设备发送数据和接收蓝牙设备发送的数据; 4. 根据具体需求,将UI元素和蓝牙数据传输功能进行绑定,实现遥控器的功能。 下面是一个简单的示例代码,演示了如何在应用实现蓝牙遥控器的功能: ```java public class MainActivity extends AppCompatActivity { private static final String TAG = "BluetoothControl"; private BluetoothAdapter mBluetoothAdapter; private BluetoothDevice mBluetoothDevice; private BluetoothSocket mBluetoothSocket; private OutputStream mOutputStream; private Button mButtonUp; private Button mButtonDown; private Button mButtonLeft; private Button mButtonRight; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 获取BluetoothAdapter实例 mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); // 获取UI元素 mButtonUp = findViewById(R.id.button_up); mButtonDown = findViewById(R.id.button_down); mButtonLeft = findViewById(R.id.button_left); mButtonRight = findViewById(R.id.button_right); // 设置UI元素的点击事件 mButtonUp.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { sendBluetoothData("UP"); } }); mButtonDown.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { sendBluetoothData("DOWN"); } }); mButtonLeft.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { sendBluetoothData("LEFT"); } }); mButtonRight.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { sendBluetoothData("RIGHT"); } }); } // 搜索蓝牙设备 private void searchBluetoothDevice() { if (mBluetoothAdapter.isDiscovering()) { mBluetoothAdapter.cancelDiscovery(); } mBluetoothAdapter.startDiscovery(); BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (BluetoothDevice.ACTION_FOUND.equals(action)) { BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); if ("蓝牙设备名称".equals(device.getName())) { mBluetoothAdapter.cancelDiscovery(); mBluetoothDevice = device; connectBluetoothDevice(); } } } }; IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); registerReceiver(receiver, filter); } // 建立蓝牙连接 private void connectBluetoothDevice() { try { UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); mBluetoothSocket = mBluetoothDevice.createRfcommSocketToServiceRecord(uuid); mBluetoothSocket.connect(); mOutputStream = mBluetoothSocket.getOutputStream(); } catch (IOException e) { Log.e(TAG, "connectBluetoothDevice: ", e); } } // 向蓝牙设备发送数据 private void sendBluetoothData(String data) { try { mOutputStream.write(data.getBytes()); } catch (IOException e) { Log.e(TAG, "sendBluetoothData: ", e); } } @Override protected void onDestroy() { super.onDestroy(); try { mBluetoothSocket.close(); } catch (IOException e) { Log.e(TAG, "onDestroy: ", e); } } } ``` 需要注意的是,上述示例代码仅适用于演示如何实现蓝牙遥控器的功能,实际应用需要根据具体需求进行修改和完善。同时,还需要考虑蓝牙连接和数据传输过程可能出现的异常情况,并进行相应的处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值