BLE2

MainActivity.java

package com.example.tawara.bletest2;

import android.Manifest;
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.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.Date;

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 tv1;
private TextView tv2;
private TextView tv3;
private TextView tv4;
private int counter = 0;

private final int REQUEST_PERMISSION = 1000;

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

// if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSIONS_REQUEST_LOCATION_STATE);
// return;
// }
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION,}, REQUEST_PERMISSION);

return;
}
// private boolean checkPermission() {
// if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSIONS_REQUEST_LOCATION_STATE);
// return false;
// }
// return true;
// }

//センサー値表示用テキストビュー
tv1 = (TextView)findViewById(R.id.textView1);
tv2 = (TextView)findViewById(R.id.textView2);
tv3 = (TextView)findViewById(R.id.textView3);
tv4 = (TextView)findViewById(R.id.textView4);

// 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);
//tv1.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));
tv1.setText("相対湿度" + ":" + 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));
tv2.setText("相対温度" + ":" + String.valueOf(soutaiOndo));

//カウンタ
counter++;
tv3.setText("カウンタ" + ":" + String.valueOf(counter));

// 現在の時刻を取得
Date date = new Date();
// 表示形式を設定
SimpleDateFormat sdf = new SimpleDateFormat("yyyy'年'MM'月'dd'日' kk'時'mm'分'ss'秒'");
tv4.setText("更新日時" + ":" + sdf.format(date));

//生データをログ出力
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.tawara.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" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>


<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>

●activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.tawara.bletest2.MainActivity">

<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:text="TextView"
android:textSize="24sp"
app:layout_constraintTop_toBottomOf="@+id/textView3"
tools:layout_editor_absoluteX="143dp" />

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="27dp"
android:text="TextView"
android:textSize="24sp"
app:layout_constraintTop_toBottomOf="@+id/textView2"
tools:layout_editor_absoluteX="143dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:id="@+id/textView" />

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="31dp"
android:text="TextView"
android:textSize="24sp"
app:layout_constraintTop_toBottomOf="@+id/textView"
tools:layout_editor_absoluteX="143dp" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="33dp"
android:text="TextView"
android:textSize="24sp"
app:layout_constraintTop_toBottomOf="@+id/textView1"
tools:layout_editor_absoluteX="143dp" />

</android.support.constraint.ConstraintLayout>

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>