From 17d97f58265510ceda0b38959a90424ee07b6910 Mon Sep 17 00:00:00 2001
From: 陈奇 <1650699704@qq.com>
Date: Tue, 13 Nov 2018 11:42:07 +0800
Subject: [PATCH] 1.0.3-1测试版本 [*]修改重新连接的方式 [*]修改商城连接 [*]修改心跳机制 [*]主要针对oppo和vivo机型添加开启定位服务

---
 FastBleLib/src/main/java/com/clj/fastble/bluetooth/BleBluetooth.java |   49 +++++++++----------------------------------------
 1 files changed, 9 insertions(+), 40 deletions(-)

diff --git a/FastBleLib/src/main/java/com/clj/fastble/bluetooth/BleBluetooth.java b/FastBleLib/src/main/java/com/clj/fastble/bluetooth/BleBluetooth.java
index 003c7af..a0abb9a 100644
--- a/FastBleLib/src/main/java/com/clj/fastble/bluetooth/BleBluetooth.java
+++ b/FastBleLib/src/main/java/com/clj/fastble/bluetooth/BleBluetooth.java
@@ -10,7 +10,6 @@
 import android.os.Handler;
 import android.os.Looper;
 import android.text.TextUtils;
-import android.util.Log;
 
 import com.clj.fastble.conn.BleConnector;
 import com.clj.fastble.conn.BleGattCallback;
@@ -45,7 +44,6 @@
     private Context context;
     private BluetoothAdapter bluetoothAdapter;
     private BluetoothGatt bluetoothGatt;
-    private int mtu = 20;
     private Handler handler = new Handler(Looper.getMainLooper());
     private HashMap<String, BluetoothGattCallback> callbackHashMap = new HashMap<>();
     private PeriodScanCallback periodScanCallback;
@@ -58,14 +56,11 @@
         bluetoothAdapter = bluetoothManager.getAdapter();
     }
 
-    public void setMtu(int mtu) {
-        this.mtu = mtu;
-    }
-
 
     public BleConnector newBleConnector() {
         return new BleConnector(this);
     }
+
 
     public boolean isInScanning() {
         return connectionState == STATE_SCANNING;
@@ -144,8 +139,7 @@
                 + "\nmac: " + scanResult.getDevice().getAddress()
                 + "\nautoConnect: " + autoConnect);
         addConnectGattCallback(callback);
-        bluetoothGatt = scanResult.getDevice().connectGatt(context, autoConnect, coreGattCallback);
-        return bluetoothGatt;
+        return scanResult.getDevice().connectGatt(context, autoConnect, coreGattCallback);
     }
 
     public void scanNameAndConnect(String name, long time_out, final boolean autoConnect, final BleGattCallback callback) {
@@ -344,21 +338,6 @@
         return connectionState;
     }
 
-
-    public static String dumpHex(byte[] src) {
-        String num = "0123456789ABCDEF";
-        StringBuilder sb = new StringBuilder();
-        sb.append("[ ");
-        for (byte aSrc : src) {
-            int high = aSrc >> 4 & 0x0f;
-            int low = aSrc & 0x0f;
-            sb.append(num.charAt(high)).append(num.charAt(low)).append(" ");
-        }
-        sb.append(" ]");
-
-        return sb.toString();
-    }
-
     private BleGattCallback coreGattCallback = new BleGattCallback() {
 
         @Override
@@ -368,9 +347,8 @@
 
         @Override
         public void onConnecting(BluetoothGatt gatt, int status) {
-            BleLog.i("BleGattCallback���onConnecting ");
+            BleLog.i("BleGattCallback���onConnectSuccess ");
             bluetoothGatt = gatt;
-
             Iterator iterator = callbackHashMap.entrySet().iterator();
             while (iterator.hasNext()) {
                 Map.Entry entry = (Map.Entry) iterator.next();
@@ -384,6 +362,7 @@
         @Override
         public void onConnectSuccess(BluetoothGatt gatt, int status) {
             BleLog.i("BleGattCallback���onConnectSuccess ");
+
             bluetoothGatt = gatt;
             Iterator iterator = callbackHashMap.entrySet().iterator();
             while (iterator.hasNext()) {
@@ -399,6 +378,7 @@
         @Override
         public void onDisConnected(BluetoothGatt gatt, int status, BleException exception) {
             BleLog.i("BleGattCallback���onConnectFailure ");
+
             closeBluetoothGatt();
             bluetoothGatt = null;
             Iterator iterator = callbackHashMap.entrySet().iterator();
@@ -422,7 +402,6 @@
                     + '\n' + "status: " + status
                     + '\n' + "newState: " + newState
                     + '\n' + "currentThread: " + Thread.currentThread().getId());
-
             if (newState == BluetoothGatt.STATE_CONNECTED) {
                 connectionState = STATE_CONNECTED;
                 onConnectSuccess(gatt, status);
@@ -448,17 +427,18 @@
 
         @Override
         public void onServicesDiscovered(BluetoothGatt gatt, int status) {
-            BleLog.i("BleGattCallback���onServicesDiscovered " + mtu);
+            BleLog.i("BleGattCallback���onServicesDiscovered ");
+
             connectionState = STATE_SERVICES_DISCOVERED;
             Iterator iterator = callbackHashMap.entrySet().iterator();
             while (iterator.hasNext()) {
                 Map.Entry entry = (Map.Entry) iterator.next();
                 Object call = entry.getValue();
                 if (call instanceof BluetoothGattCallback) {
+                    gatt.requestMtu(64);
                     ((BluetoothGattCallback) call).onServicesDiscovered(gatt, status);
                 }
             }
-            bluetoothGatt.requestMtu(mtu);
         }
 
         @Override
@@ -490,7 +470,7 @@
 
         @Override
         public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
-            BleLog.i("BleGattCallback���onCharacteristicChanged " + characteristic.getValue().length);
+            BleLog.i("BleGattCallback���onCharacteristicChanged ");
             Iterator iterator = callbackHashMap.entrySet().iterator();
             while (iterator.hasNext()) {
                 Map.Entry entry = (Map.Entry) iterator.next();
@@ -554,17 +534,6 @@
                 if (call instanceof BluetoothGattCallback) {
                     ((BluetoothGattCallback) call).onReadRemoteRssi(gatt, rssi, status);
                 }
-            }
-        }
-
-        @Override
-        public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
-            super.onMtuChanged(gatt, mtu, status);
-            Log.d("BLE", "onMtuChanged mtu=" + mtu + ",status=" + status);
-
-            System.out.println("onMtuChanged-------------------->size:" + mtu);
-            if (status == 0) {
-                System.out.println("onMtuChanged-------------------->������������");
             }
         }
     };

--
Gitblit v1.8.0