From: snicker Date: Sat, 12 Sep 2020 04:53:08 +0000 (+0800) Subject: 给Mqtt协议添加蓝牙扫描功能。 扫描到蓝牙,切在区域内的话,就直接上报,否则进行正常定位扫描。 X-Git-Url: http://git.snicker.com.cn/?a=commitdiff_plain;h=10f134b04c7a645339f31e4c5c960895d88da6a9;p=protocol_srv.git 给Mqtt协议添加蓝牙扫描功能。 扫描到蓝牙,切在区域内的话,就直接上报,否则进行正常定位扫描。 --- diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index b3da421c..549c3107 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -41,6 +41,7 @@ + @@ -64,7 +65,6 @@ - diff --git a/app/src/main/java/com/enqualcomm/support/CoreService.java b/app/src/main/java/com/enqualcomm/support/CoreService.java index 9339130b..b6e26e86 100644 --- a/app/src/main/java/com/enqualcomm/support/CoreService.java +++ b/app/src/main/java/com/enqualcomm/support/CoreService.java @@ -573,6 +573,22 @@ public class CoreService extends Service { // ArrayList contactArray=new ArrayList(); // contactArray.add(new Contact(Contact.CATEGORY_SOS, "sos1", "07522312120")); // runtime.getDbHelper().saveContacts(contactArray,false,false,false,false,false,true); +// } + } + }else if (Config.custom_branch_name.equals("SGL")) { + if (Config.getConfigInitFlag(this)==0) { + Config.setLocationMode(this, CUSTOM_MOD8_GPS); + Config.setLocationCycle(this, 1800); + Config.setPPGTestCycle(this, 0); + Config.setTemperatureTestCycle(runtime, 0); + Config.setConfigInitFlag(this, 1); +// int contactcount=runtime.getDbHelper().getContactCount(getContentResolver()); +// ICLogger.i("=====Contact count "+contactcount); +// if(contactcount==0){ //如果没有SOS +// ICLogger.i("=====add SOS 07522312120 "); +// ArrayList contactArray=new ArrayList(); +// contactArray.add(new Contact(Contact.CATEGORY_SOS, "sos1", "07522312120")); +// runtime.getDbHelper().saveContacts(contactArray,false,false,false,false,false,true); // } } }else{ diff --git a/app/src/main/java/com/enqualcomm/support/LocationClient.java b/app/src/main/java/com/enqualcomm/support/LocationClient.java index 77cd7cc0..ca6222f6 100644 --- a/app/src/main/java/com/enqualcomm/support/LocationClient.java +++ b/app/src/main/java/com/enqualcomm/support/LocationClient.java @@ -9,6 +9,7 @@ import android.os.Message; import android.os.PowerManager; import android.os.SystemClock; +import com.enqualcomm.support.adapter.BTHelper; import com.enqualcomm.support.utils.AlarmHelper; import com.enqualcomm.support.adapter.LBSHelper; import com.enqualcomm.support.utils.Connection; @@ -24,6 +25,7 @@ import com.enqualcomm.support.utils.SimSignalUtil; import com.enqualcomm.support.bean.Runtime; import com.enqualcomm.support.utils.TimeUtil; +import java.util.ArrayList; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -90,7 +92,8 @@ public class LocationClient { private LBSHelper mLbsHelper; - + private static BTHelper btHelper=null; + private int mPedometer2; private SimSignalUtil mSignalUtil; @@ -117,7 +120,37 @@ public class LocationClient { public LocationClient(Connection paramNettyConnection, Runtime paramRuntime) { connection = paramNettyConnection; runtime = paramRuntime; + btHelper=BTHelper.getInstance(paramRuntime.getContextProxy().context, new BTHelper.BTScanCallback() { + @Override + public void OnScanFinished(ArrayList btPairedScanResult, ArrayList btNewScanResult) { + + if (btNewScanResult.size() > 0) { + ICLogger.i("found bt mac " + btNewScanResult.size()); + boolean in_area = false; + for (String mac : btNewScanResult) { + if (runtime.bleInfo.btMacList.contains(mac)) { + in_area = true; + break; + } + runtime.bleInfo.is_in_area = in_area; + } + + } else { + runtime.bleInfo.is_in_area = false; + } + ICLogger.i("Dev Is in Area " + runtime.bleInfo.is_in_area); + + if (runtime.bleInfo.is_in_area == false) { + ICLogger.i("不在区域内,开启定位流程"); + startLocationProcess(runtime.getLocationInstance().getWorkingMod()); + }else{ + ICLogger.i("在区域内,直接发送区域坐标即可!"); + StringMessage AP01_CMD = protocolAPIHelper.getGPSLocationMSG(runtime); + Deliver.sendLocationMSG(AP01_CMD); + } + } + }); gLocationCline=this; //GPS 监听管理初始化 this.nmeaListener = new NMEAListener(paramRuntime.getContextProxy().getLocationManager(), paramRuntime); @@ -286,7 +319,14 @@ public class LocationClient { public void call(Object param1Object) { waitAndSendLocation(); } }); }else if (msg.what == msgWhat_StartLocationProcess){ - startLocationProcess(runtime.getLocationInstance().getWorkingMod()); + if (runtime.bleInfo.btMacList.size()>0){ + //有蓝牙搜索要求则 先进行蓝牙扫描 + btHelper.scanBLE(); + }else{ + //没有蓝牙搜索要求,就正常定位 + startLocationProcess(runtime.getLocationInstance().getWorkingMod()); + } + }else if (msg.what == msgWhat_StartSOSLocationProcess){ startSOSLocation=true;//启动SOS定位,为了快速响应,只要wifi定位即可。 forceLocation=1; @@ -562,6 +602,8 @@ public void updateCellInfo() { private static int gpsTimeoutCountOnNoMove=0;// 记录gps 无信号或者timeout的次数,超过5 就会停止使用gps定位,只要有一次 移动,或者设置为wifi定位,则清零。 private static final int MaxGpsTimeoutCountOnNoMove=5;//不动的情况最大的gps搜不到星的次数。 private static boolean skipMoveCheck=false; + + public static void setSkipMoveCheck(boolean _skipMoveCheck){ skipMoveCheck=_skipMoveCheck; } @@ -571,10 +613,11 @@ public void updateCellInfo() { private static boolean NoMoveAndNoGPSsignalSkipGpsFlag=false; private static boolean noMove=false; - public void startLocationProcess(int mod){ + public void startLocationProcess(int mod){ + updateStepCount();//update StepCount,保证每次定位前,计步数据是最新的。防止丢数据。 ICLogger.i("forceLocation="+forceLocation+", WorkingMod="+mod+", isLocating="+isLocating+", lastStep="+runtime.getLastStepCount()+", currentStep="+ runtime.getTotalStepCount()); if (isLocating && 1!=forceLocation) { //如果是强制定位,则即使是正在定位,也要执行,因为有时模式变换的时候,需要取消上一次定位信息,马上新的模式定位。 diff --git a/app/src/main/java/com/enqualcomm/support/MainActivity.java b/app/src/main/java/com/enqualcomm/support/MainActivity.java index fba1c926..69d1ce1a 100644 --- a/app/src/main/java/com/enqualcomm/support/MainActivity.java +++ b/app/src/main/java/com/enqualcomm/support/MainActivity.java @@ -17,6 +17,7 @@ import android.view.View; import android.widget.Button; import android.widget.TextView; +import com.enqualcomm.support.adapter.BTHelper; import com.enqualcomm.support.utils.DeviceInfoUtil; import com.enqualcomm.support.utils.ICLogger; import com.enqualcomm.support.utils.ImageUtil; @@ -27,6 +28,7 @@ import com.enqualcomm.support.utils.Util; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; +import java.util.ArrayList; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; @@ -59,6 +61,8 @@ public class MainActivity extends Activity { return false; } }); + + BTHelper btHelper=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -73,11 +77,14 @@ public class MainActivity extends Activity { tbtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { + Intent intent=new Intent(); ComponentName var3 = new ComponentName("com.enqualcomm.support", "com.enqualcomm.support.toolActivity"); intent.addFlags(FLAG_ACTIVITY_NEW_TASK); intent.setComponent(var3); startActivity(intent); + + } }); @@ -115,6 +122,26 @@ public class MainActivity extends Activity { // testpic(); } + + private void testBLE(){ + btHelper= BTHelper.getInstance(MainActivity.this, new BTHelper.BTScanCallback() { + @Override + public void OnScanFinished(ArrayList btPairedScanResult, ArrayList btNewScanResult) { + + +// ICLogger.i("BT Pared "+btPairedScanResult.size()); +// for ( String i :btPairedScanResult){ +// ICLogger.i(i); +// } + int count=btNewScanResult.size(); + ICLogger.i("BT New "+count); + for(int i=0;i mPairedDevicesArray=new ArrayList<>(); + private ArrayList mNewDevicesArray=new ArrayList<>(); + private Context mContext=null; + private BTScanCallback mCallback=null; + private static BTHelper mBThelper=null; + + private boolean enabledBeforScan=false; + BTHelper(){ + + } + + //enableBT + public void enableBT() { + ICLogger.i( "BT enableBT()"); + if (!mBtAdapter.isEnabled()) { +// Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); +// mContext.startActivity(enableIntent); + + mBtAdapter.enable(); + try { + Thread.sleep(2000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + //此处无法执行说没权限 +// mBtAdapter.enableNoAutoConnect(); + } + } + + //disableBT + public void disableBT(){ + ICLogger.i( "BT disableBT()"); + if (mBtAdapter.isEnabled()) { + mBtAdapter.disable(); +// Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); +// mContext.startActivity(enableIntent); + } + } + + //初始化 BTHlper + public void Init(Context context , BTScanCallback callback){ + ICLogger.i( "BT Init()"); + mCallback=callback; + mContext=context; +// // Register for broadcasts when a device is discovered +// IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); +// mContext.registerReceiver(mReceiver, filter); +// +// // Register for broadcasts when discovery has finished +// filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); +// mContext.registerReceiver(mReceiver, filter); +// + + // Get the local Bluetooth adapter + mBtAdapter = BluetoothAdapter.getDefaultAdapter(); + + } + + //获取实例 + public static BTHelper getInstance(Context context , BTScanCallback callback){ + if (mBThelper==null) { + ICLogger.i( "BT getInstance "); + mBThelper = new BTHelper(); + mBThelper.Init(context,callback); + } + return mBThelper; + } + +// // The BroadcastReceiver that listens for discovered devices and +// // changes the title when discovery is finished +// private final BroadcastReceiver mReceiver = new BroadcastReceiver() { +// @Override +// public void onReceive(Context context, Intent intent) { +// String action = intent.getAction(); +// ICLogger.i( "BT onReceive "+action); +// // When discovery finds a device +// if (BluetoothDevice.ACTION_FOUND.equals(action)) { +// // Get the BluetoothDevice object from the Intent +// BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); +// // If it's already paired, skip it, because it's been listed already +// +// ICLogger.i("BT "+device.getName() + "\t" + device.getAddress()); +// +//// ICLogger.i( device.getName() + "\n" + device.getAddress()); +// if (device.getBondState() != BluetoothDevice.BOND_BONDED) { +// if(!mNewDevicesArray.contains(device.getAddress())) +// mNewDevicesArray.add(device.getAddress()); +// } +// // When discovery is finished, change the Activity title +// } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { +// +// if (mNewDevicesArray.size() == 0) { +// //没有设备 +// } +// // Get a set of currently paired devices +// Set pairedDevices = mBtAdapter.getBondedDevices(); +// +// // If there are paired devices, add each one to the ArrayAdapter +// if (pairedDevices.size() > 0) { +// for (BluetoothDevice device : pairedDevices) { +// ICLogger.i("BT "+device.getName() + "\t" + device.getAddress()); +// if(!mPairedDevicesArray.contains(device.getAddress())) +// mPairedDevicesArray.add(device.getAddress()); +// +// } +// } +// +// //开始扫描前是关闭的,就关闭它 +// if(!enabledBeforScan) +// disableBT(); +// +// if (mCallback!=null) +// mCallback.OnScanFinished(mPairedDevicesArray,mNewDevicesArray); +// } +// } +// }; + + //清除BThelper +// public void Deinit() { +// ICLogger.i( "BT Deinit()"); +// // Make sure we're not doing discovery anymore +//// if (mBtAdapter != null) { +//// mBtAdapter.cancelDiscovery(); +//// } +//// +//// // Unregister broadcast listeners +//// if(mContext!=null) +//// mContext.unregisterReceiver(mReceiver); +//// +// +// +//// mContext=null; +////// mCallback=null; +// +// } + + //开始扫描BLE + public void scanBLE() { + ICLogger.i( "BT doDiscovery()"); + + + enabledBeforScan=mBtAdapter.isEnabled(); + + enableBT(); + + + // If we're already discovering, stop it + +// ICLogger.i(" mBtAdapter.isDiscovering() "+mBtAdapter.isDiscovering()); +// if (mBtAdapter.isDiscovering()) { +// mBtAdapter.cancelDiscovery(); +// } + +// mBtAdapter.setDiscoverableTimeout(3); + mPairedDevicesArray.clear(); + mNewDevicesArray.clear(); + // Request discover from BluetoothAdapter + + + +// mBtAdapter.startDiscovery(); + + + + + + mBtAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_NONE,5); + LeScanCallback lecallback=new LeScanCallback(){ + + @Override + public void onLeScan(BluetoothDevice bluetoothDevice, int rssi, byte[] scanRecode) { + ICLogger.i( "BT onLeScan "+bluetoothDevice.getName()+" "+bluetoothDevice.getAddress()+" rssi "+rssi); + ICLogger.logToFile("LBS","BT onLeScan "+bluetoothDevice.getName()+" "+bluetoothDevice.getAddress()+" rssi "+rssi ); + if(!mNewDevicesArray.contains(bluetoothDevice.getAddress())) + mNewDevicesArray.add(bluetoothDevice.getAddress()); + } + }; + + ICLogger.i("BT startLeScan"); + mBtAdapter.startLeScan(lecallback); + + + new Handler().postDelayed(new Runnable() { + @Override + public void run() { + mBtAdapter.stopLeScan(lecallback); + ICLogger.i("BT stopLeScan"); + + //开始扫描前是关闭的,就关闭它 + if(!enabledBeforScan) + disableBT(); + + if (mCallback!=null) + mCallback.OnScanFinished(mPairedDevicesArray,mNewDevicesArray); + } + },5000); + + + } + + + + //开启蓝牙可被搜索300秒 + private void ensureDiscoverable() { + ICLogger.i("ensure discoverable"); + if (mBtAdapter.getScanMode() != + BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) { + +// Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); +// discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300); +// mContext.startActivity(discoverableIntent); + } + } + + private advcallback advcb=new advcallback(); + + //停止自定义广播 + public void stopGattAdvertising(){ + if (mBtAdapter.isEnabled()) + mBtAdapter.stopAdvertising(advcb); + + } + + //开启自定义广播数据,前两字会被丢弃 + public void enableGattAdvertising(String serverData){ + byte[] serviceData = serverData.getBytes(); + if (mBtAdapter.isEnabled()) + mBtAdapter.startAdvertising(advcb, serviceData); + } + public class advcallback implements BluetoothAdapter.AdvertiseCallback { + + + public void onAdvertiseStart(int status){ + ICLogger.i( "--onAdvertiseStart --"); + + } + + public void onAdvertiseStop(int status){ + + ICLogger.i( "-- onAdvertiseStop --"); + + } + } + public interface BTScanCallback{ + void OnScanFinished(ArrayList btPairedScanResult,ArrayList btNewScanResult); + + } +} diff --git a/app/src/main/java/com/enqualcomm/support/bean/Runtime.java b/app/src/main/java/com/enqualcomm/support/bean/Runtime.java index 8addf48a..073e5c16 100644 --- a/app/src/main/java/com/enqualcomm/support/bean/Runtime.java +++ b/app/src/main/java/com/enqualcomm/support/bean/Runtime.java @@ -609,7 +609,44 @@ public class Runtime { return sb.toString(); } + public String getMqttGPSData(){ +/*** + * A,22.570733,N,113.8626083,E,0.00,249.5,0.0,6 + * A,gps 定位有效 + * 22.570733,纬度 N, + * 113.8626083,经度 E, + * 0.00,速度 + * 249.5,方向 + * 0.0,海拔 + * 6,卫星个数 + */ + StringBuilder sb=new StringBuilder(); + int speedi=(int)gpsp.getLocation().speed; + int speedl=(int) ((gpsp.getLocation().speed - speedi)*100); + + String latStr=gpsp.getLocation().latGPSStr; + String lonStr=gpsp.getLocation().lonGPSStr; + + if(bleInfo.is_in_area==true){ + latStr=bleInfo.gps_n; + lonStr=bleInfo.gps_e; + } + String heading=String.valueOf(gpsp.getLocation().heading); + int starcount=gpsp.getLocation().used_gps_numbs; + if (isGPSFixed()|| bleInfo.is_in_area) + { + String gpsStr = String.format(",%s,N,%s,E,%d.%02d,%s,0.0,%d", latStr, lonStr, speedi, speedl,heading,starcount); + sb.append("A").append(gpsStr); + + }else{ + String gpsStr=String.format(",0.000,N,0.000,E,0.0,0.0,0.0,0"); + sb.append("V").append(gpsStr); + } + + + return sb.toString(); + } public String getFZDGPSData(){ /*** diff --git a/app/src/main/java/com/ic/protocols/protocol_mqtt.java b/app/src/main/java/com/ic/protocols/protocol_mqtt.java index 10ac35ed..db00ffc5 100644 --- a/app/src/main/java/com/ic/protocols/protocol_mqtt.java +++ b/app/src/main/java/com/ic/protocols/protocol_mqtt.java @@ -309,7 +309,7 @@ public class protocol_mqtt { String cmd = cmdMap.get(CMD_TYPE.UD); sb.append(TimeUtil.getUTCSystemDateYYMMDD()).append(CMDSPLIT) .append(TimeUtil.getUTCSystemTimeHHmmss()).append(CMDSPLIT) - .append(runtime.getFZDGPSData()).append(CMDSPLIT) + .append(runtime.getMqttGPSData()).append(CMDSPLIT) .append(String.format("%03d", abs(runtime.getSignalStrength()))).append(CMDSPLIT) .append(runtime.getBattery()).append(CMDSPLIT) .append(runtime.getTotalStepCount()).append(CMDSPLIT)