2017-09-21 22 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); 
     } 
    } 

或者你可以注册一个广播接收器,并很容易地把它出来呀。

相关问题