美文网首页
Android P BT MAC 地址通过factory节点读取

Android P BT MAC 地址通过factory节点读取

作者: 如果这都不算帅 | 来源:发表于2019-06-06 17:32 被阅读0次

    获取蓝牙mac地址的逻辑在bluetooth_address.cpp
    vendor/qcom/proprietary/bluetooth/hidl_transport/bt/1.0/default/)

    默认是有六种读取方式
    // Get addr from vendor location
    // Get local bdaddr storage path from a system property.
    // No BDADDR found in the file. Look for BDA in a factory property.
    // Check if address is stored @ Modem NV
    // No factory BDADDR found. Look for a previously stored BDA.
    /* Generate new BDA if necessary */
    由于项目把moderm去掉了 所以原生从nv447写入的mac地址后,由qmi方式从moderm读取的方式不生效了。决定从factory节点读取
    下面是实现方式
    //从factory节点读出来操作

    bool BluetoothAddress::GetFactoryBtAddress( char * btAddr) {
        const int bt_index = 806;//从806写入 写12位
        const int bt_length =13;
        int fd = -1;
        char bt_buf[13] = {0};
        memset( btAddr, 0, 24);
            //节点位置
        fd = open("/dev/block/bootdevice/by-name/factory", O_RDONLY);
        if (fd == -1){
                    //如果没有SELINUX权限  读取不成功
            ALOGE("open factory partiton, read bt_buf failed");
            return false;
        } else {
            int i = 0;
            lseek(fd, bt_index,  SEEK_SET);
            read(fd, bt_buf, bt_length);
                    bt_buf[bt_length - 1] = '\0';
            ALOGE("read factory bt 806 address bt_buf value(%s)", bt_buf);
            if(strlen(bt_buf) == 0){
                ALOGE("read factory bt 806 address bt_buf value is null ");
                return false;
            }
            for( i = 0; i < bt_length; i++){
                btAddr[i] = bt_buf[i];
            }
            ALOGE("read factory bt address ,  btAddr(%s) ", btAddr);
            close(fd);
            return true;
        }
        return false;
    }
    

    下面是调用方法

        if (GetFactoryBtAddress(property)) {
            ALOGE("read factory,  GetFactoryBtAddress property(%s) ", property);
            using namespace std;
            string strs;
            strs=property;
            string btStrs;
            for(int a = 0; a <= 5; a++){
                string x = strs.substr(2*a,2);
                btStrs.append(x);
                  if(5 != a){
                    btStrs.append(":");
                  }
            }
            ALOGE("read factory, btStrs.c_str()(%s) ", btStrs.c_str());
            if (StringToBytes(btStrs.c_str(), local_addr)){
                ALOGE("read factory, local_addr(%s) ", local_addr);
                valid_bda = true;
                le2bd(local_addr);
            }
          }
    

    不要忘记在.h文件声明

      static bool GetFactoryBtAddress(char *btAddr);
    

    之后还要解决SELINUX权限的问题 缺少什么权限加什么权限

    相关文章

      网友评论

          本文标题:Android P BT MAC 地址通过factory节点读取

          本文链接:https://www.haomeiwen.com/subject/ynvuxctx.html