顯示具有 android 標籤的文章。 顯示所有文章
顯示具有 android 標籤的文章。 顯示所有文章

2019年1月15日 星期二

[Android] BLE Gatt error 133 (solved)

BLE disconnect. Gatt error 133

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

            connectDevice.connectGatt(context, Consts.autoConnect, gatt_callback, BluetoothDevice.TRANSPORT_LE);
}else {

            connectDevice.connectGatt(context, Consts.autoConnect, gatt_callback);
}

2014年7月10日 星期四

[Android] ArrayList save HashMap



ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();

//Save
for(int i=0;i<5;i++) {
        HashMap<String,String> tempMap = new HashMap<String,String>();
        String name = "server"+i;
        String mac = "11:22:33:44:55:6"+i;
        tempMap.put("DEVICE_NAME",name);
        tempMap.put("DEVICE_MAC",mac);
        peripheralScanResultList.add(tempMap);
}

//Get
for(HashMap<String, String> tempMap: peripheralScanResultList) {
        Iterator<String> iterator = tempMap.keySet().iterator();
        while( iterator.hasNext() ){
                String key=(String)iterator.next();
                String value=(String)tempMap.get(key);
                Log.i(TAG, key + ", " +value);
        }
}

[Android] SQLite execSQL, rawQuery

SQLiteDatabase db = dbHelper.getWritableDatabase();

a. Create table

 String INIT_TABLE = "CREATE TABLE IF NOT EXISTS " + tableName + " ("
                                     + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " 
                                     + field1 + " TEXT, "
                                     + field2 + INTEGER, "
                                     + field3 + VARCHAR );" ;


db.execSQL(INIT_TABLE);

b. Delete table

    try {
          db.execSQL("delete from "+ tableName);
    } catch (SQLException e) {
          Log.e("ERROR", e.toString());
    }
    
    db.close();

2013年4月29日 星期一

[Android] remember me checkbox


private CheckBox checkBox;
SharedPreferences login_pref;
private SharedPreferences.Editor loginPrefsEditor;
private Boolean saveLogin;

// onCreate

username = (EditText) findViewById(R.id.editText_username);
password = (EditText) findViewById(R.id.editText_password);
login = (Button) findViewById(R.id.button_login);
checkBox = (CheckBox)findViewById(R.id.checkBox1);
login.setOnClickListener(this);

login_pref = getSharedPreferences("loginPrefs", MODE_PRIVATE);
loginPrefsEditor = login_pref.edit();
saveLogin = login_pref.getBoolean("saveLogin", false);
if (saveLogin == true) {
        username.setText(login_pref.getString("username", ""));
        password.setText(login_pref.getString("password", ""));
        checkBox.setChecked(true);
}

2013年4月18日 星期四

[Android] NFC API application

如何註冊我的APP是NFC程式呢?


1. 首先要先開起 AndroidManifest.xml

<activity android:configchanges="locale" android:label="@string/app_name" android:name=".MyNfc" android:screenorientation="portrait">
    <intent-filter>
        <action android:name="android.intent.action.MAIN">
        <category android:name="android.intent.category.LAUNCHER">
    </category></action>
</intent-filter>

    <intent-filter>
            <action android:name="android.nfc.action.TAG_DISCOVERED">              
    </action>
   </intent-filter>        
    <intent-filter>
            <action android:name="android.nfc.action.TECH_DISCOVERED">                              
    </action></intent-filter>
    <meta-data android:name="android.nfc.action.TECH_DISCOVERED" android:resource="@xml/nfc_tech_filter.xml">              
</meta-data>
</activity>

2012年5月3日 星期四

[Android] SimpleDateFormat ms

java.text.SimpleDateFormat date = 
new java.text.SimpleDateFormat(""yyyy/MM/dd HH:mm:ss:SSS"); //SS = ms

saveTime = date.format(new Date(System.currentTimeMillis()));