BLE

●MainActivity.java

package com.example.XXX.bletest2;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

private final static String TAG = "BLECAST_BL main";

private BluetoothAdapter mBluetoothAdapter;
private static final int REQUEST_ENABLE_BT = 1;
private TextView tv;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//センサー値表示用テキストビュー
tv = (TextView)findViewById(R.id.textView1);

// getActionBar().setTitle("BLECAST_BL スキャン");

// if (savedInstanceState == null) {
// getFragmentManager().beginTransaction()
// .add(R.id.container, new PlaceholderFragment())
// .commit();
// }


// 対象デバイスがBLEをサポートしているかのチェック。
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
Toast.makeText(this, "このデバイスではBLEはサポートされておりません。", Toast.LENGTH_SHORT).show();
finish();
}

// Bluetooth adapter 初期化. (API 18以上が必要)
final BluetoothManager bluetoothManager =
(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();

// 対象デバイスBluetoothをサポートしているかのチェック.
if (mBluetoothAdapter == null) {
Toast.makeText(this, "Bluetoothがサポートされておりません。", Toast.LENGTH_SHORT).show();
finish();
return;
}
}

@Override
protected void onResume() {
super.onResume();

// Bluetooth機能が有効になっているかのチェック。無効の場合はダイアログを表示して有効をうながす。(intentにて)
if (!mBluetoothAdapter.isEnabled()) {
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}

//Start scan
mBluetoothAdapter.startLeScan(mLeScanCallback);
}

@Override
protected void onPause() {
super.onPause();
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}

// スキャンのコールバック
private BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() {
// デバイスが発見された時
@Override
public void onLeScan(final BluetoothDevice device, final int rssi, final byte[] scanRecord) {
runOnUiThread(new Runnable() {
@Override
public void run() {
String name = device.getName();
if(name != null){
if(name.equals("ENV_0")){
showData(device, scanRecord);
}
}
}
});
}
};

//データの表示
private void showData(BluetoothDevice device, byte[] scanRecord){
int len = scanRecord.length;
//センサー値を画面に表示
//int sensorData = (scanRecord[9]&0xFF) + ((scanRecord[10]&0xFF)*256);
int sensorData = (scanRecord[6]&0xFF) + ((scanRecord[7]&0xFF)*256);
tv.setText(String.valueOf(sensorData));

//湿度
int namaShitsudo = ((scanRecord[9]&0xFF)*256) + (scanRecord[10]&0xFF);
Log.d("生湿度",String.valueOf(namaShitsudo));
double soutaiShitsudo = (125 * namaShitsudo / 65536) - 6;
Log.d("相対湿度",String.valueOf(soutaiShitsudo));

//温度
int namaOndo = ((scanRecord[11]&0xFF)*256) + (scanRecord[12]&0xFF);
Log.d("生温度",String.valueOf(namaOndo));
double soutaiOndo = (175.72 * namaOndo / 65536) - 46.85;
Log.d("相対温度",String.valueOf(soutaiOndo));

//生データをログ出力
String str = "";
String addressNum = device.getAddress().toString(); //Advertising MAC Address
for(int i=0; i<len; i++){
str += String.format("%x",scanRecord[i]&0xFF)+":";
}
Log.d(TAG,addressNum+"="+str);
}
}



●AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.XXX.bletest2">

<uses-feature
android:name="android.hardware.bluetooth_le"
android:required="true" />

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

java resultset TYPE_SCROLL_INSENSITIVE

https://www.ibm.com/developerworks/jp/java/library/j-5things10.html

java webサービス DTO⇒JSON

●ライブラリ

・Jacksonを使用する

参考URL:http://qiita.com/takehiro224/items/6db2841c0abbf4ffb233

 

DTO

package pack;

public class Info {
public String code;
public String name;
}

 

サーブレット

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub

response.setContentType("applicaton/json;charset=UTF-8");
PrintWriter out = response.getWriter();

// ユーザ名
String user = "XXX";
// パスワード
String pass = "XXX";
// サーバ名
String servername = "localhost";
// SID
String sid = "XE";

Connection conn = null;
Statement stmt = null;
ResultSet rset = null;

try {

// JBBCドライバクラスのロード
Class.forName("oracle.jdbc.driver.OracleDriver");

// Connectionの作成
conn = DriverManager.getConnection("jdbc:oracle:thin:@" + servername + ":1521:" + sid, user, pass);

// Statementの作成
stmt = conn.createStatement();

// Resultsetの作成
rset = stmt.executeQuery("select CODE, NAME from FAMILY");

// ResultSetの件数取得
int count = 0;
while (rset.next()) {
count++;
}

// ResultSet → DTO
Info[] info = new Info[count];
int index = 0;
while (rset.next()) {
Info row = new Info();
row.code = rset.getString("CODE");
row.name = rset.getString("NAME");
info[index] = row;
index++;
}

// DTOJSON文字列
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(info);

out.println(json);

} catch (ClassNotFoundException e) {
//throw e;
} catch (SQLException e) {
//throw e;
} catch (Throwable e) {
throw e;
} finally {
try {
/* クローズ処理 */
if (rset != null) {
rset.close();
rset = null;
}

if (stmt != null) {
stmt.close();
stmt = null;
}

if (conn != null) {
conn.close();
conn = null;
}
} catch (Throwable e) {
// nop
}
}
}

 

●Jacksonを使用せず、JSON文字列の作成

// 取得したデータを出力する
// JSON 文字列の書き込み
out.println("\"employees\":[");
while (rset.next()) {
out.println("{\"code\" : \"" + rset.getString("CODE") + "\"," + "\"name\" : \"" + rset.getString("NAME") + "\"}");
}
out.println("]");

HTML Sample002

<!DOCTYPE html>
<html>
<TABLE BORDER="0">
<TR>
<TD>
<table border="1" rules="none">
<col width="150">
<col width="60">
<col width="60">
<col width="60">
<col width="60">
<col width="60">
<tr>
<td>ああああああ</td>
<td>1-2</td>
<td>1-3</td>
<td>1-4</td>
<td>1-5</td>
<td>1-6</td>
</tr>
<tr>
<td>いいい</td>
<td>2-2</td>
<td>2-3</td>
<td>2-4</td>
<td>2-5</td>
<td>2-6</td>
</tr>
</table>
</TD>
<TD>
<table border="1" rules="none">
<col width="150">
<col width="60">
<col width="60">
<col width="60">
<col width="60">
<col width="60">
<tr>
<td>ああああああ</td>
<td>1-2</td>
<td>1-3</td>
<td>1-4</td>
<td>1-5</td>
<td>1-6</td>
</tr>
<tr>
<td>いいい</td>
<td>2-2</td>
<td>2-3</td>
<td>2-4</td>
<td>2-5</td>
<td>2-6</td>
</tr>
</table>
</TD>
</TR>
</TABLE>
</html>

HTML テーブル横並び

http://siriasu.s10.xrea.com/webtips/tips01.htm

http://www.ynsk.co.jp/smpl/smpl004.htm

HTML Sample001

<!DOCTYPE html>
<html>
<table cellspacing="10">
<col width="150">
<col width="150">
<col width="150">
<col width="150">
<col width="150">
<col width="150">
<tr>
<td>ああああああ</td>
<td>1-2</td>
<td>1-3</td>
<td>1-4</td>
<td>1-5</td>
<td>1-6</td>
</tr>
<tr>
<td>いいい</td>
<td>2-2</td>
<td>2-3</td>
<td>2-4</td>
<td>2-5</td>
<td>2-6</td>
</tr>
<tr>
<td>ううう</td>
<td>3-2</td>
<td>3-3</td>
<td>3-4</td>
<td>3-5</td>
<td>3-6</td>
</tr>
<tr>
<td>えええ</td>
<td>4-2</td>
<td>4-3</td>
<td>4-4</td>
<td>4-5</td>
<td>4-6</td>
</tr>
<tr>
<td>おおお</td>
<td>5-2</td>
<td>5-3</td>
<td>5-4</td>
<td>5-5</td>
<td>5-6</td>
</tr>
<tr>
<td>かかかか</td>
<td>6-2</td>
<td>6-3</td>
<td>6-4</td>
<td>6-5</td>
<td>6-6</td>
</tr>
<tr>
<td>AAAAき</td>
<td>7-2</td>
<td>7-3</td>
<td>7-4</td>
<td>7-5</td>
<td>7-6</td>
</tr>
<tr>
<td>AAAAく</td>
<td>8-2</td>
<td>8-3</td>
<td>8-4</td>
<td>8-5</td>
<td>8-6</td>
</tr>
<tr>
<td>けけけ</td>
<td>9-2</td>
<td>9-3</td>
<td>9-4</td>
<td>9-5</td>
<td>9-6</td>
</tr>
<tr>
<td>ここここ/こここ</td>
<td>10-2</td>
<td>10-3</td>
<td>10-4</td>
<td>10-5</td>
<td>10-6</td>
</tr>
<tr>
<td>ささささささささ</td>
<td>11-2</td>
<td>11-3</td>
<td>11-4</td>
<td>11-5</td>
<td>11-6</td>
</tr>
<tr>
<td>ししししし(ししししし)</td>
<td>12-2</td>
<td>12-3</td>
<td>12-4</td>
<td>12-5</td>
<td>12-6</td>
</tr>
<tr>
<td>すすすすすすすす</td>
<td>13-2</td>
<td>13-3</td>
<td>13-4</td>
<td>13-5</td>
<td>13-6</td>
</tr>
<tr>
<td>せせせせせせ</td>
<td>14-2</td>
<td>14-3</td>
<td>14-4</td>
<td>14-5</td>
<td>14-6</td>
</tr>
<tr>
<td>そそそそそそ</td>
<td>15-2</td>
<td>15-3</td>
<td>15-4</td>
<td>15-5</td>
<td>15-6</td>
</tr>
<tr>
<td>たたたた</td>
<td>16-2</td>
<td>16-3</td>
<td>16-4</td>
<td>16-5</td>
<td>16-6</td>
</tr>
</table>
</html>

HTML テーブル

テーブル

http://www.tohoho-web.com/html/table.htm
http://www.kanzaki.com/docs/html/htminfo16.html
http://webworkersclip.com/2875/
http://www.tagindex.com/html_tag/table/col.html
http://www.newcredge.com/IT/www/html/tag/table/table-col.html

テーブルを横に並べる
http://hmaster.net/table12.html

セルを結合する
http://www.tagindex.com/html_tag/table/td_span.html