我在类似的场景中使用过O2J - Objective-C to Java Converter,效果很好。
无需太多工作,它将在您的算法/逻辑上做得很好。
它是可定制的,因此您可以为您的蓝牙代码添加自己的翻译。如果 API 的工作方式相同,您可以通过将蓝牙方法调用直接转换为 java 来获得,但它们可能不会。最好在您的 Objective-C 代码中为蓝牙添加一层间接层,以便真正轻松地提供 Android 特定的实现。例如创建一个BluetoothHelper.m 和一个BluetoothHelper.java,翻译会更顺畅。
我已经将它用于使用 FMDatabase 的项目。对于 FMDatabase 部分,我们已经将 FMDatabase/FMResultSet 作为间接层!我自己实现了 FMDatabase/FMResultSet,因为 sqlite Objective-c(基于 c 的 sqlite 函数)的 API 与 Android 太不同了。O2J 帮助我开始翻译 FMDatabase/FMResultSet,这就是我最终得到的...
调频数据库:
public class FMDatabase
{
private SQLiteDatabase database;
private HashMap<String, SQLiteStatement> compiled;
public FMDatabase(SQLiteDatabase database)
{
this.database = database;
}
public FMResultSet executeQuery_arguments(String sql, Object... args)
{
synchronized (database)
{
String[] selectionArgs = objectArgsAsStrings(args);
Cursor rawQuery = database.rawQuery(sql, selectionArgs);
return new FMResultSet(rawQuery);
}
}
public FMResultSet executeQuery(String sql, Object... args)
{
synchronized (database)
{
String[] selectionArgs = objectArgsAsStrings(args);
Cursor rawQuery = database.rawQuery(sql, selectionArgs);
return new FMResultSet(rawQuery);
}
}
public String debugQuery(String sql, Object...args)
{
StringBuilder sb = new StringBuilder();
FMResultSet rs = executeQuery(sql, args);
rs.setupColumnNames();
HashMap names = rs.columnNameToIndexMap();
Set ks = names.keySet();
for (Object k : ks)
{
sb.append(k);
sb.append("\t");
}
sb.append("\n");
while(rs.next())
{
for (Object k : ks)
{
String key = k.toString();
if(rs.getType(key) == Cursor.FIELD_TYPE_STRING)
{
sb.append(rs.stringForColumn(key));
}
else if(rs.getType(key) == Cursor.FIELD_TYPE_INTEGER)
{
sb.append(rs.longForColumn(key));
}
else if(rs.getType(key) == Cursor.FIELD_TYPE_FLOAT)
{
sb.append(rs.doubleForColumn(key));
}
else if(rs.getType(key) == Cursor.FIELD_TYPE_BLOB)
{
sb.append(rs.stringForColumn(key));
}
else
{
sb.append("<NOT STRING>");
}
sb.append("\t");
}
sb.append("\n");
}
return sb.toString();
}
public String[] objectArgsAsStrings(Object... args)
{
String[] selectionArgs = new String[args.length];
for (int i = 0; i < args.length; i++)
{
Object o = args[i];
if(o instanceof Date)
{
selectionArgs[i] = Long.toString(((Date) o).getTime());
}
else if(o instanceof Boolean)
{
selectionArgs[i] = ((Boolean) o).booleanValue() ? "TRUE" : "FALSE";
}
else
{
selectionArgs[i] = args[i] == null ? "" : o.toString();
}
}
return selectionArgs;
}
public boolean executeUpdate_arguments(String sql, Object... args)
{
synchronized (database)
{
String[] selectionArgs = objectArgsAsStrings(args);
database.execSQL(sql, selectionArgs);
return true;
}
}
public boolean executeUpdate(String sql, Object... args)
{
synchronized (database)
{
SQLiteStatement statement = bindToCachedCompiledStatement(sql, args);
statement.execute();
return true;
}
}
private SQLiteStatement bindToCachedCompiledStatement(String sql, Object... args)
{
HashMap<String, SQLiteStatement> statments = getCompiledStatements();
SQLiteStatement statement = statments.get(sql);
if (statement == null)
{
statement = database.compileStatement(sql);
statments.put(sql, statement);
}
statement.clearBindings();
// bindAllArgsAsStrings(statement, objectArgsAsStrings(args));
bindAllArgs(statement, args);
return statement;
}
private void bindAllArgs(SQLiteStatement statement, Object[] bindArgs)
{
if (bindArgs == null)
{
return;
}
int size = bindArgs.length;
for (int i = 0; i < size; i++)
{
Object arg = bindArgs[i];
int index = i + 1;
if(arg == null)
{
statement.bindNull(index);
}
else if (arg instanceof String)
{
statement.bindString(index, (String) arg);
}
else if (arg instanceof Double || arg instanceof Float)
{
Number numArg = (Number) arg;
statement.bindDouble(index, numArg.doubleValue());
}
else if (arg instanceof Integer || arg instanceof Long)
{
Number numArg = (Number) arg;
statement.bindDouble(index, numArg.longValue());
}
else
{
statement.bindString(index, arg.toString());
}
}
}
public long executeInsert(String string, Object... args)
{
synchronized (database)
{
SQLiteStatement statement = bindToCachedCompiledStatement(string, args);
try
{
return statement.executeInsert();
}
catch (Exception e)
{
Log.i("STD", "No Rows inserted", e);
return 0;
}
}
}
public void bindAllArgsAsStrings(SQLiteStatement statement, String[] bindArgs)
{
if (bindArgs == null)
{
return;
}
int size = bindArgs.length;
for (int i = 0; i < size; i++)
{
statement.bindString(i + 1, bindArgs[i]);
}
}
private HashMap<String, SQLiteStatement> getCompiledStatements()
{
if (compiled == null)
{
compiled = new HashMap<String, SQLiteStatement>();
}
return compiled;
}
public boolean rollback()
{
synchronized (database)
{
database.execSQL("ROLLBACK;");
}
return true;
}
public boolean commit()
{
synchronized (database)
{
database.execSQL("COMMIT;");
}
return true;
}
public boolean beginDeferredTransaction()
{
synchronized (database)
{
database.execSQL("BEGIN DEFERRED TRANSACTION;");
}
return true;
}
public boolean beginTransaction()
{
synchronized (database)
{
database.execSQL("BEGIN EXCLUSIVE TRANSACTION;");
}
return true;
}
public boolean open()
{
return true;
}
public void setShouldCacheStatements(boolean shouldCacheStatements)
{
// TODO
}
}
FM结果集:
public class FMResultSet
{
private boolean columnNamesSetup;
private HashMap<String, Number> columnNameToIndexMap;
private Cursor rawQuery;
public FMResultSet(Cursor rawQuery)
{
this.rawQuery = rawQuery;
}
public void close()
{
rawQuery.close();
}
public void setupColumnNames()
{
if (columnNameToIndexMap == null)
{
this.setColumnNameToIndexMap(new HashMap());
}
int columnCount = rawQuery.getColumnCount();
int columnIdx = 0;
for (columnIdx = 0; columnIdx < columnCount; columnIdx++)
{
columnNameToIndexMap.put(rawQuery.getColumnName(columnIdx).toLowerCase(), new Integer(columnIdx));
}
columnNamesSetup = true;
}
public boolean next()
{
return rawQuery.moveToNext();
}
public int columnIndexForName(String columnName)
{
if (!columnNamesSetup)
{
this.setupColumnNames();
}
columnName = columnName.toLowerCase();
Number n = columnNameToIndexMap.get(columnName);
if (n != null)
{
return NumberValueUtil.intVal(n);
}
Log.i("StdLog", String.format("Warning: I could not find the column named '%s'.", columnName));
return -1;
}
public int intForColumn(String columnName)
{
if (!columnNamesSetup)
{
this.setupColumnNames();
}
int columnIdx = this.columnIndexForName(columnName);
if (columnIdx == -1)
{
return 0;
}
return intForColumnIndex(columnIdx);
}
public int intForColumnIndex(int columnIdx)
{
return rawQuery.getInt(columnIdx);
}
public long longForColumn(String columnName)
{
if (!columnNamesSetup)
{
this.setupColumnNames();
}
int columnIdx = this.columnIndexForName(columnName);
if (columnIdx == -1)
{
return 0;
}
return longForColumnIndex(columnIdx);
}
public long longForColumnIndex(int columnIdx)
{
return (long) rawQuery.getLong(columnIdx);
}
public boolean boolForColumn(String columnName)
{
return (this.intForColumn(columnName) != 0);
}
public boolean boolForColumnIndex(int columnIdx)
{
return (this.intForColumnIndex(columnIdx) != 0);
}
public double doubleForColumn(String columnName)
{
if (!columnNamesSetup)
{
this.setupColumnNames();
}
int columnIdx = this.columnIndexForName(columnName);
if (columnIdx == -1)
{
return 0;
}
return doubleForColumnIndex(columnIdx);
}
public double doubleForColumnIndex(int columnIdx)
{
return rawQuery.getDouble(columnIdx);
}
public String stringForColumnIndex(int columnIdx)
{
return rawQuery.getString(columnIdx);
}
public String stringForColumn(String columnName)
{
if (!columnNamesSetup)
{
this.setupColumnNames();
}
int columnIdx = this.columnIndexForName(columnName);
if (columnIdx == -1)
{
return null;
}
return this.stringForColumnIndex(columnIdx);
}
public Date dateForColumn(String columnName)
{
if (!columnNamesSetup)
{
this.setupColumnNames();
}
int columnIdx = this.columnIndexForName(columnName);
if (columnIdx == -1)
{
return null;
}
return new Date((this.longForColumn(columnName)));
}
public Date dateForColumnIndex(int columnIdx)
{
return new Date((this.longForColumnIndex(columnIdx)));
}
public byte[] dataForColumn(String columnName)
{
if (!columnNamesSetup)
{
this.setupColumnNames();
}
int columnIdx = this.columnIndexForName(columnName);
if (columnIdx == -1)
{
return null;
}
return this.dataForColumnIndex(columnIdx);
}
public byte[] dataForColumnIndex(int columnIdx)
{
return rawQuery.getBlob(columnIdx);
}
public HashMap columnNameToIndexMap()
{
return columnNameToIndexMap;
}
public void setColumnNameToIndexMap(HashMap value)
{
columnNameToIndexMap = value;
}
@SuppressLint("NewApi")
public int getType(String string)
{
return rawQuery.getType(columnIndexForName(string));
}
}