2017-09-21 4 views
-1

私はBLEデバイスに接続した後、一緒にサービスと特性を得ました。このコードでは: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); 
     } 
    } 

は、別の方法としては、放送受信機を登録する可能性があり、同じように簡単にそのようにそれを配ります。

関連する問題