2017-09-21 14 views
-1

함께 BLE 장치에 연결 한 후에 서비스 및 특성을 얻었습니다. 이 코드 :FLEMENT에서 BLE 서비스 및 특성을 작업에 전달하는 최상의 솔루션은 무엇입니까?

if(gattServices == null)return; 
    //loops through available GATT SERVICES 
    for(BluetoothGattService gattService : gattServices){ 
     uuid = gattService.getUuid().toString(); 
     System.out.println("Service discovered: " + uuid); 
     new ArrayList<HashMap<String, String>>(); 
     List<BluetoothGattCharacteristic> gattCharacteristics = gattService.getCharacteristics(); 
     //loops through available characteristics 
     for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics){ 
      final String charUuid = gattCharacteristic.getUuid().toString(); 
      System.out.println("Characteristic discovered: " + charUuid); 

     } 
    } 
} 

이제 내 앱의 또 다른 활동에서 이러한 서비스 및 특성을 표시 할, 그러나 문제는 내가이 일을하는 가장 좋은 방법이 무엇인지 알 수 있다는 것입니다. 누군가 제게 조언을 해줄 수 있나요?

답변

0

당신은 모든 것을 청취자 자신을 설정하고 현재 가입되어 청취자에게 호출하는 싱글 톤 패턴 BLEManager을, 그래서 인터페이스를 만들 수 있습니다 :

public interface IBLEComListener { 

/*///////////////////////////////////////////////////////////////////////////////////////// 
// PUBLIC METHODS. 
*////////////////////////////////////////////////////////////////////////////////////////// 
/** 
* To notify BLE connected. 
* 
* @param bluetoothDevice the instance of connected BluetoothDevice. 
*/ 
void onBLEDeviceConnected(BluetoothDevice bluetoothDevice); 
/** 
* To notify BLE disconnected. 
* 
* @param bluetoothDevice the instance of connected BluetoothDevice. 
*/ 
void onBLEDeviceDisconnected(BluetoothDevice bluetoothDevice); 
/** 
* To notify when unable to initialize Bluetooth. 
*/ 
void onBLEUnableToInitializeBluetooth(); 
/** 
* To notify Services ready for Connected BLE Device. 
* 
* @param bluetoothDevice the instance of connected BluetoothDevice. 
*/ 
void onBLEDeviceServicesReady(BluetoothDevice bluetoothDevice); 
/** 
* To notify when service not found into BLE device. 
* 
* @param bluetoothDevice the instance of connected BluetoothDevice. 
*/ 
void onServiceNotFound(BluetoothDevice bluetoothDevice); 
/** 
* To notify when characteristic notification. 
* 
* @param bluetoothDevice the instance of connected BluetoothDevice. 
* @param bleMessageModelList the instance list of BLEMessageModel. 
*/ 
void onBLECharacteristicNotificationReceived(BluetoothDevice bluetoothDevice, List<BLEMessageModel> bleMessageModelList); 
/** 
* To notify when message arrived. 
* 
* @param bluetoothDevice the instance of connected BluetoothDevice. 
* @param characteristicDescriptorIdentifier the ENUM to identify the Characteristic or Descriptor. 
* @param bleMessageModelList the instance list of BLEMessageModel. 
*/ 
void onBLEMessageReceived(BluetoothDevice bluetoothDevice, CharacteristicDescriptorIdentifier characteristicDescriptorIdentifier, List<BLEMessageModel> bleMessageModelList); 
/** 
* To notify when message Sent. 
* 
* @param bluetoothDevice the instance of connected BluetoothDevice. 
* @param characteristicDescriptorIdentifier the ENUM to identify the Characteristic or Descriptor. 
*/ 
void onBLEMessageSent(BluetoothDevice bluetoothDevice, CharacteristicDescriptorIdentifier characteristicDescriptorIdentifier); 
/** 
* To notify when bluetooth off/disabled. 
*/ 
void onBluetoothDisabled(); 
/** 
* To notify when bluetooth on/enabled. 
*/ 
void onBluetoothEnabled(); 
/** 
* To notify BLE devices discovered/updated. 
* 
* @param deviceModel the BLE device. 
*/ 
    void onBLEDeviceDiscovered(DeviceModel deviceModel); 


} 

그리고 단순히 자신을 묶을 당신의 과 같은 콜백에 대한 싱글 톤 MyBLEManager.getInstance (this, this) // 컨텍스트, 리스너

그런 다음 관리자가 응답을 보내도록합니다. 그런 다음 BLE 연결을 처리하는 서비스 클래스에서 단지 좋아합니까 :

@Override 
    public void onServicesDiscovered(BluetoothGatt gatt, int status) { 
     if (status == BluetoothGatt.GATT_SUCCESS) { 
      mServicesReadyBluetoothDeviceMap.clear(); 
      for(BluetoothDevice bluetoothDevice : gatt.getConnectedDevices()) { 
       mServicesReadyBluetoothDeviceMap.put(bluetoothDevice.getAddress(), bluetoothDevice); 
      } 
      IBLEComListener comListener = A35BLEManager.getBLEComListener(); 
      if(comListener != null) { 
       comListener.onBLEDeviceServicesReady(gatt.getDevice()); 
      } 
     } else { 
      A35Log.w(TAG, "onServicesDiscovered received: " + status); 
     } 
    } 

은 또한 당신은 방송 수신기를 등록 할 수 있고, 그냥 간단하게 그런 식으로 그것을 밖으로 손.