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>