所有。 我尝试使用谷歌chromecast创建一个Android演员应用程序。所以我试着用这段代码找到chromecast:
public void startDiscoveringDevices() {
DiscoveryManager.init(mContext);
mDiscoveryManager = DiscoveryManager.getInstance();
mDiscoveryManager.registerDeviceService(CastService.class, CastDiscoveryProvider.class);
mDiscoveryManager.setPairingLevel(DiscoveryManager.PairingLevel.ON);
mDiscoveryManager.setCapabilityFilters(new CapabilityFilter(
MediaPlayer.Any,
MediaControl.Any,
VolumeControl.Any
));
mDiscoveryManager.addListener(this);
mDiscoveryManager.start();
Log.wtf("CastDeviceManager", "discovery manager was started");
}
之后我听了DiscoveryManagerListener:
// ********************* My DiscoveryManagerListener ********************************** //
@Override
public void onDeviceAdded(DiscoveryManager manager, ConnectableDevice device) {
Log.wtf("DiscoveryManagerListener", "Device added");
device.addListener(...); // Here I pass implementation of ConnectableDeviceListener interface.
device.connect(); // connect to device
}
@Override
public void onDeviceUpdated(DiscoveryManager manager, ConnectableDevice device) {
Log.wtf("DiscoveryManagerListener", "Device updated");
device.addListener(...); // Here I pass implementation of ConnectableDeviceListener interface.
// try to connect to device, if it not connected yet
if (device.isConnected()) {
device.disconnect();
}
else {
device.connect();
}
}
@Override
public void onDeviceRemoved(DiscoveryManager manager, ConnectableDevice device) {
Log.wtf("DiscoveryManagerListener", "Device removed");
mCastDevices.remove(device.hashCode()); // remove device from list of devices
}
@Override
public void onDiscoveryFailed(DiscoveryManager manager, ServiceCommandError error) {
Log.wtf("DiscoveryManagerListener", "Device(s) not found", error);
destroyDiscoveryManager();
}
// ************************************************************************************ //
// ************************** My ConnectableDeviceListener **************************** //
@Override
public void onDeviceReady(ConnectableDevice device) {
Log.wtf("ConnectableDeviceListener", "device ready");
outDeviceInfo(device, "ConnectableDeviceListener");
// put to map with devices new connected device
mCastDevices.put(device.hashCode(), new CastDevice(device, mCastLifecycleListener));
Log.wtf("ConnectableDeviceListener", "Count cast devices: " + mCastDevices.size());
}
@Override
public void onDeviceDisconnected(ConnectableDevice device) {
Log.wtf("ConnectableDeviceListener", "device disconnected");
destroyDiscoveryManager();
}
@Override
public void onPairingRequired(ConnectableDevice device, DeviceService service, PairingType pairingType) {
Log.wtf("ConnectableDeviceListener", "device paring required");
}
@Override
public void onCapabilityUpdated(ConnectableDevice device, List<String> added, List<String> removed) {
Log.wtf("ConnectableDeviceListener", "capability updated");
}
@Override
public void onConnectionFailed(ConnectableDevice device, ServiceCommandError error) {
Log.wtf("ConnectableDeviceListener", "connection failed");
destroyDiscoveryManager();
}
// ************************************************************************************ //
// this method I use to disconnect from devices in map and stop discovering new devices
public void destroyDiscoveryManager() {
mDiscoveryManager.stop();
DiscoveryManager.destroy();
Log.wtf("CastDeviceManager", "discovery manager was stopped");
for (Map.Entry<Integer, CastDevice> castDeviceEntry: mCastDevices.entrySet()) {
castDeviceEntry.getValue().stopCast();
castDeviceEntry.getValue().disconnect();
Log.wtf("CastDevice", "device.isConnected(): " + castDeviceEntry.getValue().isConnected());
}
mCastDevices.clear();
}
在我的Activity中,我通过按钮调用startDiscoveringDevices()和destroyDiscoveryManager()。一切都很好,但有一段时间它停止寻找设备。我没有在代码中改变任何东西。 也许我用bug制作了一些东西并且它有效,但是现在发生了一些事情并且它现在不起作用了?我做错了什么?