项目缘故,需要用cf.net实现对sim卡一些简单操作,以下是联系人方面的小小心得!
1
Const
10
11
P/Invoke
41
42
Struct
91
92
public static List<string[]> GetSimContacts()
93
{
94
int hSim = 0;
95
try
96
{
97
int result = SimInitialize(0, 0, 0, ref hSim);
98
if (result != SIM_OK)
99
throw new Exception("SIM clocking fails, please test SIM is installed!");
100
101
uint uiUsed = 0;
102
uint uiTotal = 0;
103
SimGetPhonebookStatus(hSim, SIM_PBSTORAGE_SIM, ref uiUsed, ref uiTotal);
104
105
List<string[]> Contacts = new List<string[]>();
106
107
//注意:uiUsed表示本地已存储的个数,uiTotal表示本地可存储的总个数
108
//当你有170个联系人记录,增加一个,会变成171个,然后删除前面170个联系人记录,这时第171人联系人的index为171,而uiUsed则为1
109
for (int i = 1; i <= uiTotal; i++)
110
{
111
SIMPHONEBOOKENTRY entry = new SIMPHONEBOOKENTRY();
112
entry.cbSize = (uint)Marshal.SizeOf(typeof(SIMPHONEBOOKENTRY));
113
if (SimReadPhonebookEntry(hSim, SIM_PBSTORAGE_SIM, (uint)i, ref entry) == SIM_OK)
114
{
115
//有效的联系人记录
116
Contacts.Add(new string[] { entry.lpszText, entry.lpszAddress });
117
}
118
}
119
return Contacts;
120
}
121
catch
122
{
123
throw;
124
}
125
finally
126
{
127
SimDeinitialize(hSim);
128
}
129
}
130
131
/// <summary>
132
/// 添加SIM卡联系人
133
/// </summary>
134
/// <param name="ContactName">联系人</param>
135
/// <param name="ContactPhone">联系电话号码</param>
136
/// <returns>操作是否成功</returns>
137
public static bool AddSimContact(string ContactName,string ContactPhone)
138
{
139
int hSim = 0;
140
try
141
{
142
int result = SimInitialize(0, 0, 0, ref hSim);
143
if (result != SIM_OK)
144
throw new Exception("SIM clocking fails, please test SIM is installed!");
145
146
SIMPHONEBOOKENTRY entry = new SIMPHONEBOOKENTRY();
147
entry.dwAddressType = 0;
148
entry.dwNumPlan = 0;
149
entry.dwParams = SIM_PARAM_PBE_ALL;
150
entry.lpszText = ContactName;
151
entry.lpszAddress = ContactPhone;
152
entry.cbSize = (uint)Marshal.SizeOf(typeof(SIMPHONEBOOKENTRY));
153
result = SimWritePhonebookEntry(hSim, SIM_PBSTORAGE_SIM,SIM_PBINDEX_FIRSTAVAILABLE, ref entry);
154
return (result == SIM_OK) ? true : false;
155
}
156
catch
157
{
158
throw;
159
}
160
finally
161
{
162
SimDeinitialize(hSim);
163
}
164
}
165
166
/// <summary>
167
/// 例子:获取SIM卡有效的联系人记录起始索引
168
/// </summary>
169
/// <returns></returns>
170
public static int GetSimValidPhoneBookEntity_MinIndex()
171
{
172
int hSim = 0;
173
try
174
{
175
int result = SimInitialize(0, 0, 0, ref hSim);
176
if (result != SIM_OK)
177
throw new Exception("SIM clocking fails, please test SIM is installed!");
178
179
SimCaps simCaps=new SimCaps();
180
result = SimGetDevCaps(hSim, SIM_CAPSTYPE_PBINDEXRANGE,ref simCaps);
181
if (result != SIM_OK)
182
throw new Exception("SimGetDevCaps Error!");
183
return (int)simCaps.dwMinPBIndex;
184
}
185
catch
186
{
187
throw;
188
}
189
finally
190
{
191
SimDeinitialize(hSim);
192
}
193
}
194
195
/// <summary>
196
/// 删除SIM卡联系人
197
/// </summary>
198
/// <param name="SIMContactIndex">SIM卡联系人的存储索引</param>
199
/// <returns>操作是否成功</returns>
200
public static bool DeleteSimContact(uint SIMContactIndex)
201
{
202
int hSim = 0;
203
try
204
{
205
int result = SimInitialize(0, 0, 0, ref hSim);
206
if (result != SIM_OK)
207
throw new Exception("SIM clocking fails, please test SIM is installed!");
208
209
result = SimDeletePhonebookEntry(hSim, SIM_PBSTORAGE_SIM, SIMContactIndex);
210
return (result == SIM_OK) ? true : false;
211
}
212
catch
213
{
214
throw;
215
}
216
finally
217
{
218
SimDeinitialize(hSim);
219
}
220
}
221
}
浙公网安备 33010602011771号