|
@@ -3,7 +3,10 @@ package com.wdkl.app.ncs.callingbed.bt_gateway;
|
|
|
import android.app.Service;
|
|
|
import android.bluetooth.BluetoothAdapter;
|
|
|
import android.bluetooth.BluetoothDevice;
|
|
|
+import android.content.BroadcastReceiver;
|
|
|
+import android.content.Context;
|
|
|
import android.content.Intent;
|
|
|
+import android.content.IntentFilter;
|
|
|
import android.os.Binder;
|
|
|
import android.os.Build;
|
|
|
import android.os.IBinder;
|
|
@@ -26,7 +29,7 @@ import static com.wdkl.app.ncs.callingbed.bt_gateway.BluetoothUtil.bytesToHex;
|
|
|
|
|
|
public class BluetoothService extends Service implements BeaconConsumer, SocketConnect.OnConnectStateListener, SocketConnect.OnSocketReadListener {
|
|
|
|
|
|
- private String TAG = BluetoothService.class.getSimpleName();
|
|
|
+ private static String TAG = BluetoothService.class.getSimpleName();
|
|
|
private BluetoothAdapter bluetoothAdapter;
|
|
|
|
|
|
private static final int DATA_TYPE_FLAGS = 0x01;
|
|
@@ -72,6 +75,8 @@ public class BluetoothService extends Service implements BeaconConsumer, SocketC
|
|
|
private long time = 0;
|
|
|
NotificationUtil notificationUtil;
|
|
|
|
|
|
+ private DataParse.OnDataCallback dataCallback;
|
|
|
+ private BluetoothStateBroadcastReceive mReceive;
|
|
|
|
|
|
@Override
|
|
|
public IBinder onBind(Intent intent) {
|
|
@@ -83,10 +88,13 @@ public class BluetoothService extends Service implements BeaconConsumer, SocketC
|
|
|
return BluetoothService.this;
|
|
|
}
|
|
|
|
|
|
- public void setdata(String data) {
|
|
|
+ public void setData(String data) {
|
|
|
sendData(data);
|
|
|
}
|
|
|
|
|
|
+ public void setOnSignDataCallback(DataParse.OnDataCallback callback) {
|
|
|
+ dataCallback = callback;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|
|
@@ -101,33 +109,13 @@ public class BluetoothService extends Service implements BeaconConsumer, SocketC
|
|
|
startForeground(notificationUtil.notificationId, notificationUtil.getBuilder().build());
|
|
|
//启动蓝牙扫描
|
|
|
this.bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
|
|
|
- if (!this.bluetoothAdapter.isEnabled()) {
|
|
|
+ if (this.bluetoothAdapter.isEnabled()) {
|
|
|
+ startScan();
|
|
|
+ } else {
|
|
|
+ //打开蓝牙
|
|
|
this.bluetoothAdapter.enable();
|
|
|
+ registerBluetoothReceiver();
|
|
|
}
|
|
|
-// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
|
|
-// bluetoothAdapter.getBluetoothLeScanner()
|
|
|
-// .startScan(Collections.singletonList(new ScanFilter.Builder().build()), new ScanSettings.Builder().build(), new BluetoothAdapter().getBluetoothLeScanner());
|
|
|
-// }
|
|
|
- this.bluetoothAdapter.startLeScan(new BluetoothAdapter.LeScanCallback() {
|
|
|
- @Override
|
|
|
- public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
|
|
|
- time = System.currentTimeMillis();
|
|
|
-
|
|
|
- localName = "";
|
|
|
- ibeaconName = "";
|
|
|
- address = "";
|
|
|
- uuid = "";
|
|
|
- majors = "";
|
|
|
- minors = "";
|
|
|
- rssis = "";
|
|
|
- distances = "";
|
|
|
- txPowerLevel = Integer.MIN_VALUE;
|
|
|
-
|
|
|
-// customParseData(device, rssi, scanRecord);//自定义解析数据 给蓝牙信标使用
|
|
|
- parseData(device, rssi, scanRecord);//标准蓝牙协议解析数据
|
|
|
-
|
|
|
- }
|
|
|
- });
|
|
|
|
|
|
//ip = "47.115.176.250";
|
|
|
//port = 3006;
|
|
@@ -150,6 +138,44 @@ public class BluetoothService extends Service implements BeaconConsumer, SocketC
|
|
|
|
|
|
}
|
|
|
|
|
|
+ private void registerBluetoothReceiver(){
|
|
|
+ if(mReceive == null){
|
|
|
+ mReceive = new BluetoothStateBroadcastReceive();
|
|
|
+ }
|
|
|
+ IntentFilter intentFilter = new IntentFilter();
|
|
|
+ intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
|
|
|
+ intentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
|
|
|
+ intentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
|
|
|
+ intentFilter.addAction("android.bluetooth.BluetoothAdapter.STATE_OFF");
|
|
|
+ intentFilter.addAction("android.bluetooth.BluetoothAdapter.STATE_ON");
|
|
|
+ registerReceiver(mReceive, intentFilter);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void startScan() {
|
|
|
+ if (bluetoothAdapter != null) {
|
|
|
+ bluetoothAdapter.startLeScan(new BluetoothAdapter.LeScanCallback() {
|
|
|
+ @Override
|
|
|
+ public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
|
|
|
+ time = System.currentTimeMillis();
|
|
|
+
|
|
|
+ localName = "";
|
|
|
+ ibeaconName = "";
|
|
|
+ address = "";
|
|
|
+ uuid = "";
|
|
|
+ majors = "";
|
|
|
+ minors = "";
|
|
|
+ rssis = "";
|
|
|
+ distances = "";
|
|
|
+ txPowerLevel = Integer.MIN_VALUE;
|
|
|
+
|
|
|
+ //customParseData(device, rssi, scanRecord);//自定义解析数据 给蓝牙信标使用
|
|
|
+ parseData(device, rssi, scanRecord);//标准蓝牙协议解析数据
|
|
|
+
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
|
|
Log.e(TAG, "onStartCommand....");
|
|
@@ -250,8 +276,7 @@ public class BluetoothService extends Service implements BeaconConsumer, SocketC
|
|
|
break;
|
|
|
case DATA_TYPE_LOCAL_NAME_SHORT:
|
|
|
case DATA_TYPE_LOCAL_NAME_COMPLETE:
|
|
|
- localName = new String(
|
|
|
- BluetoothUtil.extractBytes(scanRecord, currentPos, dataLength));
|
|
|
+ localName = new String(BluetoothUtil.extractBytes(scanRecord, currentPos, dataLength));
|
|
|
break;
|
|
|
case DATA_TYPE_TX_POWER_LEVEL:
|
|
|
txPowerLevel = scanRecord[currentPos];
|
|
@@ -285,6 +310,9 @@ public class BluetoothService extends Service implements BeaconConsumer, SocketC
|
|
|
dataStringList.add(data);
|
|
|
Log.e(TAG,"发送的数据 mac:"+device.getAddress());
|
|
|
Log.e(TAG, "发送的数据 " + data);
|
|
|
+ if (dataCallback != null) {
|
|
|
+ dataCallback.onData(data);
|
|
|
+ }
|
|
|
// sendData(data);
|
|
|
// String SS ="3418AA550000002B0000010000DB5755";
|
|
|
new Thread(new Runnable() {
|
|
@@ -365,26 +393,18 @@ public class BluetoothService extends Service implements BeaconConsumer, SocketC
|
|
|
|
|
|
@Override
|
|
|
public void onSocketRead(SocketConnect install, byte[] bytes) {
|
|
|
-// String data = new String(bytes);
|
|
|
|
|
|
- String data = bytesToHex(bytes);
|
|
|
+ /*String data = bytesToHex(bytes);
|
|
|
Log.e(TAG, "返回的数据data " + data);
|
|
|
Log.e(TAG, "返回的数据 dd " + (System.currentTimeMillis() - time) / 1000);
|
|
|
if (time == 0) return;
|
|
|
if ((System.currentTimeMillis() - time) / 1000 >= 2) {
|
|
|
time = 0;
|
|
|
-
|
|
|
-// 直接启动Activity也可以
|
|
|
-// Intent intent = new Intent(getApplication(),MainActivity.class);
|
|
|
-// intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
-// startActivity(intent);
|
|
|
-
|
|
|
baxcSocketConnectLine.release();
|
|
|
notificationUtil.setProgres(notificationUtil.progressMax + 5);
|
|
|
|
|
|
EventBus.getDefault().post(new MessageEvent("bt", Constant.EVENT_RESTART_BT));
|
|
|
- }
|
|
|
-
|
|
|
+ }*/
|
|
|
|
|
|
}
|
|
|
|
|
@@ -415,5 +435,32 @@ public class BluetoothService extends Service implements BeaconConsumer, SocketC
|
|
|
// }
|
|
|
bluetoothService = null;
|
|
|
|
|
|
+ if(mReceive != null){
|
|
|
+ unregisterReceiver(mReceive);
|
|
|
+ mReceive = null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private class BluetoothStateBroadcastReceive extends BroadcastReceiver {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onReceive(Context context, Intent intent) {
|
|
|
+ String action = intent.getAction();
|
|
|
+ switch (action){
|
|
|
+ case BluetoothDevice.ACTION_ACL_CONNECTED:
|
|
|
+ case BluetoothDevice.ACTION_ACL_DISCONNECTED:
|
|
|
+ break;
|
|
|
+ case BluetoothAdapter.ACTION_STATE_CHANGED:
|
|
|
+ int blueState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0);
|
|
|
+ if (blueState == BluetoothAdapter.STATE_OFF) {
|
|
|
+ Log.e(TAG, "蓝牙已关闭");
|
|
|
+ } else if (blueState == BluetoothAdapter.STATE_ON) {
|
|
|
+ Log.d(TAG, "蓝牙已打开");
|
|
|
+ startScan();
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|