From 73c528bf5ee7713ed749331a2de54da739527eb2 Mon Sep 17 00:00:00 2001 From: pw <329803553@qq.com> Date: Mon, 19 Nov 2018 19:44:44 +0800 Subject: [PATCH] fix some connection problems --- .../com/ficat/easyble/gatt/BleGattImpl.java | 258 ++++++++++-------- .../java/com/ficat/sample/MainActivity.java | 10 +- .../sample/adapter/ScanDeviceAdapter.java | 2 +- .../common/CommonRecyclerViewAdapter.java | 10 +- 4 files changed, 148 insertions(+), 132 deletions(-) rename sample/src/main/java/com/ficat/sample/{ => adapter}/common/CommonRecyclerViewAdapter.java (93%) diff --git a/easyble/src/main/java/com/ficat/easyble/gatt/BleGattImpl.java b/easyble/src/main/java/com/ficat/easyble/gatt/BleGattImpl.java index ff8355d..9426a4c 100644 --- a/easyble/src/main/java/com/ficat/easyble/gatt/BleGattImpl.java +++ b/easyble/src/main/java/com/ficat/easyble/gatt/BleGattImpl.java @@ -77,128 +77,120 @@ public BleGattImpl(@NonNull Context context) { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { super.onConnectionStateChange(gatt, status, newState); - String address = gatt.getDevice().getAddress(); - Map.Entry entry = findMapEntry(mConnectCallbackMap, address); - if (entry == null) { - return; - } - final BleConnectCallback callback = entry.getValue(); - final BleDevice device = entry.getKey(); - switch (newState) { - case BluetoothProfile.STATE_CONNECTED: - //start discovering services, only services are found do we deem - //connection is successful - gatt.discoverServices(); - break; - case BluetoothProfile.STATE_DISCONNECTED: - device.connected = false; - gatt.close(); - removeDevice(device); - mHandler.post(new Runnable() { - @Override - public void run() { - if (callback != null) { - callback.onDisconnected(device); + if (status == BluetoothGatt.GATT_SUCCESS) { + String address = gatt.getDevice().getAddress(); + Map.Entry entry = findMapEntry(mConnectCallbackMap, address); + if (entry == null) { + return; + } + final BleConnectCallback callback = entry.getValue(); + final BleDevice device = entry.getKey(); + switch (newState) { + case BluetoothProfile.STATE_CONNECTED: + //start discovering services, only services are found do we deem + //connection is successful + gatt.discoverServices(); + break; + case BluetoothProfile.STATE_DISCONNECTED: + device.connected = false; + refreshDeviceCache(gatt); + gatt.close(); + removeDevice(device); + mHandler.post(new Runnable() { + @Override + public void run() { + if (callback != null) { + callback.onDisconnected(device); + } } - } - }); - break; + }); + break; + } } } @Override public void onServicesDiscovered(BluetoothGatt gatt, int status) { super.onServicesDiscovered(gatt, status); - String address = gatt.getDevice().getAddress(); - Map.Entry entry = findMapEntry(mConnectCallbackMap, address); - if (entry == null) { - return; - } - - final Map> servicesInfoMap = new HashMap<>(); - List gattServices = gatt.getServices(); - for (BluetoothGattService service : gattServices) { - String uuid = service.getUuid().toString(); - ServiceInfo serviceInfo = new ServiceInfo(uuid); - List charactInfos = new ArrayList<>(); - for (BluetoothGattCharacteristic ch : service.getCharacteristics()) { - String chUuid = ch.getUuid().toString(); - boolean readable = (ch.getProperties() & BluetoothGattCharacteristic.PROPERTY_READ) > 0; - boolean writeable = (ch.getProperties() & (BluetoothGattCharacteristic.PROPERTY_WRITE | - BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE)) > 0; - boolean notify = (ch.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0; - boolean indicate = (ch.getProperties() & BluetoothGattCharacteristic.PROPERTY_INDICATE) > 0; - CharacteristicInfo charactInfo = new CharacteristicInfo(chUuid, readable, writeable, notify, indicate); - charactInfos.add(charactInfo); + if (status == BluetoothGatt.GATT_SUCCESS) { + String address = gatt.getDevice().getAddress(); + Map.Entry entry = findMapEntry(mConnectCallbackMap, address); + if (entry == null) { + return; } - servicesInfoMap.put(serviceInfo, charactInfos); - } - mServicesMap.put(address, servicesInfoMap); + List gattServices = gatt.getServices(); + Map> servicesInfoMap = getServiceInfoMap(gattServices); + mServicesMap.put(address, servicesInfoMap); - if (!mConnectedDevices.contains(address)) { - mConnectedDevices.add(address); - } + if (!mConnectedDevices.contains(address)) { + mConnectedDevices.add(address); + } - final BleConnectCallback callback = entry.getValue(); - final BleDevice device = entry.getKey(); - //remove connection timeout message - mHandler.removeCallbacksAndMessages(address); - device.connected = true; - device.connecting = false; - mHandler.post(new Runnable() { - @Override - public void run() { - if (callback != null) { - callback.onConnected(device); + final BleConnectCallback callback = entry.getValue(); + final BleDevice device = entry.getKey(); + //remove connection timeout message + mHandler.removeCallbacksAndMessages(address); + device.connected = true; + device.connecting = false; + mHandler.post(new Runnable() { + @Override + public void run() { + if (callback != null) { + callback.onConnected(device); + } } - } - }); + }); + } } @Override public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { super.onCharacteristicRead(gatt, characteristic, status); - String address = gatt.getDevice().getAddress(); - String serviceUuid = characteristic.getService().getUuid().toString(); - String characteristicUuid = characteristic.getUuid().toString(); - UuidIdentify identify = getUuidIdentifyFromMap(mReadCallbackMap, address, serviceUuid, characteristicUuid); - if (identify == null) { - return; - } - final BleReadCallback callback = mReadCallbackMap.get(identify); - final BleDevice device = getBleDeviceFromMap(address, mConnectCallbackMap); - final byte[] data = characteristic.getValue(); - mHandler.post(new Runnable() { - @Override - public void run() { - if (callback != null) { - callback.onRead(data, device); - } + if (status == BluetoothGatt.GATT_SUCCESS) { + String address = gatt.getDevice().getAddress(); + String serviceUuid = characteristic.getService().getUuid().toString(); + String characteristicUuid = characteristic.getUuid().toString(); + UuidIdentify identify = getUuidIdentifyFromMap(mReadCallbackMap, address, serviceUuid, characteristicUuid); + if (identify == null) { + return; } - }); + final BleReadCallback callback = mReadCallbackMap.get(identify); + final BleDevice device = getBleDeviceFromMap(address, mConnectCallbackMap); + final byte[] data = characteristic.getValue(); + mHandler.post(new Runnable() { + @Override + public void run() { + if (callback != null) { + callback.onRead(data, device); + } + } + }); + } } @Override public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { super.onCharacteristicWrite(gatt, characteristic, status); - String address = gatt.getDevice().getAddress(); - String serviceUuid = characteristic.getService().getUuid().toString(); - String characteristicUuid = characteristic.getUuid().toString(); - UuidIdentify identify = getUuidIdentifyFromMap(mWrtieCallbackMap, address, serviceUuid, characteristicUuid); - if (identify == null) { - return; - } - final BleWriteCallback callback = mWrtieCallbackMap.get(identify); - final BleDevice device = getBleDeviceFromMap(address, mConnectCallbackMap); - final byte[] data = characteristic.getValue(); - mHandler.post(new Runnable() { - @Override - public void run() { - if (callback != null) { - callback.onWrite(data, device); - } + if (status == BluetoothGatt.GATT_SUCCESS) { + String address = gatt.getDevice().getAddress(); + String serviceUuid = characteristic.getService().getUuid().toString(); + String characteristicUuid = characteristic.getUuid().toString(); + UuidIdentify identify = getUuidIdentifyFromMap(mWrtieCallbackMap, address, serviceUuid, characteristicUuid); + if (identify == null) { + return; } - }); + final BleWriteCallback callback = mWrtieCallbackMap.get(identify); + final BleDevice device = getBleDeviceFromMap(address, mConnectCallbackMap); + final byte[] data = characteristic.getValue(); + mHandler.post(new Runnable() { + @Override + public void run() { + if (callback != null) { + callback.onWrite(data, device); + } + } + }); + } } @Override @@ -251,33 +243,37 @@ public void run() { @Override public void onReadRemoteRssi(BluetoothGatt gatt, final int rssi, int status) { super.onReadRemoteRssi(gatt, rssi, status); - String address = gatt.getDevice().getAddress(); - final BleRssiCallback callback = mRssiCallbackMap.get(address); - final BleDevice device = getBleDeviceFromMap(address, mConnectCallbackMap); - mHandler.post(new Runnable() { - @Override - public void run() { - if (callback != null) { - callback.onRssi(rssi, device); + if (status == BluetoothGatt.GATT_SUCCESS) { + String address = gatt.getDevice().getAddress(); + final BleRssiCallback callback = mRssiCallbackMap.get(address); + final BleDevice device = getBleDeviceFromMap(address, mConnectCallbackMap); + mHandler.post(new Runnable() { + @Override + public void run() { + if (callback != null) { + callback.onRssi(rssi, device); + } } - } - }); + }); + } } @Override public void onMtuChanged(BluetoothGatt gatt, final int mtu, int status) { super.onMtuChanged(gatt, mtu, status); - String address = gatt.getDevice().getAddress(); - final BleMtuCallback callback = mMtuCallbackMap.get(address); - final BleDevice device = getBleDeviceFromMap(address, mConnectCallbackMap); - mHandler.post(new Runnable() { - @Override - public void run() { - if (callback != null) { - callback.onMtuChanged(mtu, device); + if (status == BluetoothGatt.GATT_SUCCESS) { + String address = gatt.getDevice().getAddress(); + final BleMtuCallback callback = mMtuCallbackMap.get(address); + final BleDevice device = getBleDeviceFromMap(address, mConnectCallbackMap); + mHandler.post(new Runnable() { + @Override + public void run() { + if (callback != null) { + callback.onMtuChanged(mtu, device); + } } - } - }); + }); + } } }; @@ -338,6 +334,7 @@ public void run() { @Override public void run() { device.connecting = false; + refreshDeviceCache(gatt); gatt.close(); mGattMap.remove(device.address); if (callback != null) { @@ -367,7 +364,7 @@ public void disconnect(String address) { return; } gatt.disconnect(); -// refreshDeviceCache(gatt); + refreshDeviceCache(gatt); gatt.close(); //remove connection timeout message if a connection attempt currently is in progress mHandler.removeCallbacksAndMessages(address); @@ -752,6 +749,27 @@ private UuidIdentify getUuidIdentifyFromMap(Map map, String return null; } + private Map> getServiceInfoMap(List gattServices) { + Map> servicesInfoMap = new HashMap<>(); + for (BluetoothGattService service : gattServices) { + String uuid = service.getUuid().toString(); + ServiceInfo serviceInfo = new ServiceInfo(uuid); + List charactInfos = new ArrayList<>(); + for (BluetoothGattCharacteristic ch : service.getCharacteristics()) { + String chUuid = ch.getUuid().toString(); + boolean readable = (ch.getProperties() & BluetoothGattCharacteristic.PROPERTY_READ) > 0; + boolean writeable = (ch.getProperties() & (BluetoothGattCharacteristic.PROPERTY_WRITE | + BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE)) > 0; + boolean notify = (ch.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0; + boolean indicate = (ch.getProperties() & BluetoothGattCharacteristic.PROPERTY_INDICATE) > 0; + CharacteristicInfo charactInfo = new CharacteristicInfo(chUuid, readable, writeable, notify, indicate); + charactInfos.add(charactInfo); + } + servicesInfoMap.put(serviceInfo, charactInfos); + } + return servicesInfoMap; + } + private void removeDevice(BleDevice device) { mConnectCallbackMap.remove(device); mMtuCallbackMap.remove(device.address); diff --git a/sample/src/main/java/com/ficat/sample/MainActivity.java b/sample/src/main/java/com/ficat/sample/MainActivity.java index 68b1f4b..d916e78 100644 --- a/sample/src/main/java/com/ficat/sample/MainActivity.java +++ b/sample/src/main/java/com/ficat/sample/MainActivity.java @@ -26,7 +26,7 @@ import com.ficat.easypermissions.Permission; import com.ficat.easypermissions.RequestSubscriber; import com.ficat.sample.adapter.ScanDeviceAdapter; -import com.ficat.sample.common.CommonRecyclerViewAdapter; +import com.ficat.sample.adapter.common.CommonRecyclerViewAdapter; import java.util.ArrayList; import java.util.HashMap; @@ -208,7 +208,7 @@ public void onFinish() { private void testReadRssi() { if (manager.getConnectedDevices().size() <= 0) { - Toast.makeText(MainActivity.this, "No connected device", Toast.LENGTH_SHORT).show(); + Toast.makeText(MainActivity.this, "No connected devices", Toast.LENGTH_SHORT).show(); return; } //we use the first connected device to test @@ -228,7 +228,7 @@ public void onFail(int failCode, String info, BleDevice device) { private void testSetMtu() { if (manager.getConnectedDevices().size() <= 0) { - Toast.makeText(MainActivity.this, "No connected device", Toast.LENGTH_SHORT).show(); + Toast.makeText(MainActivity.this, "No connected devices", Toast.LENGTH_SHORT).show(); return; } //we use the first connected device to test @@ -249,7 +249,7 @@ public void onFail(int failCode, String info, BleDevice device) { private void testWrite() { if (manager.getConnectedDevices().size() <= 0) { - Toast.makeText(MainActivity.this, "No connected device", Toast.LENGTH_SHORT).show(); + Toast.makeText(MainActivity.this, "No connected devices", Toast.LENGTH_SHORT).show(); return; } //we use the first connected device to test @@ -274,7 +274,7 @@ public void onFail(int failCode, String info, BleDevice device) { private void testNotify() { if (manager.getConnectedDevices().size() <= 0) { - Toast.makeText(MainActivity.this, "No connected device", Toast.LENGTH_SHORT).show(); + Toast.makeText(MainActivity.this, "No connected devices", Toast.LENGTH_SHORT).show(); return; } //we use the first connected device to test diff --git a/sample/src/main/java/com/ficat/sample/adapter/ScanDeviceAdapter.java b/sample/src/main/java/com/ficat/sample/adapter/ScanDeviceAdapter.java index 28ecc51..700595a 100644 --- a/sample/src/main/java/com/ficat/sample/adapter/ScanDeviceAdapter.java +++ b/sample/src/main/java/com/ficat/sample/adapter/ScanDeviceAdapter.java @@ -7,7 +7,7 @@ import com.ficat.easyble.BleDevice; import com.ficat.sample.R; -import com.ficat.sample.common.CommonRecyclerViewAdapter; +import com.ficat.sample.adapter.common.CommonRecyclerViewAdapter; import java.util.List; diff --git a/sample/src/main/java/com/ficat/sample/common/CommonRecyclerViewAdapter.java b/sample/src/main/java/com/ficat/sample/adapter/common/CommonRecyclerViewAdapter.java similarity index 93% rename from sample/src/main/java/com/ficat/sample/common/CommonRecyclerViewAdapter.java rename to sample/src/main/java/com/ficat/sample/adapter/common/CommonRecyclerViewAdapter.java index a685d89..dd98c9b 100644 --- a/sample/src/main/java/com/ficat/sample/common/CommonRecyclerViewAdapter.java +++ b/sample/src/main/java/com/ficat/sample/adapter/common/CommonRecyclerViewAdapter.java @@ -1,4 +1,4 @@ -package com.ficat.sample.common; +package com.ficat.sample.adapter.common; import android.content.Context; import android.support.annotation.NonNull; @@ -67,7 +67,7 @@ public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) { MyViewHolder mHolder = (MyViewHolder) holder; if (mClickListener != null) { - mHolder.mItemView.setOnClickListener(new View.OnClickListener() { + mHolder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mClickListener.onItemClick(view, position); @@ -75,8 +75,8 @@ public void onClick(View view) { }); } if (mLongClickListener != null) { - mHolder.mItemView.setLongClickable(true); - mHolder.mItemView.setOnLongClickListener(new View.OnLongClickListener() { + mHolder.itemView.setLongClickable(true); + mHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View view) { mLongClickListener.onItemLongClick(view, position); @@ -110,11 +110,9 @@ public int getItemViewType(int position) { public static class MyViewHolder extends RecyclerView.ViewHolder { public SparseArray mViews; - public View mItemView; public MyViewHolder(@NonNull View itemView, @NonNull int[] resViewIds) { super(itemView); - this.mItemView = itemView; mViews = new SparseArray<>(); for (int viewId : resViewIds) { View view = itemView.findViewById(viewId);