Skip to content

Commit

Permalink
Merge pull request #4 from zkjs/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Ganben authored Nov 23, 2016
2 parents df0e853 + e0a2942 commit 8a7e496
Show file tree
Hide file tree
Showing 16 changed files with 468 additions and 120 deletions.
16 changes: 14 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,35 @@
android:theme="@style/AppTheme"
>

<activity android:name=".MainActivity" >
<activity
android:name=".MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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

<activity android:name=".YunbaSettingActivity" android:screenOrientation="portrait" />


<!-- 云巴配置 -->
<meta-data android:name="YUNBA_APPKEY" android:value="58117375374bd76b1b5b690a" />
<meta-data android:name="YUNBA_APPKEY" android:value="5816f90f6cf991dc38fd2161" />
<service android:name="io.yunba.android.core.YunBaService"> </service>
<receiver android:name="io.yunba.android.core.YunBaReceiver">
<intent-filter>
<action android:name="android.intent.action.USER_PRESENT" />
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
<receiver android:name=".receiver.YunBaMessageReceiver">
<intent-filter>
<action android:name="io.yunba.android.MESSAGE_RECEIVED_ACTION" />
<action android:name="io.yunba.android.PRESENCE_RECEIVED_ACTION" />
<category android:name="net.chaoc.blescanner" />
</intent-filter>
</receiver>

</application>

Expand Down
3 changes: 0 additions & 3 deletions app/src/main/assets/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,5 @@
<cdn-host>cdn.zkjinshi.com</cdn-host>
<!--pcm图片(资源服务)-->
<pcd-host>pcd.zkjinshi.com</pcd-host>
<!-- 云巴默认alias 及 topic -->
<yunba-alias>alias</yunba-alias>
<yunba-topic>topic</yunba-topic>

</config>
14 changes: 9 additions & 5 deletions app/src/main/java/net/chaoc/blescanner/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
*/

public class Constants {
public static final String API_HOST = "api-host";//api
public static final String CDN_HOST = "cdn-host";//cdn图片
public static final String PCD_HOST = "pcd-host";//pcm图片
public static final String YUNBA_ALIAS = "yunba-alias";
public static final String YUNBA_TOPIC = "yunba-topic";
public static final String API_HOST = "api-host"; // api
public static final String CDN_HOST = "cdn-host"; // cdn图片
public static final String PCD_HOST = "pcd-host"; // pcm图片

public static final String YUNBA_ALIAS = "yunba-alias"; // 云巴publish Alias key
public static final String YUNBA_TOPIC = "demo"; // 云巴订阅Topic key

public static final int MANUFACTURER_DATA = 65280; // 蓝牙设备生产商ID
public static final String SOS_SIGNAL = "1266"; // 紧急呼救信号
}
128 changes: 103 additions & 25 deletions app/src/main/java/net/chaoc/blescanner/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
package net.chaoc.blescanner;

import android.app.AlertDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanResult;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.gson.Gson;

import net.chaoc.blescanner.models.BleInfo;
import net.chaoc.blescanner.models.YunbaPayload;
import net.chaoc.blescanner.utils.CacheUtil;
import net.chaoc.blescanner.utils.ConfigUtil;

Expand Down Expand Up @@ -45,6 +53,8 @@ public class MainActivity extends AppCompatActivity {
private final static int REQUEST_ENABLE_BT = 1;
private static final int PERMISSION_REQUEST_COARSE_LOCATION = 1;
private static final int PUBLISH_YUNBA_INTERVAL = 5000; //云巴publish间隔,单位毫秒
private static final int PUBLISH_YUNBA_INTERVAL_SOS = 1000; //(紧急呼救)云巴publish间隔,单位毫秒
private Gson mGson = new Gson();

BluetoothManager btManager;
BluetoothAdapter btAdapter;
Expand All @@ -59,7 +69,7 @@ protected void onCreate(Bundle savedInstanceState) {
ButterKnife.bind(this);

initView();
init();
initBlutetooth();
//registerMessageReceiver();
}

Expand All @@ -70,12 +80,30 @@ protected void onResume() {
checkConfig();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();

if (id == R.id.action_settings) {
Intent intent = new Intent(this, YunbaSettingActivity.class);
startActivity(intent);
}

return super.onOptionsItemSelected(item);
}

private void initView() {
peripheralTextView.setMovementMethod(new ScrollingMovementMethod());
stopScanningButton.setVisibility(View.INVISIBLE);
}

private void init() {
private void initBlutetooth() {
btManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
btAdapter = btManager.getAdapter();
btScanner = btAdapter.getBluetoothLeScanner();
Expand All @@ -100,7 +128,7 @@ private void init() {
// builder.show();
// }

YunBaManager.start(this);

/*YunBaManager.subscribe(this, new String[]{"t1"}, new IMqttActionListener() {
@Override
Expand All @@ -118,24 +146,55 @@ public void onFailure(IMqttToken arg0, Throwable arg1) {
// 检查云巴 alias & topic是否已经配置
private void checkConfig() {
if (TextUtils.isEmpty(CacheUtil.getInstance().getYunbaAlias())
|| TextUtils.isEmpty(CacheUtil.getInstance().getYunbaTopic())) {
|| TextUtils.isEmpty(CacheUtil.getInstance().getAPID())) {
//TODO: need to config alias here
Log.e("Alias", "Alias or Topic is not configed");
Log.e("Alias", "Alias or Topic is not configured");
Intent intent = new Intent(this, YunbaSettingActivity.class);
startActivity(intent);
}
}

private void showBlueToothMsg() {
new AlertDialog.Builder(this)
.setTitle("请打开蓝牙")
.setMessage("请在设置中打开蓝牙")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_BLUETOOTH_SETTINGS);
startActivity(intent);
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}

@OnClick(R.id.StartScanButton)
public void startScanning(View view) {
System.out.println("start scanning");
peripheralTextView.setText("");
startScanningButton.setVisibility(View.INVISIBLE);
stopScanningButton.setVisibility(View.VISIBLE);
AsyncTask.execute(new Runnable() {
@Override
public void run() {
btScanner.startScan(leScanCallback);
if (null != btAdapter && btAdapter.isEnabled()) {
if (null == btScanner) {
btScanner = btAdapter.getBluetoothLeScanner();
}
});
if(null == btScanner) {
return;
}
System.out.println("start scanning");
peripheralTextView.setText("");
startScanningButton.setVisibility(View.INVISIBLE);
stopScanningButton.setVisibility(View.VISIBLE);
AsyncTask.execute(new Runnable() {
@Override
public void run() {
btScanner.startScan(leScanCallback);
}
});
} else {
showBlueToothMsg();
}
}

@OnClick(R.id.StopScanButton)
Expand All @@ -159,28 +218,38 @@ public void run() {
public void onScanResult(int callbackType, ScanResult result) {
String payload = toHexString(result.getScanRecord().getManufacturerSpecificData().valueAt(0));
String name = result.getDevice().getName();
int manufacturer = result.getScanRecord().getManufacturerSpecificData().keyAt(0);
int rssi = result.getRssi();

peripheralTextView.append("Name: " + name + " rssi: " + rssi + " : " + result.getScanRecord().getManufacturerSpecificData().keyAt(0) + "\n" + payload + "\n");
if (manufacturer != Constants.MANUFACTURER_DATA) {
return ;
}

if (peripheralTextView.getText().toString().length() > 1024) {
peripheralTextView.setText("");
peripheralTextView.scrollTo(0,0);
}
peripheralTextView.append("Name: " + name + " rssi: " + rssi + " md: " + manufacturer + "\n" + payload + "\n");
// auto scroll for text view
final int scrollAmount = peripheralTextView.getLayout().getLineTop(peripheralTextView.getLineCount()) - peripheralTextView.getHeight();
// if there is no need to scroll, scrollAmount will be <=0
if (scrollAmount > 0)
peripheralTextView.scrollTo(0, scrollAmount);

// 将扫描到的蓝牙信息publish到云巴
// 同一个设备连续扫描到会间隔5秒(PUBLISH_YUNBA_INTERVAL)再重复发送
// 同一个设备连续扫描到后publish到yunba会间隔5秒(PUBLISH_YUNBA_INTERVAL)再重复发送
// 如果是紧急呼救状态,间隔为1秒
if (bleCache.get(payload) != null) {
if ( (new Date().getTime() - bleCache.get(payload).getTimestamp().getTime()) > PUBLISH_YUNBA_INTERVAL) {
if ( (new Date().getTime() - bleCache.get(payload).getTimestamp().getTime()) > (isSOS(payload) ? PUBLISH_YUNBA_INTERVAL_SOS : PUBLISH_YUNBA_INTERVAL) ) {
bleCache.get(payload).setTimestamp(new Date());
publishToYunba(payload);
publishToYunba(payload, rssi);
} else {
//do nothing
Log.i(TAG, "payload already published:"+payload);
}
} else {
bleCache.put(payload,new BleInfo(name,rssi,payload,new Date()));
publishToYunba(payload);
publishToYunba(payload, rssi);
}
}
};
Expand All @@ -199,18 +268,27 @@ private String toHexString(byte[] bytes) {
return hexString.toString();
}

// 是否未紧急呼救
private boolean isSOS(String payload) {
return payload.startsWith(Constants.SOS_SIGNAL);
}

// 发送扫描到的蓝牙设备信息到云巴
private void publishToYunba(final String payload) {
private void publishToYunba(final String payload, int rssi) {
String alias = CacheUtil.getInstance().getYunbaAlias();
if(TextUtils.isEmpty(alias)) {
alias = ConfigUtil.getInstance().getYunAlias();
String apid = CacheUtil.getInstance().getAPID();
if(TextUtils.isEmpty(alias) || TextUtils.isEmpty(apid)) {
return;
}
Log.i(TAG,"ready to publish :" + alias + ":"+payload);
YunBaManager.publish2ToAlias(this, alias, payload, null,
YunbaPayload yunbaPayload = new YunbaPayload(apid, payload, rssi);

final String data = mGson.toJson(yunbaPayload);
Log.i(TAG,"ready to publish :" + alias + ":"+data);
YunBaManager.publish2ToAlias(this, alias, data, null,
new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
Log.i(TAG, "publish2 to alias succeed : " + payload);
Log.i(TAG, "publish2 to alias succeed : " + data);
}

@Override
Expand Down
36 changes: 36 additions & 0 deletions app/src/main/java/net/chaoc/blescanner/MyApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,25 @@
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import net.chaoc.blescanner.utils.CacheUtil;
import net.chaoc.blescanner.utils.ConfigUtil;

import org.eclipse.paho.client.mqttv3.IMqttActionListener;
import org.eclipse.paho.client.mqttv3.IMqttToken;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;

import io.yunba.android.manager.YunBaManager;

/**
* Created by yejun on 10/29/16.
* Copyright (C) 2016 qinyejun
Expand All @@ -36,6 +43,35 @@ public void onCreate() {
saveConfig();
//initLog();
//initImageLoader();

YunBaManager.start(this);
YunBaManager.subscribe(getApplicationContext(), new String[]{Constants.YUNBA_TOPIC}, new IMqttActionListener() {

@Override
public void onSuccess(IMqttToken arg0) {
Log.i(TAG, "Yunba: Subscribe topic succeed");
}

@Override
public void onFailure(IMqttToken arg0, Throwable arg1) {
Log.i(TAG, "Yunba: Subscribe topic failed");
}
});
String apid = CacheUtil.getInstance().getAPID();
if (!TextUtils.isEmpty(apid)) {
YunBaManager.setAlias(this, apid, new IMqttActionListener(){

@Override
public void onSuccess(IMqttToken iMqttToken) {
Log.i("Yunba", "Yunba: set Alias succeed");
}

@Override
public void onFailure(IMqttToken iMqttToken, Throwable throwable) {
Log.i("Yunba", "Yunba: set Alias failed");
}
});
}
}

public static Context getContext(){
Expand Down
Loading

0 comments on commit 8a7e496

Please sign in to comment.