的默认蓝牙名称为 Android Bluedroid
通过搜索你会找到如下文件 device/generic/common/bluetooth/bdroid_buildcfg.h
#ifndef _BDROID_BUILDCFG_H
#define _BDROID_BUILDCFG_H
#define BTM_DEF_LOCAL_NAME "Android Bluedroid"
#endif
如果单一情况你修改此处即可,但如果多台烧录此 room 的设备同时打开蓝牙,你搜索到的蓝牙名称都为 BTM_DEF_LOCAL_NAME 对应的值
为了避免此种情况,我们采用另一种修改方式
将蓝牙名称修改为 你当前设备的 displayID,如果包含 _ ,则取 _ 之前的名称再加上 mac 地址的后六位
举个栗子, 设备 displayID 为 MTK6737-VT_V1.02 蓝牙mac地址为 EF:6D:3C:22:25:56, 则最终的蓝牙名称为 MTK6737-VT_222556
frameworks\base\core\java\android\bluetooth\BluetoothAdapter.java
@RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN)
public boolean enable() {
if (isEnabled()) {
if (DBG) Log.d(TAG, "enable(): BT already enabled!");
updateBleName();//cczheng add
return true;
}
try {
boolean enableResult = mManagerService.enable(ActivityThread.currentPackageName());
if (enableResult) {
updateBleName();//cczheng add
}
return enableResult /*mManagerService.enable(ActivityThread.currentPackageName())*/;
} catch (RemoteException e) {Log.e(TAG, "", e);}
return false;
}
/**
* cczheng add updateBleName whenBluetooth enable
*/
private void updateBleName(){
final String ctag = "ccz";
final String bleName=getName();
Log.e(ctag,".bleName:"+bleName);
new Thread(){
@Override
public void run() {
super.run();
int timeCounter=0;
String macStr=null;
while(timeCounter<20){//500 *20 =10,000 ms
macStr=getAddress();
if(TextUtils.isEmpty(macStr) || macStr.contains("00:00:00")){
try { sleep(500); } catch (InterruptedException e) {}
}else{
break;
}
timeCounter++;
}
Log.e(ctag,".mac:"+macStr);
if(!TextUtils.isEmpty(macStr) && !macStr.contains("00:00:00")){
timeCounter=0;
macStr = macStr.substring(macStr.length() - 8, macStr.length());
macStr = macStr.replaceAll(":", "");
String display = android.os.Build.DISPLAY;
if (display.contains("_")){
display = display.split("_")[0];
}else{
display = "BT";
}
String newBleName = display + "_" + macStr;
Log.e(ctag,".newBleName:"+newBleName);
if(!newBleName.equals(bleName)){
if(bleName.equals("ANDROID BT")
|| bleName.contains("Android Bluedroid")
|| bleName.contains("BT")
|| bleName.contains(display + "_")){
try{
while (mService == null || mService.getState() != STATE_ON) {
try{ sleep(200); } catch (InterruptedException e){}
timeCounter++;
if (timeCounter > 20) {
break;
}
}
Log.e(ctag,".setbleName:"+newBleName);
mService.setName(newBleName);
}catch (RemoteException e){
e.printStackTrace();
}
Log.e(ctag,".setbleNameFinished:"+newBleName);
}
}
}
}
}.start();
}