java侧,AdapterProperties类

boolean setName(String name) { synchronized (mObject) { return mService.setAdapterPropertyNative(AbstractionLayer.BT_PROPERTY_BDNAME, name.getBytes()); } }

JNI

static jboolean setAdapterPropertyNative(JNIEnv* env, jobject obj, jint type, jbyteArray value) { ALOGV("%s”, __func__);

if (!sBluetoothInterface) return JNI_FALSE;

jbyte* val = env->GetByteArrayElements(value, NULL); bt_property_t prop; prop.type = (bt_property_type_t)type; prop.len = env->GetArrayLength(value); prop.val = val;

int ret = sBluetoothInterface->set_adapter_property(&prop); env->ReleaseByteArrayElements(value, val, 0);

return (ret == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE; }

BT协议栈

设置BTIF_CORE_STORAGE_ADAPTER_WRITE类型request_ID,然后调用btif_transfer_context(execute_storage_request, storage_req_id,(char*)&req,sizeof(btif_storage_req_t) + property->len,btif_in_storage_request_copy_cb);,这个调用先执行btif_in_storage_request_copy_cb,再是execute_storage_request,实现真正记录

bt_status_t btif_set_adapter_property(const bt_property_t* property) { btif_storage_req_t req; bt_status_t status = BT_STATUS_SUCCESS; int storage_req_id = BTIF_CORE_STORAGE_NOTIFY_STATUS; /* default */ char bd_name[BTM_MAX_LOC_BD_NAME_LEN + 1]; uint16_t name_len = 0;

BTIF_TRACE_EVENT(“btif_set_adapter_property type: %d, len %d, 0x%x”, property->type, property->len, property->val);

if (!btif_is_enabled()) return BT_STATUS_NOT_READY;

switch (property->type) { case BT_PROPERTY_BDNAME: { name_len = property->len > BTM_MAX_LOC_BD_NAME_LEN ? BTM_MAX_LOC_BD_NAME_LEN : property->len; memcpy(bd_name, property->val, name_len); bd_name[name_len] = ‘\0’;

  BTIF\_TRACE\_EVENT("set property name : %s", (char\*)bd\_name);

  BTA\_DmSetDeviceName((char\*)bd\_name);

  storage\_req\_id = BTIF\_CORE\_STORAGE\_ADAPTER\_WRITE;
} break;

case BT\_PROPERTY\_ADAPTER\_SCAN\_MODE: {
  bt\_scan\_mode\_t mode = \*(bt\_scan\_mode\_t\*)property->val;
  tBTA\_DM\_DISC disc\_mode;
  tBTA\_DM\_CONN conn\_mode;

  switch (mode) {
    case BT\_SCAN\_MODE\_NONE:
      disc\_mode = BTA\_DM\_NON\_DISC;
      conn\_mode = BTA\_DM\_NON\_CONN;
      break;

    case BT\_SCAN\_MODE\_CONNECTABLE:
      disc\_mode = BTA\_DM\_NON\_DISC;
      conn\_mode = BTA\_DM\_CONN;
      break;

    case BT\_SCAN\_MODE\_CONNECTABLE\_DISCOVERABLE:
      disc\_mode = BTA\_DM\_GENERAL\_DISC;
      conn\_mode = BTA\_DM\_CONN;
      break;

    default:
      BTIF\_TRACE\_ERROR("invalid scan mode (0x%x)", mode);
      return BT\_STATUS\_PARM\_INVALID;
  }

  BTIF\_TRACE\_EVENT("set property scan mode : %x", mode);

  BTA\_DmSetVisibility(disc\_mode, conn\_mode, BTA\_DM\_IGNORE, BTA\_DM\_IGNORE);

  storage\_req\_id = BTIF\_CORE\_STORAGE\_ADAPTER\_WRITE;
} break;
case BT\_PROPERTY\_ADAPTER\_DISCOVERY\_TIMEOUT: {
  /\* Nothing to do beside store the value in NV.  Java
     will change the SCAN\_MODE property after setting timeout,
     if required \*/
  storage\_req\_id = BTIF\_CORE\_STORAGE\_ADAPTER\_WRITE;
} break;
case BT\_PROPERTY\_CLASS\_OF\_DEVICE: {
  DEV\_CLASS dev\_class;
  memcpy(dev\_class, property->val, DEV\_CLASS\_LEN);

  BTIF\_TRACE\_EVENT("set property dev\_class : 0x%02x%02x%02x", dev\_class\[0\],
                   dev\_class\[1\], dev\_class\[2\]);

  BTM\_SetDeviceClass(dev\_class);
} break;
case BT\_PROPERTY\_BDADDR:
case BT\_PROPERTY\_UUIDS:
case BT\_PROPERTY\_ADAPTER\_BONDED\_DEVICES:
case BT\_PROPERTY\_REMOTE\_FRIENDLY\_NAME:
  /\* no write support through HAL, these properties are only populated from
   \* BTA events \*/
  status = BT\_STATUS\_FAIL;
  break;
default:
  BTIF\_TRACE\_ERROR("btif\_get\_adapter\_property : invalid type %d",
                   property->type);
  status = BT\_STATUS\_FAIL;
  break;

}

if (storage_req_id != BTIF_CORE_STORAGE_NO_ACTION) { /* pass on to storage for updating local database */

req.write\_req.bd\_addr = RawAddress::kEmpty;
memcpy(&(req.write\_req.prop), property, sizeof(bt\_property\_t));

return btif\_transfer\_context(execute\_storage\_request, storage\_req\_id,
                             (char\*)&req,
                             sizeof(btif\_storage\_req\_t) + property->len,
                             btif\_in\_storage\_request\_copy\_cb);

}

return status; }

 

真正干活的execute_storage_request,记录蓝牙名称并触发adapter_properties_cb这个回调,framework记录缓存起来等待后续查询。

static void execute_storage_request(uint16_t event, char* p_param) { bt_status_t status = BT_STATUS_SUCCESS;

BTIF_TRACE_EVENT(“execute storage request event : %d”, event);

switch (event) { case BTIF_CORE_STORAGE_ADAPTER_WRITE: { btif_storage_req_t* p_req = (btif_storage_req_t*)p_param; bt_property_t* p_prop = &(p_req->write_req.prop); BTIF_TRACE_EVENT(“type: %d, len %d, 0x%x”, p_prop->type, p_prop->len, p_prop->val);

  status = btif\_storage\_set\_adapter\_property(p\_prop);
  HAL\_CBACK(bt\_hal\_cbacks, adapter\_properties\_cb, status, 1, p\_prop);
} break;

… …