在Android上使用LocalSocket实现上层Java和底层C++的通(2)

paramLocalSocket.getOutputStream().write(arrayOfByte1);
            paramLocalSocket.getOutputStream().write(arrayOfByte2);
        }

private void processRequest(LocalSocket paramLocalSocket, Parcel paramParcel)
{
          int cmd = paramParcel.readInt();
          //Log.i(TAG,"parcel:"+String.valueOf(cmd));
          if(cmd>0&&cmd<CMMB_REQUEST_CMD_MAX){
          //String info = "unknown";
          Parcel parcel_info = Parcel.obtain();
          TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
          switch (cmd)
          {
          case CMMB_REQUEST_GET_IMSI:{
              Log.i(TAG, "received CMMB_REQUEST_GET_IMSI command!");
              /*
               * 唯一的用户ID:
               * 例如:IMSI(国际移动用户识别码) for a GSM phone.
               * 需要权限:READ_PHONE_STATE
               */
              String info = tm.getSubscriberId();//String
              if(info!=null) Log.i(TAG, info);
              parcel_info.writeInt(CMMB_REQUEST_GET_IMSI);
              parcel_info.writeString(info);
            }
            break;
....
          try {
             sendResult(paramLocalSocket, parcel_info);
          } catch (IOException e) {
            // TODO Auto-generated catch block
            Log.e(getClass().getName(), e.getMessage());
            e.printStackTrace();
          }

}

以上是服务器端,下面是客户端
#define  SOCKET_NAME_CMMB_SIM_COMMAND   "socket_cmmb_sim_command"
...
    fd = socket_local_client(
                            SOCKET_NAME_CMMB_SIM_COMMAND,
                            ANDROID_SOCKET_NAMESPACE_ABSTRACT,
                            SOCK_STREAM );
    //check open failed
    if (fd < 0)
    {
           LOGE("Failed to open socket: %s\n\r'", SOCKET_NAME_CMMB_SIM_COMMAND);
        goto error;
        }

//get command data
    p_request.writeInt32 (CMMB_REQUEST_GET_IMSI);
    data = (char *)p_request.data();
    datasize = (int)p_request.dataSize();
    //get command length
    dataLength[0] = dataLength[1] = 0;
    dataLength[2] = (char)((datasize >> 8) & 0xff);
    dataLength[3] = (char)(datasize & 0xff);

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/pxdyz.html