2017-09-21 27 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

您可以做一個Singleton模式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); 
     } 
    } 

或者你可以註冊一個廣播接收器,並很容易地把它出來呀。

相關問題