From 3bd6e9023e011336da840e11a9d48bb31c7a0261 Mon Sep 17 00:00:00 2001 From: 陈奇 <1650699704@qq.com> Date: Wed, 07 Nov 2018 09:31:59 +0800 Subject: [PATCH] [*]将电磁版本的唤醒心跳改成两秒,唤醒之后回到30一次心跳 [*]整体版本页面显示的绝对安全换成了安全 [*]自定义语音修改和优化,添加了无声的选择 --- FastBleLib/src/main/java/com/clj/fastble/bluetooth/BleBluetooth.java | 50 +++++++++++++++++++++++++++++++++++++++----------- 1 files changed, 39 insertions(+), 11 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 047d288..003c7af 100644 --- a/FastBleLib/src/main/java/com/clj/fastble/bluetooth/BleBluetooth.java +++ b/FastBleLib/src/main/java/com/clj/fastble/bluetooth/BleBluetooth.java @@ -10,6 +10,7 @@ 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; @@ -44,6 +45,7 @@ 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; @@ -56,11 +58,14 @@ 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; @@ -139,7 +144,8 @@ + "\nmac: " + scanResult.getDevice().getAddress() + "\nautoConnect: " + autoConnect); addConnectGattCallback(callback); - return scanResult.getDevice().connectGatt(context, autoConnect, coreGattCallback); + bluetoothGatt = scanResult.getDevice().connectGatt(context, autoConnect, coreGattCallback); + return bluetoothGatt; } public void scanNameAndConnect(String name, long time_out, final boolean autoConnect, final BleGattCallback callback) { @@ -338,6 +344,21 @@ 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 @@ -347,9 +368,9 @@ @Override public void onConnecting(BluetoothGatt gatt, int status) { - BleLog.i("BleGattCallback���onConnectSuccess "); - + BleLog.i("BleGattCallback���onConnecting "); bluetoothGatt = gatt; + Iterator iterator = callbackHashMap.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry entry = (Map.Entry) iterator.next(); @@ -363,7 +384,6 @@ @Override public void onConnectSuccess(BluetoothGatt gatt, int status) { BleLog.i("BleGattCallback���onConnectSuccess "); - bluetoothGatt = gatt; Iterator iterator = callbackHashMap.entrySet().iterator(); while (iterator.hasNext()) { @@ -379,7 +399,6 @@ @Override public void onDisConnected(BluetoothGatt gatt, int status, BleException exception) { BleLog.i("BleGattCallback���onConnectFailure "); - closeBluetoothGatt(); bluetoothGatt = null; Iterator iterator = callbackHashMap.entrySet().iterator(); @@ -429,8 +448,7 @@ @Override public void onServicesDiscovered(BluetoothGatt gatt, int status) { - BleLog.i("BleGattCallback���onServicesDiscovered "); - + BleLog.i("BleGattCallback���onServicesDiscovered " + mtu); connectionState = STATE_SERVICES_DISCOVERED; Iterator iterator = callbackHashMap.entrySet().iterator(); while (iterator.hasNext()) { @@ -440,6 +458,7 @@ ((BluetoothGattCallback) call).onServicesDiscovered(gatt, status); } } + bluetoothGatt.requestMtu(mtu); } @Override @@ -459,7 +478,6 @@ @Override public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { BleLog.i("BleGattCallback���onCharacteristicWrite "); - Iterator iterator = callbackHashMap.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry entry = (Map.Entry) iterator.next(); @@ -472,8 +490,7 @@ @Override public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { - BleLog.i("BleGattCallback���onCharacteristicChanged "); - + BleLog.i("BleGattCallback���onCharacteristicChanged " + characteristic.getValue().length); Iterator iterator = callbackHashMap.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry entry = (Map.Entry) iterator.next(); @@ -539,5 +556,16 @@ } } } + + @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